Skip to content
26 changes: 10 additions & 16 deletions src/sentry/utils/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import logging
import sys
import typing
from collections.abc import Generator, Mapping, Sequence, Sized
from types import FrameType
from typing import TYPE_CHECKING, Any, NamedTuple
Expand Down Expand Up @@ -237,12 +238,15 @@ def before_send_transaction(event: Event, _: Hint) -> Event | None:
num_of_spans = len(event["spans"])

event["tags"]["spans_over_limit"] = str(num_of_spans >= 1000)
if not event["measurements"]:
event["measurements"] = {}
event["measurements"]["num_of_spans"] = {
"value": num_of_spans,
"unit": None,
}

# Type safety: `event["contexts"]["trace"]["data"]` is a dictionary if it is set.
# See https://develop.sentry.dev/sdk/data-model/event-payloads/contexts/#trace-context.
data = typing.cast(
dict[str, object],
event.setdefault("contexts", {}).setdefault("trace", {}).setdefault("data", {}),
)
data["num_of_spans"] = num_of_spans

return event


Expand Down Expand Up @@ -695,15 +699,6 @@ def parse_org_slug(x: Organization | RpcOrganization | str) -> str:
)


def set_measurement(measurement_name, value, unit=None):
try:
transaction = sentry_sdk.Scope.get_current_scope().transaction
if transaction is not None:
transaction.set_measurement(measurement_name, value, unit)
except Exception:
pass


def set_span_attribute(data_name, value):
span = sentry_sdk.get_current_span()
if span is not None:
Expand Down Expand Up @@ -746,6 +741,5 @@ def merge_context_into_scope(
"patch_transport_for_instrumentation",
"isolation_scope",
"set_current_event_project",
"set_measurement",
"traces_sampler",
)
Loading