Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing metrics not hosted via starlette #36

Closed
MatthewScholefield opened this issue Nov 29, 2021 · 4 comments
Closed

Exposing metrics not hosted via starlette #36

MatthewScholefield opened this issue Nov 29, 2021 · 4 comments

Comments

@MatthewScholefield
Copy link

MatthewScholefield commented Nov 29, 2021

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):

  1. Is this supported/possible?
  2. Would an example that uses start_http_server be accepted into the README?
  3. Would code that calls start_http_server handling lifespan hooks be accepted as a contribution?

cc @NargiT

@stephenhillier
Copy link
Owner

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! 👍

@intelroman
Copy link
Contributor

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 .

@intelroman
Copy link
Contributor

intelroman commented Feb 17, 2022

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

@stephenhillier
Copy link
Owner

stephenhillier commented Nov 11, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants