Exposing metrics not hosted via starlette
MatthewScholefield opened this issue · comments
Often in a production application one might want to hide the /metrics
endpoint from public traffic. Using the methods listed in the README, one would have to explicitly mask the /metrics
route within a reverse proxy and bind to two different ports which can be non-trivial (see encode/uvicorn#571 for context).
In my experience I've found it easier to just expose the metrics on a separate port (Ie. 9090) via Prometheus's default start_http_server
function but I'm not sure if this is supported by starlette-exporter. This way the metrics requests are served completely internally (and, for example, can only be exposed internally within a kubernetes cluster). While probably not necessary, to be clean I also integrated the server used in start_http_server
with starlette's lifespan events (otherwise I'm worried for example the socket won't unbind for some period of time when hot reloading).
My questions are (edit: updated questions):
- Is this supported/possible?
- Would an example that uses
start_http_server
be accepted into the README? - Would code that calls
start_http_server
handling lifespan hooks be accepted as a contribution?
cc @NargiT
Good question. The middleware isn't tied to any particular endpoint, so you could skip adding the /metrics
route and provide your own implementation as far as I know. The handle_metrics
handler function is just a way to expose prometheus_client.CollectorRegistry()
.
Any contributions welcome! 👍
I will try something on this but will be slow .. but I have an idea what can it be done .
Is not a promise but when I will try some stuff and come back if I have something successful .
Found a way .. but need to find a way to do the testing .
When I try to start the server on specific port on testing is not working.
See:
https://github.com/intelroman/starlette_exporter/blob/master/starlette_exporter/__init__.py
Worked for me in FastAPI
This issue is getting old and I'm doing some clean up. The metrics can be exposed any way that Prometheus supports, and is not limited by starlette_exporter
. Please reopen if there's more to discuss.