Skip to content

Commit 28a87df

Browse files
authored
Deprecate set_measurement() API. (#3934)
Deprecate `set_measurement()`. This will be replaced by `set_data()` which internally is using the Otel `set_attribute()`. Fixes #3074
1 parent c6db420 commit 28a87df

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

sentry_sdk/api.py

+4
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def start_transaction(
388388

389389
def set_measurement(name, value, unit=""):
390390
# type: (str, float, MeasurementUnit) -> None
391+
"""
392+
.. deprecated:: 2.28.0
393+
This function is deprecated and will be removed in the next major release.
394+
"""
391395
transaction = get_current_scope().transaction
392396
if transaction is not None:
393397
transaction.set_measurement(name, value, unit)

sentry_sdk/tracing.py

+20
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,16 @@ def set_status(self, value):
613613

614614
def set_measurement(self, name, value, unit=""):
615615
# type: (str, float, MeasurementUnit) -> None
616+
"""
617+
.. deprecated:: 2.28.0
618+
This function is deprecated and will be removed in the next major release.
619+
"""
620+
621+
warnings.warn(
622+
"`set_measurement()` is deprecated and will be removed in the next major version. Please use `set_data()` instead.",
623+
DeprecationWarning,
624+
stacklevel=2,
625+
)
616626
self._measurements[name] = {"value": value, "unit": unit}
617627

618628
def set_thread(self, thread_id, thread_name):
@@ -1061,6 +1071,16 @@ def finish(
10611071

10621072
def set_measurement(self, name, value, unit=""):
10631073
# type: (str, float, MeasurementUnit) -> None
1074+
"""
1075+
.. deprecated:: 2.28.0
1076+
This function is deprecated and will be removed in the next major release.
1077+
"""
1078+
1079+
warnings.warn(
1080+
"`set_measurement()` is deprecated and will be removed in the next major version. Please use `set_data()` instead.",
1081+
DeprecationWarning,
1082+
stacklevel=2,
1083+
)
10641084
self._measurements[name] = {"value": value, "unit": unit}
10651085

10661086
def set_context(self, key, value):

tests/tracing/test_misc.py

+42
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,48 @@ def test_set_meaurement_public_api(sentry_init, capture_events):
323323
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}
324324

325325

326+
def test_set_measurement_deprecated(sentry_init):
327+
sentry_init(traces_sample_rate=1.0)
328+
329+
with start_transaction(name="measuring stuff") as trx:
330+
with pytest.warns(DeprecationWarning):
331+
set_measurement("metric.foo", 123)
332+
333+
with pytest.warns(DeprecationWarning):
334+
trx.set_measurement("metric.bar", 456)
335+
336+
with start_span(op="measuring span") as span:
337+
with pytest.warns(DeprecationWarning):
338+
span.set_measurement("metric.baz", 420.69, unit="custom")
339+
340+
341+
def test_set_meaurement_compared_to_set_data(sentry_init, capture_events):
342+
"""
343+
This is just a test to see the difference
344+
between measurements and data in the resulting event payload.
345+
"""
346+
sentry_init(traces_sample_rate=1.0)
347+
348+
events = capture_events()
349+
350+
with start_transaction(name="measuring stuff") as transaction:
351+
transaction.set_measurement("metric.foo", 123)
352+
transaction.set_data("metric.bar", 456)
353+
354+
with start_span(op="measuring span") as span:
355+
span.set_measurement("metric.baz", 420.69, unit="custom")
356+
span.set_data("metric.qux", 789)
357+
358+
(event,) = events
359+
assert event["measurements"]["metric.foo"] == {"value": 123, "unit": ""}
360+
assert event["contexts"]["trace"]["data"]["metric.bar"] == 456
361+
assert event["spans"][0]["measurements"]["metric.baz"] == {
362+
"value": 420.69,
363+
"unit": "custom",
364+
}
365+
assert event["spans"][0]["data"]["metric.qux"] == 789
366+
367+
326368
@pytest.mark.parametrize(
327369
"trace_propagation_targets,url,expected_propagation_decision",
328370
[

0 commit comments

Comments
 (0)