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

Add use_prometheus_metrics function, to automaticaly enable metrics #47

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ $ pip install blacksheep-prometheus

## Usage

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

```python
from blacksheep.server import Application
from blacksheep_prometheus import PrometheusMiddleware, metrics
from blacksheep_prometheus import use_prometheus_metrics

app = Application()

app.middlewares.append(PrometheusMiddleware())
app.router.add_get('/metrics/', metrics)
use_prometheus_metrics(app)
```

### Options
Expand Down
22 changes: 21 additions & 1 deletion blacksheep_prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
from typing import Optional

from blacksheep.server import Application

from .middleware import PrometheusMiddleware
from .view import metrics


def use_prometheus_metrics(
app: Application,
*,
endpoint: str = "/metrics/",
middleware: Optional[PrometheusMiddleware] = None,
) -> None:
"""
Configures the given application to use Prometheus and provide services that can be
injected in request handlers.
"""
middleware = middleware or PrometheusMiddleware()
app.middlewares.append(middleware)
app.router.add_get(endpoint, metrics)


__all__ = [
'metrics',
'use_prometheus_metrics',
'PrometheusMiddleware',
]
Loading