stephenhillier / starlette_exporter

Prometheus exporter for Starlette and FastAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

filter_unhandled_paths broken with root_path set via Uvicorn

courtland opened this issue · comments

I noticed that setting the root_path via Uvicorn breaks all metrics having a path label when filter_unhandled_paths is enabled.

The FastAPI docs suggest that setting root_path via uvicorn is equivalent to the FastAPI attribute of the same name

This exporter however seems to make the assumption that it's set via FastAPI. I believe this is the problematic bit of code.

if hasattr(app, "root_path"):

In my case, I simply switched to setting root_path via the FastAPI attribute.

I'd be happy to submit a fix, but I'm not sure what the best way to inspect the uvicorn settings parameters in this context. Any tips would be appreciated.

Thanks!

I don't know of a way to inspect uvicorn parameters from middleware, my guess is that it's not feasible.

I wonder if the only option is to add another optional config param root_path e.g.:

app.add_middleware(
  PrometheusMiddleware,
  app_name="hello_world",
  root_path="/api",

I don't know of a way to inspect uvicorn parameters from middleware, my guess is that it's not feasible.

I wonder if the only option is to add another optional config param root_path e.g.:

app.add_middleware(
  PrometheusMiddleware,
  app_name="hello_world",
  root_path="/api",

Yeah... I like that idea. Unfortunately I've noticed some weird differences (in FastAPI) between setting it via uvicorn and the FastAPI app.

This is probably caused by issue in Starlette, fixed only in 0.33.0: encode/starlette#2352

But FastAPI does not support this version yet: tiangolo/fastapi#10798

Well, I've tried starlette==0.34.0, but without any success. It looks starlette-exporter itself does not handle root_path properly. See #81