fix(metrics,logs): Don't attach span_id if no active span
#6162
1 issue
find-bugs: Found 1 issue (1 medium)
Medium
else branch overwrites a pre-existing telemetry['span_id'] with external propagation context - `sentry_sdk/scope.py:1855-1866`
The original code only set telemetry['span_id'] when telemetry.get('span_id') is None. In the new structure, the else branch runs whenever the if-condition fails for any reason — including the case where telemetry['span_id'] is already populated by the caller. In that else branch, if an external propagation context exists, it unconditionally overwrites the already-set telemetry['span_id']. This is a behavioral regression: any caller that pre-populated span_id on the telemetry payload (e.g. logs/metrics constructed with an explicit span_id) will have it silently replaced when external (e.g. OTel) propagation is registered, leading to incorrect span_id values reported to Sentry.
Duration: 40.6s · Tokens: 149.9k in / 2.3k out · Cost: $0.68 (+extraction: $0.00)
Annotations
Check warning on line 1866 in sentry_sdk/scope.py
sentry-warden / warden: find-bugs
else branch overwrites a pre-existing telemetry['span_id'] with external propagation context
The original code only set telemetry['span_id'] when telemetry.get('span_id') is None. In the new structure, the else branch runs whenever the if-condition fails for any reason — including the case where telemetry['span_id'] is already populated by the caller. In that else branch, if an external propagation context exists, it unconditionally overwrites the already-set telemetry['span_id']. This is a behavioral regression: any caller that pre-populated span_id on the telemetry payload (e.g. logs/metrics constructed with an explicit span_id) will have it silently replaced when external (e.g. OTel) propagation is registered, leading to incorrect span_id values reported to Sentry.