-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathmain.rs
129 lines (112 loc) · 3.86 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#[macro_use]
extern crate lazy_static;
use hyper::{
header::CONTENT_TYPE,
service::{make_service_fn, service_fn},
Body, Method, Request, Response, Server,
};
use opentelemetry::{
global,
metrics::{Counter, Histogram},
sdk::{
export::metrics::aggregation,
metrics::{controllers, processors, selectors},
},
Context, KeyValue,
};
use opentelemetry_prometheus::PrometheusExporter;
use prometheus::{Encoder, TextEncoder};
use std::convert::Infallible;
use std::sync::Arc;
use std::time::SystemTime;
lazy_static! {
static ref HANDLER_ALL: [KeyValue; 1] = [KeyValue::new("handler", "all")];
}
async fn serve_req(
cx: Context,
req: Request<Body>,
state: Arc<AppState>,
) -> Result<Response<Body>, hyper::Error> {
println!("Receiving request at path {}", req.uri());
let request_start = SystemTime::now();
state.http_counter.add(&cx, 1, &[]);
let response = match (req.method(), req.uri().path()) {
(&Method::GET, "/metrics") => {
let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_families = state.exporter.registry().gather();
encoder.encode(&metric_families, &mut buffer).unwrap();
state.http_body_gauge.record(&cx, buffer.len() as u64, &[]);
Response::builder()
.status(200)
.header(CONTENT_TYPE, encoder.format_type())
.body(Body::from(buffer))
.unwrap()
}
(&Method::GET, "/") => Response::builder()
.status(200)
.body(Body::from("Hello World"))
.unwrap(),
_ => Response::builder()
.status(404)
.body(Body::from("Missing Page"))
.unwrap(),
};
state.http_req_histogram.record(
&cx,
request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()),
&[],
);
Ok(response)
}
struct AppState {
exporter: PrometheusExporter,
http_counter: Counter<u64>,
http_body_gauge: Histogram<u64>,
http_req_histogram: Histogram<f64>,
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let controller = controllers::basic(processors::factory(
selectors::simple::histogram([1.0, 2.0, 5.0, 10.0, 20.0, 50.0]),
aggregation::cumulative_temporality_selector(),
))
.build();
let exporter = opentelemetry_prometheus::exporter(controller).init();
let cx = Context::new();
let meter = global::meter("ex.com/hyper");
let state = Arc::new(AppState {
exporter,
http_counter: meter
.u64_counter("example.http_requests_total")
.with_description("Total number of HTTP requests made.")
.init(),
http_body_gauge: meter
.u64_histogram("example.http_response_size_bytes")
.with_description("The metrics HTTP response sizes in bytes.")
.init(),
http_req_histogram: meter
.f64_histogram("example.http_request_duration_seconds")
.with_description("The HTTP request latencies in seconds.")
.init(),
});
// For every connection, we must make a `Service` to handle all
// incoming HTTP requests on said connection.
let make_svc = make_service_fn(move |_conn| {
let state = state.clone();
let cx = cx.clone();
// This is the `Service` that will handle the connection.
// `service_fn` is a helper to convert a function that
// returns a Response into a `Service`.
async move {
Ok::<_, Infallible>(service_fn(move |req| {
serve_req(cx.clone(), req, state.clone())
}))
}
});
let addr = ([127, 0, 0, 1], 3000).into();
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
server.await?;
Ok(())
}