Skip to content

Sentry not logging traces for FastAPI's StreamingResponse endpoint #5002

@Macok

Description

@Macok

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

I initialised sentry_sdk with

sentry_sdk.init(
        dsn=os.environ['SENTRY_DSN'],
        environment=os.getenv("SENTRY_ENV", "production"),
        send_default_pii=True,
        traces_sample_rate=1.0,
    )

In the Trace Explorer, I can see traces from all endpoints of my app, but not below one:

@app.post("/generate_summary")
def generate_summary(params: GenerateSummaryParams,
                     user: str = Depends(get_current_user)):
    conversation = fetch_conversation(user, params.conversation_id)
    return StreamingResponse(do_generate_summary(conversation, params.doc_ids, user),
                             media_type="text/event-stream")

I tried to manually add transaction inside do_generate_summary() generator like below:

    with sentry_sdk.start_transaction(name="my_transaction"):
        with sentry_sdk.start_span(name="my_span"):
            ...

With above code, something even more strange happens. Now, I can see transactions created by sentry_sdk for my /generate_summary endpoint, but my_transaction transaction and my_span span are missing!

Expected Result

Traces for FastAPI endpoints returning a StreamingResponse should be sent to Sentry.

Actual Result

Traces for FastAPI endpoints returning a StreamingResponse are not being sent to Sentry.

Version

sentry-sdk==2.42.1
fastapi==0.119.1
Python 3.12.12

Metadata

Metadata

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions