actix-web-prom
Prometheus instrumentation for actix-web. This middleware is heavily influenced by the work in sd2k/rocket_prometheus. We track the same default metrics and allow for adding user defined metrics.
By default two metrics are tracked (this assumes the namespace actix_web_prom):
-
actix_web_prom_http_requests_total(labels: endpoint, method, status): the total number of HTTP requests handled by the actix HttpServer. -
actix_web_prom_http_requests_duration_seconds(labels: endpoint, method, status): the request duration for all HTTP requests handled by the actix HttpServer.
Usage
First add actix-web-prom to your Cargo.toml:
[]
= "0.8.0"
You then instantiate the prometheus middleware and pass it to .wrap():
use HashMap;
use ;
use ;
async
async
Using the above as an example, a few things are worth mentioning:
apiis the metrics namespace/metricswill be auto exposed (GET requests only) with Content-Type headercontent-type: text/plain; version=0.0.4; charset=utf-8Some(labels)is used to add fixed labels to the metrics;Nonecan be passed instead if no additional labels are necessary.
A call to the /metrics endpoint will expose your metrics:
$ curl http://localhost:8080/metrics
# HELP api_http_requests_duration_seconds HTTP request duration in seconds for all requests
# TYPE api_http_requests_duration_seconds histogram
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.005"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.01"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.025"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.05"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.1"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.25"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="0.5"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="1"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="2.5"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="5"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="10"} 1
api_http_requests_duration_seconds_bucket{endpoint="/metrics",label1="value1",method="GET",status="200",le="+Inf"} 1
api_http_requests_duration_seconds_sum{endpoint="/metrics",label1="value1",method="GET",status="200"} 0.00003
api_http_requests_duration_seconds_count{endpoint="/metrics",label1="value1",method="GET",status="200"} 1
# HELP api_http_requests_total Total number of HTTP requests
# TYPE api_http_requests_total counter
api_http_requests_total{endpoint="/metrics",label1="value1",method="GET",status="200"} 1
Features
If you enable process feature of this crate, default process metrics will also be collected.
Default process metrics
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.22
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1048576
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 78
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 17526784
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1628105774.92
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1893163008
Custom metrics
You instantiate PrometheusMetrics and then use its .registry to register your custom
metric (in this case, we use a IntCounterVec).
Then you can pass this counter through .data() to have it available within the resource
responder.
use ;
use ;
use ;
async
async
Custom Registry
Some apps might have more than one actix_web::HttpServer.
If that's the case, you might want to use your own registry:
use ;
use ;
use System;
use Registry;
use thread;
async
async
Configurable routes pattern cardinality
Let's say you have on your app a route to fetch posts by language and by slug GET /posts/{language}/{slug}.
By default, actix-web-prom will provide metrics for the whole route with the label endpoint set to the pattern /posts/{language}/{slug}.
This is great but you cannot differentiate metrics across languages (as there is only a limited set of them).
Actix-web-prom can be configured to allow for more cardinality on some route params.
For that you need to add a middleware to pass some extensions data, specifically the MetricsConfig struct that contains the list of params you want to keep cardinality on.
use Service;
use HttpMessage;
use MetricsConfig;
resource
.wrap_fn
.route;
See the full example with_cardinality_on_params.rs.
Configurable metric names
If you want to rename the default metrics, you can use ActixMetricsConfiguration to do so.
use ;
new
.endpoint
.metrics_configuration
.build
.unwrap;
See full example configuring_default_metrics.rs.