Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Pfennig <[email protected]>
  • Loading branch information
0xE282B0 committed Mar 14, 2024
1 parent 0ab7657 commit 21a6dc1
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions podtato-head/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,95 @@ helm upgrade --install podtato-head ./delivery/chart -f podtato-head-microservic
```
## Exercise 3: Add Metrics
- Metrics Endpoint
```sh
cargo add opentelemetry-prometheus opentelemetry_sdk opentelemetry once_cell prometheus
```
```rust
use std::time::SystemTime;
use std::sync::Arc;use once_cell::sync::Lazy;
use opentelemetry::{
metrics::{Counter, Histogram, MeterProvider as _, Unit},
KeyValue,
};
use opentelemetry_sdk::metrics::SdkMeterProvider;
use prometheus::{Encoder, Registry, TextEncoder};

static HANDLER_ALL: Lazy<[KeyValue; 1]> = Lazy::new(|| [KeyValue::new("handler", "all")]);

...

async fn serve_req(req: Request<impl hyper::body::Body>, state: Arc<AppState>) -> Result<Response<Full<Bytes>>, Infallible> {
let request_start = SystemTime::now();

state.http_counter.add(1, HANDLER_ALL.as_ref());

...
state.http_req_histogram.record(
request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()),
&[],
);
...
if req.method() == &Method::GET && req.uri().path() == "/metrics" {
let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_families = state.registry.gather();
encoder.encode(&metric_families, &mut buffer).unwrap();
state
.http_body_gauge
.record(buffer.len() as u64, HANDLER_ALL.as_ref());

// return Ok(Response::builder()
// .status(200)
// .header(CONTENT_TYPE, encoder.format_type())
// .body(Body::from(buffer)));
return Ok(Response::new(Full::new(Bytes::from(buffer))));
}

}
...

struct AppState {
registry: Registry,
http_counter: Counter<u64>,
http_body_gauge: Histogram<u64>,
http_req_histogram: Histogram<f64>,
}


... main

let registry = Registry::new();
let exporter = opentelemetry_prometheus::exporter()
.with_registry(registry.clone())
.build()?;
let provider = SdkMeterProvider::builder().with_reader(exporter).build();

let meter = provider.meter("hyper-example");
let state = Arc::new(AppState {
registry,
http_counter: meter
.u64_counter("http_requests_total")
.with_description("Total number of HTTP requests made.")
.init(),
http_body_gauge: meter
.u64_histogram("example.http_response_size")
.with_unit(Unit::new("By"))
.with_description("The metrics HTTP response sizes in bytes.")
.init(),
http_req_histogram: meter
.f64_histogram("example.http_request_duration")
.with_unit(Unit::new("ms"))
.with_description("The HTTP request latencies in milliseconds.")
.init(),
});

...

let state = state.clone();

.serve_connection(io, service_fn(move |req| serve_req(req, state.clone())))

```
## Exercise 4: Add Tracing
- https://github.com/open-telemetry/opentelemetry-rust/issues/1478
-
Expand Down

0 comments on commit 21a6dc1

Please sign in to comment.