From fb576b0e71e2c514ad1616d9d4932fbd35703bef Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Wed, 2 Jun 2021 19:47:31 +0200 Subject: [PATCH] move hyper prometheus example into something runnable (#562) --- Cargo.toml | 1 + examples/hyper-prometheus/Cargo.toml | 14 +++++++ .../hyper-prometheus/src/main.rs | 42 ++++++++++++------- 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 examples/hyper-prometheus/Cargo.toml rename opentelemetry-prometheus/examples/hyper.rs => examples/hyper-prometheus/src/main.rs (70%) diff --git a/Cargo.toml b/Cargo.toml index 5ecb7cf3e3..c8d041a8bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/hyper-prometheus/Cargo.toml b/examples/hyper-prometheus/Cargo.toml new file mode 100644 index 0000000000..c33fb3fee1 --- /dev/null +++ b/examples/hyper-prometheus/Cargo.toml @@ -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"] } diff --git a/opentelemetry-prometheus/examples/hyper.rs b/examples/hyper-prometheus/src/main.rs similarity index 70% rename from opentelemetry-prometheus/examples/hyper.rs rename to examples/hyper-prometheus/src/main.rs index 798351a77a..2ab79c58e2 100644 --- a/opentelemetry-prometheus/examples/hyper.rs +++ b/examples/hyper-prometheus/src/main.rs @@ -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, @@ -22,29 +22,41 @@ lazy_static! { } async fn serve_req( - _req: Request, + req: Request, state: Arc, ) -> Result, 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) } @@ -69,7 +81,7 @@ pub async fn main() -> Result<(), Box> { .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