{"id":13705527,"url":"https://github.com/dkruchinin/sanic-prometheus","last_synced_at":"2025-05-05T16:33:00.816Z","repository":{"id":19247334,"uuid":"86519249","full_name":"dkruchinin/sanic-prometheus","owner":"dkruchinin","description":"Prometheus metrics for Sanic,  an async python web server","archived":false,"fork":false,"pushed_at":"2023-08-01T05:04:29.000Z","size":46,"stargazers_count":79,"open_issues_count":15,"forks_count":29,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-29T20:40:33.238Z","etag":null,"topics":["monitoring","prometheus","python","sanic"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dkruchinin.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-29T00:02:34.000Z","updated_at":"2025-01-29T13:04:23.000Z","dependencies_parsed_at":"2024-06-18T20:01:40.546Z","dependency_job_id":"246787e6-30b0-45f2-9a5d-709886eeeb52","html_url":"https://github.com/dkruchinin/sanic-prometheus","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkruchinin%2Fsanic-prometheus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkruchinin%2Fsanic-prometheus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkruchinin%2Fsanic-prometheus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dkruchinin%2Fsanic-prometheus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dkruchinin","download_url":"https://codeload.github.com/dkruchinin/sanic-prometheus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252534036,"owners_count":21763709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["monitoring","prometheus","python","sanic"],"created_at":"2024-08-02T22:00:42.994Z","updated_at":"2025-05-05T16:33:00.481Z","avatar_url":"https://github.com/dkruchinin.png","language":"Python","funding_links":[],"categories":["Extensions","Python"],"sub_categories":["Monitoring"],"readme":"Sanic prometheus metrics\n=========================\n|Build Status| |PyPI| |PyPI version|\n\nAfter googling for a while I didn't find a library that would enable some `prometheus \u003chttps://prometheus.io/\u003e`_ metrics for `Sanic \u003chttps://github.com/channelcat/sanic\u003e`_-based apps, so I had to write one. It makes adding monitoring to your Sanic app super easy, just add one line to your code (ok, two if you count import :) and point Prometheus to a newly appeared `/metrics` endpoint.\n\nVersions compatibility\n----------------------\n\n* ☑︎ use **\u003e= 0.1.0** for Sanic \u003c= 0.4.1\n* ☑︎ use **0.1.3** for Sanic \u003e= 0.5.0\n* ☑︎ use \u003e= **0.1.4** if you need multiprocessing support\n* ☑︎ use **0.1.6** if you have to use `promtheus-client` \u003c= 0.4.2\n* ☑︎ use **0.1.8** with `prometheus-client` \u003e= 0.5.0\n* ☑︎ use **0.2.0** with `prometheus-client` \u003e= 0.7.1 and Sanic \u003e= 18.12\n\nExposed metrics\n-----------------\n\nAt the moment ``sanic-prometheus`` provides four metrics:\n\n* **sanic_request_count** - total number of requests (labels: *method*, *endpoint*, *status*) [`counter \u003chttps://prometheus.io/docs/concepts/metric_types/#counter\u003e`_]\n* **sanic_request_latency_sec** - request latency in seconds (labels: *method*, *endpoint*) [`histogram \u003chttps://prometheus.io/docs/concepts/metric_types/#histogram\u003e`_]\n* **sanic_mem_rss_bytes** - resident memory used by the process (in bytes) [`gauge \u003chttps://prometheus.io/docs/concepts/metric_types/#gauge\u003e`_]\n* **sanic_mem_rss_perc** - a percent of total physical memory used by the process running Sanic [`gauge \u003chttps://prometheus.io/docs/concepts/metric_types/#gauge\u003e`_]\n\nLabels\n-----------------\n\n* **method**: a HTTP method (i.e. GET/POST/DELETE/etc)\n* **endpoint**: just a string, a name identifying a point handling a group of requests. By default it's just the first element of the relative path of the URL being called (i.e. for http://myhost/a/b/c you'll end up having ``/a`` as your endpoint). It is quite configurable, in fact it's up you what's gonna get to the ``endpoint`` label (see ``help(sanic_prometheus.monitor)`` for more details)\n* **http_status**: a HTTP status code\n\nEnabling monitoring\n-----------------\n\nEasy-peasy:\n\n.. code:: python\n\n  from sanic import Sanic\n  from sanic_prometheus import monitor\n\n  app = Sanic()\n  ...\n\n  if __name__ == \"__main__\":\n    monitor(app).expose_endpoint() # adds /metrics endpoint to your Sanic server\n    app.run(host=\"0.0.0.0\", port=8000)\n\n\nActually, there're two ways to run monitoring:\n\n\n1. The one you've seen above, ``monitor(app).expose_endpoint()``.\n   It just adds a new ``route`` to your Sanic app, exposing ``/metrics`` endpoint\n   on the same host and port your Sanic server runs. It might be useful if you run your\n   app in a container and you do not want to expose different ports for metrics and everything else.\n   You can customize the ``/metrics`` endpoint by passing the ``metrics_path`` keyword argument:\n   ``monitor(app, metrics_path='/my_metrics_path').expose_endpoint()``.\n2. ``monitor(app).start_server(addr=..., port=...)``.\n   Runs a HTTP server on given address and port and exposes ``/metrics`` endpoint on it.\n   This might be useful if you want to restrict access to your ``/metrics`` endpoint using some\n   firewall rules\n\n\nMultiprocess mode\n-----------------\n\nSanic allows to launch multiple worker processes to utilise parallelisation, which is great but makes metrics collection much trickier (`read more \u003chttps://github.com/prometheus/client_python/blob/master/README.md#multiprocess-mode-gunicorn\u003e`_) and introduces some limitations.\n\nIn order to collect metrics from multiple workers, create a directory and point a ``prometheus_multiproc_dir`` environment variable to it. Make sure the directory is empty before you launch your service::\n\n\n     % rm -rf /path/to/your/directory/*\n     % env prometheus_multiproc_dir=/path/to/your/directory python your_sanic_app.py\n\n\nUnfortunately you can not use ``monitor(app).start_server(addr=..., port=...)`` in multiprocess mode as it exposes a prometheus endpoint from a newly created process.\n\nConfiguration\n-----------------\n\nBest you can do is::\n\n     % ipython\n     In [1]: from sanic_prometheus import monitor\n     In [2]: help(monitor)\n\n\nPrometheus quering examples:\n-----------------------------\n\n* *Average latency over last 30 minutes*::\n\n    rate(sanic_request_latency_sec_sum{endpoint='/your-endpoint'}[30m]) /\n    rate(sanic_request_latency_sec_count{endpoint='/your-endpoint'}[30m])\n\n* *95th percentile of request latency*::\n\n    histogram_quantile(0.95, sum(rate(sanic_request_latency_sec_bucket[5m])) by (le))\n\n* *Physical memory usage percent over last 10 minutes*::\n\n    rate(sanic_mem_rss_perc[10m])\n\n.. |Build Status| image:: https://travis-ci.org/dkruchinin/sanic-prometheus.svg?branch=master\n   :target: https://travis-ci.org/dkruchinin/sanic-prometheus\n.. |PyPI| image:: https://img.shields.io/pypi/v/sanic-prometheus.svg\n   :target: https://pypi.python.org/pypi/sanic-prometheus/\n.. |PyPI version| image:: https://img.shields.io/pypi/pyversions/sanic-prometheus.svg\n   :target: https://pypi.python.org/pypi/sanic-prometheus/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkruchinin%2Fsanic-prometheus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdkruchinin%2Fsanic-prometheus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdkruchinin%2Fsanic-prometheus/lists"}