Skip to content

Commit 26ff13d

Browse files
author
Nikita Tomchik
authored
Merge pull request #47 from Cdayz/feature/#8
Add use_prometheus_metrics function, to automaticaly enable metrics
2 parents 8862b0a + 4da6a46 commit 26ff13d

File tree

5 files changed

+277
-260
lines changed

5 files changed

+277
-260
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ $ pip install blacksheep-prometheus
2222

2323
## Usage
2424

25-
A complete example that exposes prometheus metrics endpoint under `/metrics/` path.
25+
A complete example that exposes prometheus metrics endpoint under default `/metrics/` endpoint.
2626

2727
```python
2828
from blacksheep.server import Application
29-
from blacksheep_prometheus import PrometheusMiddleware, metrics
29+
from blacksheep_prometheus import use_prometheus_metrics
3030

3131
app = Application()
32-
33-
app.middlewares.append(PrometheusMiddleware())
34-
app.router.add_get('/metrics/', metrics)
32+
use_prometheus_metrics(app)
3533
```
3634

3735
### Options

blacksheep_prometheus/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1+
from typing import Optional
2+
3+
from blacksheep.server import Application
4+
15
from .middleware import PrometheusMiddleware
26
from .view import metrics
37

8+
9+
def use_prometheus_metrics(
10+
app: Application,
11+
*,
12+
endpoint: str = "/metrics/",
13+
middleware: Optional[PrometheusMiddleware] = None,
14+
) -> None:
15+
"""
16+
Configures the given application to use Prometheus and provide services that can be
17+
injected in request handlers.
18+
"""
19+
middleware = middleware or PrometheusMiddleware()
20+
app.middlewares.append(middleware)
21+
app.router.add_get(endpoint, metrics)
22+
23+
424
__all__ = [
5-
'metrics',
25+
'use_prometheus_metrics',
626
'PrometheusMiddleware',
727
]

0 commit comments

Comments
 (0)