getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.87k stars 489 forks source link

Sentry Integration Causing '431 Request Header Fields Too Large' Error with FastAPI and Azure API Management #3526

Closed Amirshox closed 2 weeks ago

Amirshox commented 2 weeks ago

I'm encountering an issue where enabling Sentry in my FastAPI application causes a 431 Request Header Fields Too Large error when requests are processed through Azure API Management (used for load balancing). The problem disappears when Sentry is disabled, indicating that Sentry might be adding additional data to the headers, causing them to exceed size limits.

Here is the relevant Sentry and Azure OpenAI integration code:

import sentry_sdk
from openai import AzureOpenAI

# Sentry initialization
sentry_sdk.init(
    dsn=SENTRY_DSN,
    environment=ENVIRONMENT,
    traces_sample_rate=0.5,
    profiles_sample_rate=0.5,
)

# Azure OpenAI client setup
client = AzureOpenAI(
    api_key=OPENAI_API_KEY,
    api_version=OPENAI_API_VERSION,
    azure_endpoint=OPENAI_URL,
)

try:
    response = client.chat.completions.create(
        model=model,
        messages=prompt,
        response_format={"type": "json_object"},
        temperature=temperature,
        max_tokens=max_tokens,
        tools=tools,
        tool_choice="auto"
    )
    output = response.choices[0].message.tool_calls[0].function.arguments
except Exception as e:
    print(f"Error: {e}")

Disabling Sentry resolves the issue, and the request is processed as expected.

antonpirker commented 2 weeks ago

Hey @Amirshox,

thanks for writting in. Sentry adds the sentry-trace and the baggage header to outgoing HTTP requests (for its distributed tracing).

Is the Request Header Fields Too Large coming from Azure or from the OpenAI server?

Can you check your logs if those headers are the problem? Or do you see in your Azure service somehow the incoming requests with its headers?

The SDK has an option trace_propagation_targets that can be used to prevent adding those headers to certain URLs: https://docs.sentry.io/platforms/python/configuration/options/#trace-propagation-targets

Amirshox commented 2 weeks ago

Hey @Amirshox,

thanks for writting in. Sentry adds the sentry-trace and the baggage header to outgoing HTTP requests (for its distributed tracing).

Is the Request Header Fields Too Large coming from Azure or from the OpenAI server?

Can you check your logs if those headers are the problem? Or do you see in your Azure service somehow the incoming requests with its headers?

The SDK has an option trace_propagation_targets that can be used to prevent adding those headers to certain URLs: https://docs.sentry.io/platforms/python/configuration/options/#trace-propagation-targets

Hey @antonpirker,

Thank you for pointing me in the right direction! It turns out the issue was indeed related to the sentry-trace and baggage headers added by Sentry.

The 431 Request Header Fields Too Large error was actually from the OpenAI server.

Using the trace_propagation_targets option from the Sentry SDK, I was able to prevent these headers from being added to requests sent to specific URLs. This resolved the 431 Request Header Fields Too Large error without needing to disable Sentry entirely.

szokeasaurusrex commented 2 weeks ago

Hey @Amirshox, sounds like using trace_propagation_targets solved your problem, so I am going to close this issue.

If I misunderstood, and you still need more support here, please reopen the issue!