opentelemetry-python
opentelemetry-python copied to clipboard
X-B3-Sampled: 0 alone does not disable the tracing
Describe your environment fastapi>=0.70.0 uvicorn[standard]>=0.15.0 opentelemetry-api==1.8.0 opentelemetry-sdk==1.8.0 opentelemetry-exporter-zipkin==1.8.0 opentelemetry-propagator-b3==1.8.0 opentelemetry-instrumentation-fastapi==0.27b0
Steps to reproduce Start server
from opentelemetry import trace
from opentelemetry.exporter.zipkin.json import ZipkinExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from fastapi import FastAPI
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.propagate import set_global_textmap
from opentelemetry.propagators.b3 import B3MultiFormat
import uvicorn
app = FastAPI()
tracer = trace.get_tracer(__name__)
zipkin_exporter = ZipkinExporter(
endpoint="http://127.0.0.1:9411/api/v2/spans",
)
span_processor = BatchSpanProcessor(zipkin_exporter)
trace_provider = TracerProvider(resource=Resource.create({"service.name": "open-telemetry-test"}))
trace_provider.add_span_processor(span_processor)
trace.set_tracer_provider(trace_provider)
set_global_textmap(B3MultiFormat())
@app.get('/hello')
def hello():
with tracer.start_as_current_span("hello"):
return {'hello': 'there'}
FastAPIInstrumentor.instrument_app(app,tracer_provider=trace_provider)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Test
import requests
requests.get("http://localhost:8000/hello", headers={'X-B3-Sampled': '0'})
What is the expected behavior? No trace log to Zipkin https://github.com/openzipkin/b3-propagation
What is the actual behavior? Trace still log to Zipkin
Additional context Zipkin docker image: ghcr.io/openzipkin/zipkin-slim:2.23
If has other B3 headers, X-B3-Sampled': '0' works
import requests
from opentelemetry.sdk.trace import id_generator
requests.get("http://localhost:8000/hello", headers={
'X-B3-TraceId': "{:032x}".format(id_generator.RandomIdGenerator().generate_trace_id()),
'X-B3-ParentSpanId': "{:016x}".format(id_generator.RandomIdGenerator().generate_span_id()),
'X-B3-SpanId': "{:016x}".format(id_generator.RandomIdGenerator().generate_span_id()),
'X-B3-Sampled': '0'
})
Thanks for the report. It looks like sending a sampling header alone is a common practice in Zipkin eco. This should be handled here. Feel free to send a fix if you can.
I have attempted to fix in here but not work. Not sure whats missing.
options = 0
# The b3 spec provides no defined behavior for both sample and
# flag values set. Since the setting of at least one implies
# the desire for some form of sampling, propagate if either
# header is set to allow.
if sampled in self._SAMPLE_PROPAGATE_VALUES or flags == "1":
options |= trace.TraceFlags.SAMPLED
if (
trace_id == trace.INVALID_TRACE_ID
or span_id == trace.INVALID_SPAN_ID
or self._trace_id_regex.fullmatch(trace_id) is None
or self._span_id_regex.fullmatch(span_id) is None
):
if sampled in self._SAMPLE_PROPAGATE_VALUES or flags == "1":
return context
else:
return trace.set_span_in_context(
trace.NonRecordingSpan(
trace.SpanContext(
# trace an span ids are encoded in hex, so must be converted
trace_id=trace_id,
span_id=span_id,
is_remote=True,
trace_flags=trace.TraceFlags(options),
trace_state=trace.TraceState(),
)
),
context,
)
You are trying to return an empty context because sampled=0 would not in _SAMPLE_PROPAGATE_VALUES={"1", "True", "true", "d"}. It should return context with invalid root span and default trace flag (0).
tried below 3 tests, seems need to have valid trace_id and span_id to get 'X-B3-Sampled': '0' alone to work
if (
trace_id == trace.INVALID_TRACE_ID
or span_id == trace.INVALID_SPAN_ID
or self._trace_id_regex.fullmatch(trace_id) is None
or self._span_id_regex.fullmatch(span_id) is None
):
if sampled in self._SAMPLE_PROPAGATE_VALUES or flags == "1":
return context
else:
return trace.set_span_in_context(
# **1: not work**
# trace.NonRecordingSpan(
# trace.SpanContext(
# trace_id=trace_id,
# span_id=span_id,
# is_remote=False,
# trace_flags=trace.TraceFlags(options),
# trace_state=trace.TraceState(),
# )
# ),
# **2: not work**
#trace.INVALID_SPAN,
# **3: works**
trace.NonRecordingSpan(
trace.SpanContext(
trace_id=self._id_generator.generate_trace_id(),
span_id=self._id_generator.generate_span_id(),
is_remote=False,
trace_flags=trace.TraceFlags(options),
trace_state=trace.TraceState(),
)
),
context,
)