Prometheus integration for Starlette.

perdy, updated 🕥 2022-12-08 13:21:24

Starlette Prometheus

Build Status codecov Package Version PyPI Version

Introduction

Prometheus integration for Starlette.

Requirements

  • Python 3.6+
  • Starlette 0.9+

Installation

console $ pip install starlette-prometheus

Usage

A complete example that exposes prometheus metrics endpoint under /metrics/ path.

```python from starlette.applications import Starlette from starlette_prometheus import metrics, PrometheusMiddleware

app = Starlette()

app.add_middleware(PrometheusMiddleware) app.add_route("/metrics/", metrics) ```

Metrics for paths that do not match any Starlette route can be filtered by passing filter_unhandled_paths=True argument to add_middleware method. Note that not turning on this filtering can lead to unbounded memory use when lots of different routes are called.

Contributing

This project is absolutely open to contributions so if you have a nice idea, create an issue to let the community discuss it.

Issues

Bump certifi from 2021.10.8 to 2022.12.7

opened on 2022-12-08 13:21:23 by dependabot[bot]

Bumps certifi from 2021.10.8 to 2022.12.7.

Commits


Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options
You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language - `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language - `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language - `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language You can disable automated security fix PRs for this repo from the [Security Alerts page](https://github.com/perdy/starlette-prometheus/network/alerts).

Setting requests in progress multiprocess mode as livesum

opened on 2022-09-19 13:20:01 by rbizos

By default it was by pid, this could generate a lot of metrics in case of many workers and multiprocess. This can overtime stack as worker processes can restart and leak metrics like this. With this change, this would remove the pid and the metric will be the same regardless of the multiprocess context

Revamp implementation without BaseHTTPMiddleware

opened on 2022-07-20 07:53:19 by frankie567

Hello there 👋

Because of the numerous limitations of the BaseHTTPMiddleware class provided by Starlette, the Starlette dev' team is about to deprecate it and encourage people to write "pure" ASGI middlewares. In particular, one of this limitation causes the issue #33 here.

This PR is an attempt at converting the existing middleware without BaseHTTPMiddleware. The resulting code is quite similar, the only "tricky" thing is the part where we wrap the send function with our own; as it's the common way to do in ASGI.

All existing tests are passing.

I would be glad to discuss it and make all changes needed so we can integrate this new approach in the library.

Cheers!

Submounted routes use incorrect path in labels

opened on 2022-06-24 11:23:56 by ter0

When submounting routes, rather than the full path template being used, only the mount prefix is used.

Running the following app: ``` from starlette.applications import Starlette from starlette.middleware import Middleware from starlette.responses import Response from starlette.routing import Mount, Route from starlette_prometheus import PrometheusMiddleware, metrics

async def foo(request): return Response()

async def bar_baz(request): return Response()

routes = [ Route("/foo", foo), Mount("/bar", Route("/baz", bar_baz)), Route("/metrics", metrics), ] middleware = [Middleware(PrometheusMiddleware)] app = Starlette(routes=routes, middleware=middleware) ```

Then making the following requests: $ curl localhost:8000/foo $ curl localhost:8000/bar/baz $ curl localhost:8000/metrics

Gives the following output (I only included one metric as an example, but it's the same for all of them). Note the label for the request to localhost:8000/bar/baz has a path label of /bar.

starlette_requests_total{method="GET",path_template="/foo"} 1.0 starlette_requests_total{method="GET",path_template="/bar"} 1.0 starlette_requests_total{method="GET",path_template="/metrics"} 1.0

respect PROMETHEUS_MULTIPROC_DIR in example metrics view

opened on 2022-04-23 09:54:01 by celloni

Hey

Since prometheus_client:0.10.x deprecated prometheus_multiproc_dir in favor of PROMETHEUS_MULTIPROC_DIR. So I updated the example metrics view to also respect PROMETHEUS_MULTIPROC_DIR - wdyt?

Show `metrics` endpoint documentation in FastAPI openapi.json

opened on 2022-03-01 20:58:40 by pozsa

Is there a way to show the endpoint in openapi.json when using the middleware with FastAPI?

Releases

v0.9.0 2022-01-10 15:26:57

v0.9.0 (2022-01-10)

✨ New Features

  • c801022 update prometheus-client to 0.12
  • c04164f Python 3.10 compatibility and upgrade requirements

🐛 Bug Fixes

  • 90837df UnboundLocalError: local variable 'status_code' referenced before assignment

v0.8.0 2021-08-31 11:41:43

v0.8.0 (2021-08-31)

✨ New Features

José Antonio Perdiguero

Artificial Intelligence Engineer & Software Architect

GitHub Repository

starlette prometheus metrics middleware asgi