Skip to content

fix(metrics): omit span_id when no span is active - #2403

Merged
Litarnus merged 1 commit into
5.xfrom
fix/metric-span-id-without-active-span
Nov 19, 2026
Merged

Litarnus merged 1 commit into
5.xfrom
fix/metric-span-id-without-active-span

Conversation

@Litarnus

Copy link
Copy Markdown
Collaborator

Description

A metric recorded without an active span carried a span_id that belongs to no span. MetricsAggregator read it from the scope's trace context, which falls back to the propagation context's random span ID when no span is active.

Metrics now only get a span_id when a span is active or an external propagation context provides one. Otherwise span_id is left out of the envelope item. The trace ID is unchanged.

This matches the metrics spec (span_id is optional and is the span that was active when the metric was emitted) and what sentry-python and sentry-javascript do.

Behaviour change: Metric::getSpanId() now returns ?SpanId. A before_send_metric callback that calls it without a span active gets null instead of a synthetic ID.

In the serialized item span_id now comes after attributes; key order has no meaning for ingestion.

Issues

None.

🤖 Generated with Claude Code

@Litarnus
Litarnus requested review from a team and stayallive as code owners November 19, 2026 15:28
@Litarnus
Litarnus changed the base branch from master to 5.x November 19, 2026 15:31
Metrics took their span ID from the scope's trace context, which falls
back to the propagation context when no span is active. That span ID
is random and belongs to no span, so the metric pointed at a span that
does not exist.

Only set the span ID when a span is active or an external propagation
context provides one, and leave it out of the envelope item otherwise.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Litarnus
Litarnus force-pushed the fix/metric-span-id-without-active-span branch from 54a8af4 to dbd3d4a Compare November 19, 2026 15:34
@Litarnus
Litarnus merged commit 0eb9317 into 5.x Nov 19, 2026
54 checks passed
@Litarnus
Litarnus deleted the fix/metric-span-id-without-active-span branch November 19, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant