TraceContextTextMapPropagator injects invalid traceparent header (all zeros) when SpanContext is invalid
Maintainer thường phản hồi trong vòng 3 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- observability
Hướng nghiên cứu
Bắt đầu trong opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py và tái hiện ví dụ SpanContext không hợp lệ trong issue. Kiểm tra propagator và các guard liên quan của B3MultiFormat, B3SingleFormat và JaegerPropagator được đề cập trong báo cáo. Hoàn thành có nghĩa là các context không hợp lệ không inject traceparent hay tracestate, trong khi các context hợp lệ vẫn tiếp tục được propagate bình thường.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe your environment
- OS: Linux / macOS
- Python version: 3.10+
- SDK version: main (1.37.0.dev0)
- API version: main (1.37.0.dev0)
What happened?
In TraceContextTextMapPropagator.inject, the propagator guards against injecting invalid contexts by checking:
if span_context == trace.INVALID_SPAN_CONTEXT:
return
Because SpanContext is a tuple consisting of (trace_id, span_id, is_remote, trace_flags, trace_state, is_valid) and INVALID_SPAN_CONTEXT is statically defined with is_remote=False, trace_flags=0, and trace_state=DEFAULT_TRACE_STATE, any invalid SpanContext (i.e. where is_valid is False) that has:
is_remote=True- non-zero
trace_flags(such asTraceFlags.SAMPLED) - or non-empty
trace_state
evaluates span_context == trace.INVALID_SPAN_CONTEXT to False.
As a consequence, the guard fails to trigger and inject() proceeds to format and inject an illegal traceparent header containing all zeroes into outbound network requests:
traceparent: 00-00000000000000000000000000000000-0000000000000000-00
This violates:
- W3C Trace Context Specification: Section 3.2.2.3 forbids all-zero
trace-idandparent-id, and Section 4.2 states an implementation MUST NOT forward an invalidtraceparent. - OpenTelemetry Specification (TraceContext Propagator): "If the SpanContext is invalid, the propagator MUST NOT inject anything into the carrier."
Strict reverse proxies, service meshes (Envoy), API gateways, and downstream microservices may reject HTTP requests with invalid W3C headers with 400 Bad Request or drop the context.
Steps to Reproduce
from opentelemetry import trace
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from opentelemetry.trace.span import NonRecordingSpan, SpanContext
propagator = TraceContextTextMapPropagator()
# An invalid remote span context (e.g. from an upstream call)
invalid_span_context = SpanContext(trace_id=0, span_id=0, is_remote=True)
print("is_valid:", invalid_span_context.is_valid) # False
print("== INVALID_SPAN_CONTEXT:", invalid_span_context == trace.INVALID_SPAN_CONTEXT) # False
carrier = {}
ctx = trace.set_span_in_context(NonRecordingSpan(invalid_span_context))
propagator.inject(carrier, ctx)
print("Injected carrier:", carrier)
Expected Result
No traceparent or tracestate header should be injected when span_context.is_valid is False:
Injected carrier: {}
Actual Result
An illegal all-zero traceparent header is injected onto the carrier:
Injected carrier: {'traceparent': '00-00000000000000000000000000000000-0000000000000000-00'}
Additional context
The fix is straightforward. Replace the equality comparison against trace.INVALID_SPAN_CONTEXT with a check on the is_valid property:
diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py b/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py
--- a/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py
+++ b/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py
@@ -81,7 +81,7 @@ class TraceContextTextMapPropagator(textmap.TextMapPropagator):
"""
span = trace.get_current_span(context)
span_context = span.get_span_context()
- if span_context == trace.INVALID_SPAN_CONTEXT:
+ if not span_context.is_valid:
return
traceparent_string = f"00-{format_trace_id(span_context.trace_id)}-{format_span_id(span_context.span_id)}-{span_context.trace_flags:02x}"
setter.set(carrier, self._TRACEPARENT_HEADER_NAME, traceparent_string)
(Note: The same span_context == trace.INVALID_SPAN_CONTEXT pattern is also present in B3MultiFormat, B3SingleFormat, and JaegerPropagator.)
- Ngôn ngữ chính
- Python
- Star
- 2.7k
- Fork
- 1k
- Merge trung bình
- 4 ngày 15 giờ
- Pull request đã merge (30 ngày)
- 48
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của open-telemetry/opentelemetry-python
-
Prometheus exporter greedy regex in unit annotation stripping corrupts compound and ratio metric unitsCó thể làm lại được @samarth-git27 đã nhận 8 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
open-telemetry/opentelemetry-python#5766 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Delta exponential histogram never goes back to the maximum scale after a wide-range intervalCó thể đã có người làm @inchang-ing đã nhận 18 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
open-telemetry/opentelemetry-python#5716 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
open-telemetry/opentelemetry-python#5700 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Tests in test_metric_reader_storage.py assert on process-global mock state instead of on the storage under testCó thể đã có người làm @ocelotl đã nhận 40 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
open-telemetry/opentelemetry-python#5638 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
`SpanProcessor.on_end` exceptions escape cleanup and replace application exceptionsCó thể đã có người làm @somuai đã nhận 44 ngày trước. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
open-telemetry/opentelemetry-python#5624 ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của open-telemetry/opentelemetry-python
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Status: Triaged Type: Enhancement
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
canonical/craft-store#448 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
good first issue research size:s skill:numerics sub-issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
SKaiNET-developers/SKaiNET#1372 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoft/vscode-flake8#601 ·
Maintainer thường phản hồi trong vòng 1 ngày