Skip to content

Commit

Permalink
Make set_measurement public api and remove experimental status (#1909)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
  • Loading branch information
sl0thentr0py and antonpirker committed Feb 17, 2023
1 parent f62c83d commit 0dcd082
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"set_extra",
"set_user",
"set_level",
"set_measurement",
]

# Initialize the debug support after everything is loaded
Expand Down
17 changes: 16 additions & 1 deletion sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
from typing import ContextManager
from typing import Union

from sentry_sdk._types import Event, Hint, Breadcrumb, BreadcrumbHint, ExcInfo
from sentry_sdk._types import (
Event,
Hint,
Breadcrumb,
BreadcrumbHint,
ExcInfo,
MeasurementUnit,
)
from sentry_sdk.tracing import Span, Transaction

T = TypeVar("T")
Expand Down Expand Up @@ -45,6 +52,7 @@ def overload(x):
"set_extra",
"set_user",
"set_level",
"set_measurement",
]


Expand Down Expand Up @@ -213,3 +221,10 @@ def start_transaction(
):
# type: (...) -> Union[Transaction, NoOpSpan]
return Hub.current.start_transaction(transaction, **kwargs)


def set_measurement(name, value, unit=""):
# type: (str, float, MeasurementUnit) -> None
transaction = Hub.current.scope.transaction
if transaction is not None:
transaction.set_measurement(name, value, unit)
1 change: 0 additions & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"max_spans": Optional[int],
"record_sql_params": Optional[bool],
"smart_transaction_trimming": Optional[bool],
"custom_measurements": Optional[bool],
"profiles_sample_rate": Optional[float],
"profiler_mode": Optional[str],
},
Expand Down
10 changes: 1 addition & 9 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,19 +632,12 @@ def finish(self, hub=None, end_timestamp=None):
contexts.update({"profile": self._profile.get_profile_context()})
self._profile = None

if has_custom_measurements_enabled():
event["measurements"] = self._measurements
event["measurements"] = self._measurements

return hub.capture_event(event)

def set_measurement(self, name, value, unit=""):
# type: (str, float, MeasurementUnit) -> None
if not has_custom_measurements_enabled():
logger.debug(
"[Tracing] Experimental custom_measurements feature is disabled"
)
return

self._measurements[name] = {"value": value, "unit": unit}

def set_context(self, key, value):
Expand Down Expand Up @@ -819,5 +812,4 @@ def finish(self, hub=None, end_timestamp=None):
has_tracing_enabled,
is_valid_sample_rate,
maybe_create_breadcrumbs_from_span,
has_custom_measurements_enabled,
)
7 changes: 0 additions & 7 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,6 @@ def _format_sql(cursor, sql):
return real_sql or to_string(sql)


def has_custom_measurements_enabled():
# type: () -> bool
client = sentry_sdk.Hub.current.client
options = client and client.options
return bool(options and options["_experiments"].get("custom_measurements"))


class Baggage(object):
__slots__ = ("sentry_items", "third_party_items", "mutable")

Expand Down
18 changes: 16 additions & 2 deletions tests/tracing/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os

import sentry_sdk
from sentry_sdk import Hub, start_span, start_transaction
from sentry_sdk import Hub, start_span, start_transaction, set_measurement
from sentry_sdk.tracing import Span, Transaction

try:
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_circular_references(monkeypatch, sentry_init, request):


def test_set_meaurement(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"custom_measurements": True})
sentry_init(traces_sample_rate=1.0)

events = capture_events()

Expand All @@ -257,3 +257,17 @@ def test_set_meaurement(sentry_init, capture_events):
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}
assert event["measurements"]["metric.baz"] == {"value": 420.69, "unit": "custom"}
assert event["measurements"]["metric.foobar"] == {"value": 17.99, "unit": "percent"}


def test_set_meaurement_public_api(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)

events = capture_events()

with start_transaction(name="measuring stuff"):
set_measurement("metric.foo", 123)
set_measurement("metric.bar", 456, unit="second")

(event,) = events
assert event["measurements"]["metric.foo"] == {"value": 123, "unit": ""}
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}

0 comments on commit 0dcd082

Please sign in to comment.