stephenhillier / starlette_exporter

Prometheus exporter for Starlette and FastAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add custom metrics ?

rgarrigue opened this issue · comments

commented

HI again ! Last issue for the period I believe ;-)

Would you mind adding a quick doc about how to extend the metrics ? The FastAPI / starlette ones are a very good basis, but I'ld like to add some related to my app.

For example I have a Postgres database, I want to add the number of active subscription in the metrics, aka the result of a SELECT * FROM subscription WHERE active == 1 that would show up as starlette_subscription_active{app_name="starlette",query="SELECT * FROM subscription WHERE active == 1"} 1382

Custom metrics sounds like a good idea. It looks like https://github.com/rycus86/prometheus_flask_exporter provides a couple ways to add new counters with custom labels.

I'm not 100% sure I know how to tackle that specific example though. Would you put that query into a function and run it in a loop, updating a prometheus metric each time? Just from a quick google search I notice that there are projects like https://github.com/albertodonato/query-exporter that can run independently of your FastAPI/Starlette backend and report out on data from your database.

commented

Sorry for the late answer.

Indeed there are other exporter that would be a better fit for simple raw database queries results like my example. Thinking about it, I'm likely to use it to use it for some quick counters, until the apps developers implement their own exporters.

Your flask exporter example seems to be really good. I see everything I'm likely to use in there

  • A simple default setup like yours (see #Configuration)
  • A fine grain by request control though annotations
  • A way to add some static information metrics.info('app_info', 'Application info', version='1.0.3')
  • A way to add dynamic information like my number of active subscription example
info = metrics.info('app_subscription_count, 'Number of subscriptions')
...
info.set(app.get_subscription_count())

starlette_exporter will export all the prometheus metrics from the process, so it is just about creating/populating them using the prometheus_client API.

Starlette example, but same idea applies to FastAPI/anything as well:

from prometheus_client import Counter
from starlette.responses import RedirectResponse

REDIRECT_COUNT = Counter("redirect_total", "Count of redirects", ("from",))

async def some_view(request):
    REDIRECT_COUNT.labels(from="some_view").inc()
    return RedirectResponse(url="https://example.com", status_code=302)

Then if you hit /metrics:

...
redirect_total{from="some_view"} 2.0
...
commented

Interesting, thanks @rcoup. Would be nice to have this example in the README.md :-)

@rcoup thanks for the explanation. Do you mind if I add the example to the README?

Of course! Not sure it entirely describes the original question from the ticket, but it is a custom metric 😃