fix(metrics,logs): Don't attach span_id if no active span
#6162
2 issues
Medium
apply_to_telemetry uses global current span instead of self's span - `sentry_sdk/scope.py:1855-1857`
The new code calls sentry_sdk.get_current_span() which internally invokes sentry_sdk.get_current_scope() rather than using self. The previous implementation derived span_id from self.get_trace_context(). If apply_to_telemetry is ever invoked on a scope other than the global current scope (e.g. an isolation/merged scope passed in by the client), the attached span_id will reflect the globally active span rather than the scope's own span. This is a subtle behavioral/side-effect change that may attach the wrong span_id to logs/metrics in nested or forked scope contexts.
Low
span_id resolved from contextvar current scope rather than the merged scope passed to apply_to_telemetry - `sentry_sdk/scope.py:1855-1857`
The new code calls sentry_sdk.get_current_span(), which internally resolves the scope via sentry_sdk.get_current_scope() (a contextvar lookup) instead of using self. However, apply_to_telemetry is invoked on a merged scope produced by Scope._merge_scopes() and passed through client._capture_telemetry. In async/threaded paths (or any case where the contextvar current scope differs from the merged scope at apply time), this can attach a span_id from an unrelated active span, or fail to attach one even though the merged scope has a span. The previous implementation derived the span via self.get_trace_context(), which was scope-consistent. Using self.span (or passing self to get_current_span) would preserve the prior scope semantics while still avoiding the synthesized propagation-context span_id.
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 1 | 59.8s | $0.53 |
| find-bugs | 1 | 1m 8s | $0.85 |
| skill-scanner | 0 | 23.9s | $0.18 |
| security-review | 0 | 18.7s | $0.19 |
Duration: 2m 50s · Tokens: 639.6k in / 6.4k out · Cost: $1.74