File: README.md

package info (click to toggle)
prometheus-fastapi-instrumentator 7.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 492 kB
  • sloc: python: 2,586; makefile: 5
file content (61 lines) | stat: -rw-r--r-- 1,819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Example `prom-multi-proc-gunicorn`

Minimal example that shows integration of FastAPI (Gunicorn) with the Prometheus
client library in multi process mode without Prometheus FastAPI Instrumentator.
Highlights missing metrics that are not supported in multi process mode.

To run the example, you must have run `poetry install` and `poetry shell` in the
root of this repository. The following commands are executed relative to this
directory.

Set environment variable to an unused location:

```shell
export PROMETHEUS_MULTIPROC_DIR=/tmp/python-testing-pfi/560223ba-887f-429a-9c48-933df56a68ba
```

Start the app with Gunicorn using two Uvicorn workers:

```shell
rm -rf "$PROMETHEUS_MULTIPROC_DIR"
mkdir -p "$PROMETHEUS_MULTIPROC_DIR"
gunicorn main:app \
  --config gunicorn.conf.py \
  --workers 2 \
  --worker-class uvicorn.workers.UvicornWorker \
  --bind 0.0.0.0:8080
```

Interact with app:

```shell
for i in {1..5}; do curl localhost:8080/ping; done
curl localhost:8080/metrics
```

You should see something like this:

```txt
# TYPE ping_total counter
ping_total 5.0
# HELP metrics_total Number of metrics calls.
# TYPE metrics_total counter
metrics_total 1.0
# HELP main_total Counts of main executions.
# TYPE main_total counter
main_total 2.0
```

Check the returned metrics:

- `main_total` is `2`, because Gunicorn is using two workers.
- There are no `created_by` metrics. These are not supported by the Prometheus
  client library in multi process mode.
- No metrics for things like CPU and memory. They come from components like the
  `ProcessCollector` and `PlatformCollector` which are not supported by the
  Prometheus client library in multi process mode.

Links:

- <https://github.com/prometheus/client_python#multiprocess-mode-eg-gunicorn>
- <https://docs.gunicorn.org/en/stable/settings.html>