Skip to content

Commit 0dcd082

Browse files
Make set_measurement public api and remove experimental status (#1909)
Co-authored-by: Anton Pirker <[email protected]>
1 parent f62c83d commit 0dcd082

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

sentry_sdk/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"set_extra",
3232
"set_user",
3333
"set_level",
34+
"set_measurement",
3435
]
3536

3637
# Initialize the debug support after everything is loaded

sentry_sdk/api.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
from typing import ContextManager
1717
from typing import Union
1818

19-
from sentry_sdk._types import Event, Hint, Breadcrumb, BreadcrumbHint, ExcInfo
19+
from sentry_sdk._types import (
20+
Event,
21+
Hint,
22+
Breadcrumb,
23+
BreadcrumbHint,
24+
ExcInfo,
25+
MeasurementUnit,
26+
)
2027
from sentry_sdk.tracing import Span, Transaction
2128

2229
T = TypeVar("T")
@@ -45,6 +52,7 @@ def overload(x):
4552
"set_extra",
4653
"set_user",
4754
"set_level",
55+
"set_measurement",
4856
]
4957

5058

@@ -213,3 +221,10 @@ def start_transaction(
213221
):
214222
# type: (...) -> Union[Transaction, NoOpSpan]
215223
return Hub.current.start_transaction(transaction, **kwargs)
224+
225+
226+
def set_measurement(name, value, unit=""):
227+
# type: (str, float, MeasurementUnit) -> None
228+
transaction = Hub.current.scope.transaction
229+
if transaction is not None:
230+
transaction.set_measurement(name, value, unit)

sentry_sdk/consts.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"max_spans": Optional[int],
3434
"record_sql_params": Optional[bool],
3535
"smart_transaction_trimming": Optional[bool],
36-
"custom_measurements": Optional[bool],
3736
"profiles_sample_rate": Optional[float],
3837
"profiler_mode": Optional[str],
3938
},

sentry_sdk/tracing.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,19 +632,12 @@ def finish(self, hub=None, end_timestamp=None):
632632
contexts.update({"profile": self._profile.get_profile_context()})
633633
self._profile = None
634634

635-
if has_custom_measurements_enabled():
636-
event["measurements"] = self._measurements
635+
event["measurements"] = self._measurements
637636

638637
return hub.capture_event(event)
639638

640639
def set_measurement(self, name, value, unit=""):
641640
# type: (str, float, MeasurementUnit) -> None
642-
if not has_custom_measurements_enabled():
643-
logger.debug(
644-
"[Tracing] Experimental custom_measurements feature is disabled"
645-
)
646-
return
647-
648641
self._measurements[name] = {"value": value, "unit": unit}
649642

650643
def set_context(self, key, value):
@@ -819,5 +812,4 @@ def finish(self, hub=None, end_timestamp=None):
819812
has_tracing_enabled,
820813
is_valid_sample_rate,
821814
maybe_create_breadcrumbs_from_span,
822-
has_custom_measurements_enabled,
823815
)

sentry_sdk/tracing_utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,6 @@ def _format_sql(cursor, sql):
243243
return real_sql or to_string(sql)
244244

245245

246-
def has_custom_measurements_enabled():
247-
# type: () -> bool
248-
client = sentry_sdk.Hub.current.client
249-
options = client and client.options
250-
return bool(options and options["_experiments"].get("custom_measurements"))
251-
252-
253246
class Baggage(object):
254247
__slots__ = ("sentry_items", "third_party_items", "mutable")
255248

tests/tracing/test_misc.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55

66
import sentry_sdk
7-
from sentry_sdk import Hub, start_span, start_transaction
7+
from sentry_sdk import Hub, start_span, start_transaction, set_measurement
88
from sentry_sdk.tracing import Span, Transaction
99

1010
try:
@@ -232,7 +232,7 @@ def test_circular_references(monkeypatch, sentry_init, request):
232232

233233

234234
def test_set_meaurement(sentry_init, capture_events):
235-
sentry_init(traces_sample_rate=1.0, _experiments={"custom_measurements": True})
235+
sentry_init(traces_sample_rate=1.0)
236236

237237
events = capture_events()
238238

@@ -257,3 +257,17 @@ def test_set_meaurement(sentry_init, capture_events):
257257
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}
258258
assert event["measurements"]["metric.baz"] == {"value": 420.69, "unit": "custom"}
259259
assert event["measurements"]["metric.foobar"] == {"value": 17.99, "unit": "percent"}
260+
261+
262+
def test_set_meaurement_public_api(sentry_init, capture_events):
263+
sentry_init(traces_sample_rate=1.0)
264+
265+
events = capture_events()
266+
267+
with start_transaction(name="measuring stuff"):
268+
set_measurement("metric.foo", 123)
269+
set_measurement("metric.bar", 456, unit="second")
270+
271+
(event,) = events
272+
assert event["measurements"]["metric.foo"] == {"value": 123, "unit": ""}
273+
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}

0 commit comments

Comments
 (0)