stephenhillier / starlette_exporter

Prometheus exporter for Starlette and FastAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

group_paths not work for method `OPTIONS`

ZimmerHao opened this issue · comments

I think something wrong when request method is OPTIONS like below

starlette_requests_total{app_name="starlette",method="GET",path="/api/v1/datasets",status_code="200"} 92.0
starlette_requests_total{app_name="starlette",method="GET",path="/api/v1/tasks/nm",status_code="200"} 53.0
starlette_requests_total{app_name="starlette",method="GET",path="/api/v1/datasets/{did}",status_code="200"} 22.0
starlette_requests_total{app_name="starlette",method="GET",path="/api/v1/datasets/{did}/stats",status_code="200"} 7.0
starlette_requests_total{app_name="starlette",method="DELETE",path="/api/v1/datasets/{did}",status_code="200"} 4.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/567738610",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/2003501303",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/921436406",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/799719666",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/1602879743",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/150110457",status_code="200"} 1.0
starlette_requests_total{app_name="starlette",method="OPTIONS",path="/api/v1/datasets/1256292570",status_code="200"} 1.0

My version is starlette-exporter==0.11.0 and I add prometheus middleware

app.add_middleware(PrometheusMiddleware, group_paths=True)
app.add_route("/metrics", handle_metrics)

Is there any problem? Please take a look

Hi @ZimmerHao, thanks for creating an issue.

I added another test for OPTIONS methods to try to reproduce the issue, but it's passing https://github.com/stephenhillier/starlette_exporter/blob/master/tests/test_middleware.py#L349-L361. I tried setting up the OPTIONS endpoint these two ways (with just a basic test handler) and they both worked:

        @app.route("/200/{test_param}", methods=["GET", "OPTIONS"])
        def normal_response(request):
            return JSONResponse({"message": "Hello World"})


        mounted_routes = Mount("/", routes=[
            Route("/test/{item}", test_mounted_function_param, methods=["GET", "OPTIONS"]),
        ]

Is your OPTIONS endpoint set up or defined differently?

One more thing, I can reproduce a similar issue if OPTIONS isn't listed as an allowed method for any handlers using that path. In that case, the URLs do not get grouped (i.e. it looks like your metrics output), but the status codes say 405 Method Not Allowed not 200 OK. Using app.add_middleware(PrometheusMiddleware, filter_unhandled_paths=True, group_paths=True) helps with that, but I'm not sure that'll help you since you are seeing 200 status codes.

Closing due to inactivity - please feel free to reopen with a reproducible case if still an issue.