6 个版本
0.1.2 | 2022年9月29日 |
---|---|
0.1.2-beta.10 | 2021年10月21日 |
0.1.2-beta.8 | 2021年8月5日 |
#1278 in HTTP 服务器
6,484 个月下载量
用于 todors
38KB
332 行
Prometheus 仪表板对 actix-web 的集成。此中间件受到并分支自 actix-web-prom 的启发。默认跟踪三个指标(假设命名空间为 actix_web_prometheus
)
actix_web_prometheus_incoming_requests
(标签:端点、方法、状态):由 actix HttpServer 处理的 HTTP 请求总数。actix_web_prometheus_response_code
(标签:端点、方法、状态码、类型):由 actix HttpServer 处理的所有 HTTP 请求的响应代码。actix_web_prometheus_response_time
(标签:端点、方法、状态):由 actix HttpServer 处理的所有 HTTP 请求的总请求持续时间。
用法
首先将 actix-web-prom
添加到您的 Cargo.toml
[dependencies]
actix-web-prometheus = "0.1.0-beta.8"
然后实例化 Prometheus 中间件并将其传递给 .wrap()
use std::collections::HashMap;
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web_prometheus::{PrometheusMetrics, PrometheusMetricsBuilder};
fn health() -> HttpResponse {
HttpResponse::Ok().finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let mut labels = HashMap::new();
labels.insert("label1".to_string(), "value1".to_string());
let prometheus = PrometheusMetricsBuilder::new("api")
.endpoint("/metrics")
.const_labels(labels)
.build()
.unwrap();
HttpServer::new(move || {
App::new()
.wrap(prometheus.clone())
.service(web::resource("/health").to(health))
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
以上为例,有几点需要注意
api
是指标命名空间/metrics
将自动公开(仅支持 GET 请求),带有内容类型头content-type: text/plain; version=0.0.4; charset=utf-8
Some(labels)
用于向指标添加固定标签;如果不需要额外的标签,则可以传递None
。调用 /metrics 端点将公开您的指标
$ curl https://127.0.0.1:8080/metrics
# HELP actix_web_prometheus_incoming_requests Incoming Requests
# TYPE actix_web_prometheus_incoming_requests counter
actix_web_prometheus_incoming_requests{endpoint="/metrics",method="GET",status="200"} 23
# HELP actix_web_prometheus_response_code Response Codes
# TYPE actix_web_prometheus_response_code counter
actix_web_prometheus_response_code{endpoint="/metrics",method="GET",statuscode="200",type="200"} 23
# HELP actix_web_prometheus_response_time Response Times
# TYPE actix_web_prometheus_response_time histogram
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.005"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.01"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.025"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.05"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.1"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.25"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="0.5"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="1"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="2.5"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="5"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="10"} 23
actix_web_prometheus_response_time_bucket{endpoint="/metrics",method="GET",status="200",le="+Inf"} 23
actix_web_prometheus_response_time_sum{endpoint="/metrics",method="GET",status="200"} 0.00410981
actix_web_prometheus_response_time_count{endpoint="/metrics",method="GET",status="200"} 23
特性
如果您启用此crate的 process
特性,也将收集默认进程指标。 默认进程指标
# 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
自定义指标
您实例化 PrometheusMetrics
,然后使用其 .registry
来注册您的自定义指标(在这种情况下,我们使用一个 IntCounterVec
)。然后,您可以通过 .data()
将此计数器传递,使其在资源响应者中可用。
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web_prometheus::{PrometheusMetrics, PrometheusMetricsBuilder};
use prometheus::{opts, IntCounterVec};
fn health(counter: web::Data<IntCounterVec>) -> HttpResponse {
counter.with_label_values(&["endpoint", "method", "status"]).inc();
HttpResponse::Ok().finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let prometheus = PrometheusMetricsBuilder::new("api")
.endpoint("/metrics")
.build()
.unwrap();
let counter_opts = opts!("counter", "some random counter").namespace("api");
let counter = IntCounterVec::new(counter_opts, &["endpoint", "method", "status"]).unwrap();
prometheus
.registry
.register(Box::new(counter.clone()))
.unwrap();
HttpServer::new(move || {
App::new()
.wrap(prometheus.clone())
.app_data(web::Data::new(counter.clone()))
.service(web::resource("/health").to(health))
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
依赖项
~16-29MB
~535K SLoC