{"id":13705529,"url":"https://github.com/skar404/prometheus-sanic","last_synced_at":"2025-05-05T16:33:05.728Z","repository":{"id":47575201,"uuid":"218519183","full_name":"skar404/prometheus-sanic","owner":"skar404","description":"Actual forke for sanic-prometheus. Prometheus metrics for Sanic,  an async python web server","archived":false,"fork":true,"pushed_at":"2022-08-21T11:22:34.000Z","size":145,"stargazers_count":3,"open_issues_count":5,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-06T17:35:56.443Z","etag":null,"topics":["monitoring","pip","prometheus","python","python-library","sanic"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"dkruchinin/sanic-prometheus","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skar404.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-30T12:10:10.000Z","updated_at":"2022-12-14T13:03:57.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/skar404/prometheus-sanic","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skar404%2Fprometheus-sanic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skar404%2Fprometheus-sanic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skar404%2Fprometheus-sanic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skar404%2Fprometheus-sanic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skar404","download_url":"https://codeload.github.com/skar404/prometheus-sanic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224455886,"owners_count":17314200,"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","pip","prometheus","python","python-library","sanic"],"created_at":"2024-08-02T22:00:43.027Z","updated_at":"2024-11-13T13:30:37.431Z","avatar_url":"https://github.com/skar404.png","language":"Python","funding_links":[],"categories":["Extensions"],"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\nInstallation and enabling monitoring\n-------------------------------------\n\nInstallatio:\n\n.. code:: bash\n\n  pip install prometheus-sanic\n\nEasy-peasy:\n\n.. code:: python\n\n  from sanic import Sanic\n  from prometheus_sanic 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\nVersions compatibility\n----------------------\n\n* ☑︎ use **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 ``prometheus-sanic`` 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(prometheus_sanic.monitor)`` for more details)\n* **http_status**: a HTTP status code\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 prometheus_sanic 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://github.com/skar404/prometheus-sanic/workflows/Tests/badge.svg\n   :target: https://github.com/skar404/prometheus-sanic/actions/\n.. |PyPI| image:: https://img.shields.io/pypi/v/prometheus-sanic.svg\n   :target: https://pypi.python.org/pypi/prometheus-sanic/\n.. |PyPI version| image:: https://img.shields.io/pypi/pyversions/prometheus-sanic.svg\n   :target: https://pypi.python.org/pypi/prometheus-sanic/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskar404%2Fprometheus-sanic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskar404%2Fprometheus-sanic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskar404%2Fprometheus-sanic/lists"}