Package: prometheus-sanic
After googling for a while I didn’t find a library that would enable someprometheusmetrics forSanic-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/metricsendpoint.Installation and enabling monitoringInstallatio:pipinstallprometheus-sanicEasy-peasy:fromsanicimportSanicfromprometheus_sanicimportmonitorapp=Sanic()...if__name__=="__main__":monitor(app).expose_endpoint()# adds /metrics endpoint to your Sanic serverapp.run(host="0.0.0.0",port=8000)Actually, there’re two ways to run monitoring:The one you’ve seen above,monitor(app).expose_endpoint().
It just adds a newrouteto your Sanic app, exposing/metricsendpoint
on the same host and port your Sanic server runs. It might be useful if you run your
app in a container and you do not want to expose different ports for metrics and everything else.
You can customize the/metricsendpoint by passing themetrics_pathkeyword argument:monitor(app,metrics_path='/my_metrics_path').expose_endpoint().monitor(app).start_server(addr=...,port=...).
Runs a HTTP server on given address and port and exposes/metricsendpoint on it.
This might be useful if you want to restrict access to your/metricsendpoint using some
firewall rulesVersions compatibility☑︎ use0.1.0for Sanic <= 0.4.1☑︎ use0.1.3for Sanic >= 0.5.0☑︎ use >=0.1.4if you need multiprocessing support☑︎ use0.1.6if you have to usepromtheus-client<= 0.4.2☑︎ use0.1.8withprometheus-client>= 0.5.0☑︎ use0.2.0withprometheus-client>= 0.7.1 and Sanic >= 18.12Exposed metricsAt the momentprometheus-sanicprovides four metrics:sanic_request_count- total number of requests (labels:method,endpoint,status) [counter]sanic_request_latency_sec- request latency in seconds (labels:method,endpoint) [histogram]sanic_mem_rss_bytes- resident memory used by the process (in bytes) [gauge]sanic_mem_rss_perc- a percent of total physical memory used by the process running Sanic [gauge]Labelsmethod: a HTTP method (i.e. GET/POST/DELETE/etc)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. forhttp://myhost/a/b/cyou’ll end up having/aas your endpoint). It is quite configurable, in fact it’s up you what’s gonna get to theendpointlabel (seehelp(prometheus_sanic.monitor)for more details)http_status: a HTTP status codeMultiprocess modeSanic allows to launch multiple worker processes to utilise parallelisation, which is great but makes metrics collection much trickier (read more) and introduces some limitations.In order to collect metrics from multiple workers, create a directory and point aprometheus_multiproc_direnvironment variable to it. Make sure the directory is empty before you launch your service:% rm -rf /path/to/your/directory/*
% env prometheus_multiproc_dir=/path/to/your/directory python your_sanic_app.pyUnfortunately you can not usemonitor(app).start_server(addr=...,port=...)in multiprocess mode as it exposes a prometheus endpoint from a newly created process.ConfigurationBest you can do is:% ipython
In [1]: from prometheus_sanic import monitor
In [2]: help(monitor)Prometheus quering examples:Average latency over last 30 minutes:rate(sanic_request_latency_sec_sum{endpoint='/your-endpoint'}[30m]) /
rate(sanic_request_latency_sec_count{endpoint='/your-endpoint'}[30m])95th percentile of request latency:histogram_quantile(0.95, sum(rate(sanic_request_latency_sec_bucket[5m])) by (le))Physical memory usage percent over last 10 minutes:rate(sanic_mem_rss_perc[10m])