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

Incorrect http_requests_total with multiple Middleware #295

Open
rajeshdhanda opened this issue Mar 20, 2024 · 3 comments
Open

Incorrect http_requests_total with multiple Middleware #295

rajeshdhanda opened this issue Mar 20, 2024 · 3 comments

Comments

@rajeshdhanda
Copy link

Below is 2 different app along with code and metrics in Grafana.

APP 1

app = FastAPI()
app.middleware("http")(loggingMiddleware)
Instrumentator().instrument(app).expose(app)

Metrics : increase(http_requests_total{job="app-1-api"}[1m])

Screenshot 2024-03-20 at 12 48 24 PM

APP 2

app = FastAPI()
app.middleware("http")(loggingMiddleware)
Instrumentator().instrument(app).expose(app)

@app.middleware("http")
async def auth_middleware(request: Request, call_next):
    global auth_exclude_endpoints
    if request.method != "POST" :
        return await call_next(request)
    ClientAuth = request.headers.get("Authorization")
    auth_resp = await auth_validator(ClientAuth)
    if auth_resp.status_code == 200 :
        return await call_next(request)
    return auth_resp 

Metrics : increase(http_requests_total{job="app-2-api"}[1m])

Screenshot 2024-03-20 at 12 48 01 PM

Upon observation, it's noted that the http_requests_total metric consistently increases in App 2, whereas the traffic distribution to both apps seems similar in reality. Appreciate any insights or suggestions on what might be causing this discrepancy or if there's any missing configuration.

@shrinusivakumar
Copy link

@rajeshdhanda I'm experiencing the same issue. It seems that the issue is known. Check out the comment in main.py:

    # Starlette will call app.build_middleware_stack() with every new middleware
    # added, which will call all this again, which will make the registry
    # complain about duplicated metrics.
    #
    # The Python Prometheus client currently doesn't seem to have a way to
    # verify if adding a metric will cause errors or not, so the only way to
    # handle it seems to be with this try block.

If I'm reading this right, then it looks like each middleware will call to add each metric. Adding the first instance of the metrics should be fine. The subsequent calls from each middleware should raise an exception, but I think that exception is not happening in our case. Will continue to dig.

@IceRiverDev
Copy link

Hi guys, I also have a question about the http_requests_total. As i understanding, this metric is a counter and the value should always increase, but when i execute query http_requests_total in prometheus dashboard, the graph is like below: image

Is the graph normal or not? For my perception the line should always have an increasing trend instead of decreasing.

@Fulmo
Copy link

Fulmo commented Jun 26, 2024

Hi folks, does someone have solution for this? I'm observing the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants