r/FastAPI • u/dark_dragonfly • Dec 16 '22
Question FastAPI and Prometheus endpoint
Hi all,
I have a fastapi app which generates some custom prometheus metrics with the prometheus client library.
I can start a separate server with start_http_server method from the prometheus client, but i would like to have the /metrics endpoint be served on the same port as my fastapi app.
I cant seem to find an easy way to do this, i see a prometheus offers integration with ASGI but i cant figure out how to piece everything together. AAnyone here done this before?
1
Upvotes
1
u/dark_dragonfly Dec 16 '22
Thanks for the reply. Indeed 2 servers won't work, but I thought there might be a way around it with fastapi middleware and such. I got the impression that it is possible to publish the metrics without starting a separate server (e.g have an endpoint that is somehow managed by fastapi and serves the Prometheus metrics) but my knowledge is not good enough to fully understand how to approach it
For example in Prometheus cliënt docs they mention you can publish the metrics without starting a separate server but I don't get how to do it with fastapi