Package: applipy-prometheus
Applipy Prometheus Metricspip install applipy_prometheusExposes applipy metrics in prometheus format as an HTTP endpoint with path/metrics.UsageAdd theapplipy_prometheus.PrometheusModuleto your application. Optionally,
define through which http server to expose the/metricsendpoint, if no name
is given it defaults to the anonymous server:# dev.yamlapp:name:demomodules:-applipy_prometheus.PrometheusModulehttp.servers:-name:internalhost:0.0.0.0port:8080prometheus.server_name:internalTo run this test just installapplipy_prometheusandpyyamland run the applipy application:pipinstallapplipy_prometheuspyyaml
python-mapplipyYou can now queryhttp://0.0.0.0:8080/metricsand you should see some metrics for that endpoint (you'll have to query it
twice to see metrics).This module usesapplipy_metrics's registry to
load the metrics and generate the Prometheus document.Metrics Endpoint WrapperThis library also comes withMetricsWrapper. It is anapplipy_http.EndpointWrapperthat can be bound to your APIs and will automatically measure the request time
and store it as a summary with nameapplipy_web_request_duration_seconds.By default, the library will add theMetricsWrapperto the API that registers
the endpoint/metrics, namedprometheus, and the anonymous API, the one
that is bound without named parameters. This functionalities can be disabled by
setting the configuration valuesprometheus.observe_prometheus_apiandprometheus.observe_anonymous_apitofalse.A named API is one that is registered with named parameters like so:bind(with_names(Api, 'api_name')).The anonymous API has no named parameters and is usually registered like so:bind(Api).You can also tell the module to add theMetricsWrapperto you named APIs by
setting the configuration valueprometheus.api_namesto a list containing the
names of you named APIs.In the case your API is registered with multiple parameter names, the one
that applies for the wrapper is the name for thewrappersparameter.The wrapper haspriority100.The metrics are tagged by default with:method: HTTP request method (i.e.GET,POST, etc.)path: path of the endpoint handling the requestserver: name of the server handling the request (anonymous server is
empty string)status: status code of the responseOn top of that, a dictionary is added to theContextwith the keymetrics.tagswhere you can add custom tags to the metric.ExampleFull prometheus module configAll keys and their default values:prometheus:server_name:nullobserve_prometheus_api:trueobserve_anonymous_api:trueapi_names:[]Endpoint with custom metric tag# myendpoint.pyfromaiohttpimportwebfromapplipy_httpimportEndpointclassMyEndpoint(Endpoint):asyncdefget(self,req,ctx):ctx['metrics.tags']['custom_tag']='value'returnweb.Response(body='Ok')defpath(self):return'/'Usage with anonymous API# mymodule.pyfromapplipyimportModulefromapplipy_httpimportApi,HttpModule,Endpoint,PathFormatterfromapplipy_injectimportwith_namesfromapplipy_prometheusimportMetricsWrapperfrommyendpointimportMyEndpointclassMyModule(Module):defconfigure(self,bind,register):bind(Endpoint,MyEndpoint)bind(PathFormatter)bind(Api)@classmethoddefdepends_on(cls):returnHttpModule,# dev.yamlapp:name:testmodules:[mymodule.MyModule]http.servers:-host:0.0.0.0port:8080Usage with named API# mymodule.pyfromapplipyimportModulefromapplipy_httpimportApi,HttpModule,Endpoint,PathFormatterfromapplipy_injectimportwith_namesfromapplipy_prometheusimportMetricsWrapperfrommyendpointimportMyEndpointclassMyModule(Module):defconfigure(self,bind,register):bind(Endpoint,MyEndpoint,name='myApi')bind(PathFormatter,name='myApi')bind(with_names(Api,'myApi'))@classmethoddefdepends_on(cls):returnHttpModule,# dev.yamlapp:name:testmodules:[mymodule.MyModule]http.servers:-host:0.0.0.0port:8080prometheus:api_names:[myApi]