Skip to content

Commit

Permalink
move hyper prometheus example into something runnable (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu authored Jun 2, 2021
1 parent 1235682 commit fb576b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"examples/external-otlp-tonic-tokio",
"examples/grpc",
"examples/http",
"examples/hyper-prometheus",
"examples/tracing-grpc",
"examples/zipkin",
"examples/multiple-span-processors"
Expand Down
14 changes: 14 additions & 0 deletions examples/hyper-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "hyper-prometheus"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio"] }
opentelemetry-prometheus = { path = "../../opentelemetry-prometheus" }
prometheus = "0.12"
lazy_static = "1.4"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate lazy_static;
use hyper::{
header::CONTENT_TYPE,
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
Body, Method, Request, Response, Server,
};
use opentelemetry::{
global,
Expand All @@ -22,29 +22,41 @@ lazy_static! {
}

async fn serve_req(
_req: Request<Body>,
req: Request<Body>,
state: Arc<AppState>,
) -> Result<Response<Body>, hyper::Error> {
println!("Receiving request at path {}", req.uri());
let request_start = SystemTime::now();

let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_families = state.exporter.registry().gather();
encoder.encode(&metric_families, &mut buffer).unwrap();

state.http_counter.add(1);
state.http_body_gauge.record(buffer.len() as u64);

let response = Response::builder()
.status(200)
.header(CONTENT_TYPE, encoder.format_type())
.body(Body::from(buffer))
.unwrap();
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(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(request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()));

Ok(response)
}

Expand All @@ -69,7 +81,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.bind(HANDLER_ALL.as_ref()),
http_body_gauge: meter
.u64_value_recorder("example.http_response_size_bytes")
.with_description("The HTTP response sizes in bytes.")
.with_description("The metrics HTTP response sizes in bytes.")
.init()
.bind(HANDLER_ALL.as_ref()),
http_req_histogram: meter
Expand Down

0 comments on commit fb576b0

Please sign in to comment.