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
2
u/[deleted] Dec 16 '22
You won't be able to start 2 servers/processes listening on the same port. First one to start, wins. The other will fail to bind as the port is already in use.
What you could do though is start both on different ports, say 8080 and 8081 or something, then put nginx in front of it all and proxy through to which ever backend based on the requested path. As a bonus you can terminate TLS at the nginx level using a single cert etc etc too.