-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathactix-web.rs
74 lines (64 loc) · 2.09 KB
/
actix-web.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::sync::Mutex;
use actix_web::middleware::Compress;
use actix_web::{web, App, HttpResponse, HttpServer, Responder, Result};
use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelValue)]
pub enum Method {
Get,
Post,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
pub struct MethodLabels {
pub method: Method,
}
pub struct Metrics {
requests: Family<MethodLabels, Counter>,
}
impl Metrics {
pub fn inc_requests(&self, method: Method) {
self.requests.get_or_create(&MethodLabels { method }).inc();
}
}
pub struct AppState {
pub registry: Registry,
}
pub async fn metrics_handler(state: web::Data<Mutex<AppState>>) -> Result<HttpResponse> {
let state = state.lock().unwrap();
let mut body = String::new();
encode(&mut body, &state.registry).unwrap();
Ok(HttpResponse::Ok()
.content_type("application/openmetrics-text; version=1.0.0; charset=utf-8")
.body(body))
}
pub async fn some_handler(metrics: web::Data<Metrics>) -> impl Responder {
metrics.inc_requests(Method::Get);
"okay".to_string()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let metrics = web::Data::new(Metrics {
requests: Family::default(),
});
let mut state = AppState {
registry: Registry::default(),
};
state
.registry
.register("requests", "Count of requests", metrics.requests.clone());
let state = web::Data::new(Mutex::new(state));
HttpServer::new(move || {
App::new()
.wrap(Compress::default())
.app_data(metrics.clone())
.app_data(state.clone())
.service(web::resource("/metrics").route(web::get().to(metrics_handler)))
.service(web::resource("/handler").route(web::get().to(some_handler)))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}