|
13 | 13 |
|
14 | 14 | </div> |
15 | 15 |
|
| 16 | +## asgi-monitor |
| 17 | +### A library for easy and fast configuration of logging, tracing and metrics for ASGI applications. |
16 | 18 |
|
17 | | -### A library for easy and fast configuration of logging, tracing and monitoring of ASGI applications. |
| 19 | +### Purpose |
| 20 | + |
| 21 | +Quickly add minimal features for flexible application monitoring. |
| 22 | + |
| 23 | +Features: |
| 24 | + - [Prometheus](https://prometheus.io) metrics |
| 25 | + - [OpenTelemetry](https://opentelemetry.io) traces |
| 26 | + - [Structlog](https://www.structlog.org/) logging with native **logging** module support |
| 27 | + - Integrations with [FastAPI](https://fastapi.tiangolo.com) and [Starlette](https://www.starlette.io) |
| 28 | + - Logging support for [Uvicorn](https://www.uvicorn.org) and [Gunicorn](https://gunicorn.org) with custom **UvicornWorker** |
| 29 | + |
| 30 | +### Installation |
| 31 | + |
| 32 | +```shell |
| 33 | +pip install asgi-monitor |
| 34 | +``` |
| 35 | + |
| 36 | +### Quickstart |
| 37 | + |
| 38 | +#### Logging and metrics |
| 39 | + |
| 40 | +```python |
| 41 | +import logging |
| 42 | + |
| 43 | +from asgi_monitor.integrations.fastapi import setup_metrics |
| 44 | +from asgi_monitor.logging import configure_logging |
| 45 | +from asgi_monitor.logging.uvicorn import build_uvicorn_log_config |
| 46 | +from fastapi import FastAPI |
| 47 | +from uvicorn import run |
| 48 | + |
| 49 | +logger = logging.getLogger(__name__) |
| 50 | +app = FastAPI(debug=True) |
| 51 | + |
| 52 | + |
| 53 | +def run_app() -> None: |
| 54 | + log_config = build_uvicorn_log_config(level=logging.INFO, json_format=True, include_trace=False) |
| 55 | + |
| 56 | + configure_logging(level=logging.INFO, json_format=True, include_trace=False) |
| 57 | + setup_metrics(app, app_name="fastapi", include_metrics_endpoint=True, include_trace_exemplar=False) |
| 58 | + |
| 59 | + logger.info("App is ready to start") |
| 60 | + |
| 61 | + run(app, host="127.0.0.1", port=8000, log_config=log_config) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + run_app() |
| 66 | +``` |
| 67 | + |
| 68 | +In this example, all logs will be presented in JSON format and the following metrics will be set for the application: |
| 69 | +1. `fastapi_app_info` - ASGI application information |
| 70 | +2. `fastapi_requests_total` - Total count of requests by method and path |
| 71 | +3. `fastapi_responses_total` - Total count of responses by method, path and status codes |
| 72 | +4. `fastapi_request_duration_seconds` - Histogram of request duration by path, in seconds |
| 73 | +5. `fastapi_requests_in_progress` - Gauge of requests by method and path currently being processed |
| 74 | +6. `fastapi_requests_exceptions_total` - Total count of exceptions raised by path and exception type |
| 75 | + |
| 76 | +And these metrics are available by endpoint `/metrics`, |
| 77 | +but you can import `get_latest_metrics` from `asgi_monitor.metrics` to create a custom endpoint. |
| 78 | + |
| 79 | +> **Warning** |
| 80 | +> |
| 81 | +> If you are using **Gunicorn**, then you need to set the environment variable **"PROMETHEUS_MULTIPROC_DIR"** |
| 82 | +> with the path to the directory where the consistent metrics will be stored. |
| 83 | +> |
| 84 | +> This approach will **block** the _event loop_ when recording metrics! |
| 85 | +
|
| 86 | +See the `prometheus_client` [documentation](https://prometheus.github.io/client_python/) for adding your custom metrics. |
| 87 | + |
| 88 | +#### Tracing |
| 89 | + |
| 90 | +You can also add query tracing and your logic using `opentelemetry`. |
| 91 | + |
| 92 | +Create your **TracerProvider** with the necessary settings and add it to the **TracingConfig**. |
| 93 | + |
| 94 | +See [example](https://github.com/draincoder/asgi-monitor/blob/develop/examples/real_world/app/main.py) |
| 95 | +to understand the tracing setup.\ |
| 96 | +This example also contains all the infrastructure configs to evaluate the capabilities of the application, |
| 97 | +carefully study them and customize them to your needs. |
| 98 | + |
| 99 | +### API Metrics dashboard |
| 100 | + |
| 101 | + |
| 102 | +### Tempo traces from dashboard logs |
| 103 | + |
| 104 | + |
| 105 | +> **Warning** |
| 106 | +> |
| 107 | +> Do not use these configs in production, as authorization and long-term data storage are not configured there! |
| 108 | +> |
0 commit comments