Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spans not recorded by default if context is propagated #4430

Open
mzealey opened this issue Feb 14, 2025 · 1 comment
Open

Spans not recorded by default if context is propagated #4430

mzealey opened this issue Feb 14, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@mzealey
Copy link

mzealey commented Feb 14, 2025

Describe your environment

OS: Ubuntu 2204
Python version: 3.10.12
Package version: 1.3.0/0.51b0

What happened?

I originally raised this issue as open-telemetry/opentelemetry-python-contrib#3267 but now realise it seems to be a core bug rather than in a particular instrumentor.

The root of this issue appears to be that the propagator in https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py#L85-L87 returns a NonRecordingSpan if traceparent is set, however if it is not set a default _Span is created. https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py#L1160 then converts this into an _Span iff sampler is specified and returns that we should sample this trace.

The net result is some very confusing (and I think undocumented) behaviour: By default the root span is recording no matter whether a sampler is used or not. But if a valid traceparent header is seen it defaults to not recording unless there is a sampler set and it returns True (eg OTEL_TRACES_SAMPLER=always_on)

Steps to Reproduce

from fastapi import FastAPI
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

resource = Resource.create()
trace.set_tracer_provider(TracerProvider(resource=resource))
span_processor = BatchSpanProcessor(ConsoleSpanExporter())
trace.get_tracer_provider().add_span_processor(span_processor)

app = FastAPI()
FastAPIInstrumentor.instrument_app(app)


@app.get("/")
def root():
    return {"message": "Hello World"}

Run as fastapi dev v.py

Expected Result

Test with:

curl http://127.0.0.1:8000
curl http://127.0.0.1:8000 -H "traceparent: 00-94df63b03874c83da3cc8e207789df94-17fb4659f79f7ca2-00"

I would expect this to log to 2 traces to the console.

Actual Result

Only the first trace is logged, the one with traceparent is dropped.

Run again with OTEL_TRACES_SAMPLER=always_on fastapi dev v.py and repeat the curls and both traces appear on the console.

Additional context

open-telemetry/opentelemetry-python-contrib#3267

Would you like to implement a fix?

None

@mzealey mzealey added the bug Something isn't working label Feb 14, 2025
@mzealey
Copy link
Author

mzealey commented Feb 14, 2025

I should add this also happens with the flask implementation hence raising it centrally here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant