Skip to content

Commit 63df412

Browse files
committed
move more stuff to client
1 parent e36c40b commit 63df412

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

sentry_sdk/_metrics.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def _capture_metric(
2121
attributes=None, # type: Optional[dict[str, Any]]
2222
):
2323
# type: (...) -> None
24-
2524
client = sentry_sdk.get_client()
2625

2726
attrs = {} # type: dict[str, Union[str, bool, float, int]]
@@ -38,25 +37,10 @@ def _capture_metric(
3837
else safe_repr(v)
3938
)
4039

41-
span = sentry_sdk.get_current_span()
42-
trace_id = "00000000-0000-0000-0000-000000000000"
43-
span_id = None
44-
45-
if span:
46-
trace_context = span.get_trace_context()
47-
trace_id = trace_context.get("trace_id", trace_id)
48-
span_id = trace_context.get("span_id")
49-
else:
50-
scope = sentry_sdk.get_current_scope()
51-
if scope:
52-
propagation_context = scope._propagation_context
53-
if propagation_context:
54-
trace_id = propagation_context.trace_id or trace_id
55-
5640
metric = {
5741
"timestamp": time.time(),
58-
"trace_id": trace_id,
59-
"span_id": span_id,
42+
"trace_id": None,
43+
"span_id": None,
6044
"name": name,
6145
"type": metric_type,
6246
"value": float(value),

sentry_sdk/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class SDKInfo(TypedDict):
248248
"Metric",
249249
{
250250
"timestamp": float,
251-
"trace_id": str,
251+
"trace_id": Optional[str],
252252
"span_id": Optional[str],
253253
"name": str,
254254
"type": MetricType,

sentry_sdk/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,17 @@ def _capture_metric(self, metric):
976976
if release is not None and "sentry.release" not in metric["attributes"]:
977977
metric["attributes"]["sentry.release"] = release
978978

979+
span = sentry_sdk.get_current_span()
980+
trace_id = "00000000-0000-0000-0000-000000000000"
981+
982+
if span:
983+
metric["trace_id"] = span.trace_id or trace_id
984+
metric["span_id"] = span.span_id or None
985+
else:
986+
propagation_context = isolation_scope.get_active_propagation_context()
987+
if propagation_context:
988+
metric["trace_id"] = propagation_context.trace_id or trace_id
989+
979990
if isolation_scope._user is not None:
980991
for metric_attribute, user_attribute in (
981992
("user.id", "id"),

0 commit comments

Comments
 (0)