trallnag / prometheus-fastapi-instrumentator

Instrument your FastAPI with Prometheus metrics.
ISC License
948 stars 84 forks source link

FastAPI app with many routers #274

Closed bhaktatejas922 closed 11 months ago

bhaktatejas922 commented 11 months ago

Hey,

Trying to use this in a FastAPI app with many routers

app.include_router(router)
app.on_event("startup")(setup_caching)
app.on_event("shutdown")(Engine.teardown)
Instrumentator().instrument(app).expose(app, endpoint="/api/v1/metrics")

It only seems to get metrics at the root, are openapi.json and /docs and nothing else

satishsbhat commented 8 months ago

@bhaktatejas922 how did you resolve this ?

bhaktatejas922 commented 8 months ago

add it before you add the router

`

app = FastAPI()

origins = ["*"]

@app.get("/health")
def get_health():
    return {"status": "OK"}

app.add_middleware(
    CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
)

Instrumentator().instrument(app).expose(app, endpoint="/api/v1/metrics")
app.include_router(router)

`

satishsbhat commented 8 months ago

@bhaktatejas922 Thanks that works !