From 4c6c44a2ea1bf5e8c0fdb2a8c0a7be676a9096f0 Mon Sep 17 00:00:00 2001 From: Christopher Roddy <76755988+cjrkoa@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:06:23 -0400 Subject: [PATCH] prometheus metrics fixes #1094 in progress --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 1 + crates/ott-balancer-bin/Cargo.toml | 1 + crates/ott-balancer-bin/src/service.rs | 22 +++++++++++++++++++++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9fc05055e..fe63f9b29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1595,6 +1595,7 @@ dependencies = [ "ott-balancer-protocol", "ott-common", "pin-project", + "prometheus", "rand 0.8.5", "reqwest", "route-recognizer", @@ -1795,6 +1796,21 @@ dependencies = [ "yansi", ] +[[package]] +name = "prometheus" +version = "0.13.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c" +dependencies = [ + "cfg-if 1.0.0", + "fnv", + "lazy_static", + "memchr", + "parking_lot 0.12.1", + "protobuf", + "thiserror", +] + [[package]] name = "prost" version = "0.11.9" @@ -1827,6 +1843,12 @@ dependencies = [ "prost", ] +[[package]] +name = "protobuf" +version = "2.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94" + [[package]] name = "quick-error" version = "1.2.3" diff --git a/Cargo.toml b/Cargo.toml index 87eaba1f5..715b6e8d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ once_cell = "1.17.1" ott-common = { path = "crates/ott-common" } ott-balancer-protocol = { path = "crates/ott-balancer-protocol" } pin-project = "1.0.12" +prometheus = "0.13.3" rand = "0.8.5" reqwest = { version = "0.11.17", features = ["json", "stream", "rustls-tls"] } serde = { version = "1", features = ["derive", "rc"] } diff --git a/crates/ott-balancer-bin/Cargo.toml b/crates/ott-balancer-bin/Cargo.toml index b514c9c47..8456b4a2c 100644 --- a/crates/ott-balancer-bin/Cargo.toml +++ b/crates/ott-balancer-bin/Cargo.toml @@ -33,4 +33,5 @@ ott-balancer-protocol.workspace = true route-recognizer = "0.3.1" once_cell.workspace = true pin-project.workspace = true +prometheus.workspace = true trust-dns-resolver = { version = "0.22.0", features = ["system-config"] } diff --git a/crates/ott-balancer-bin/src/service.rs b/crates/ott-balancer-bin/src/service.rs index a6a6e5aec..abfc0f96f 100644 --- a/crates/ott-balancer-bin/src/service.rs +++ b/crates/ott-balancer-bin/src/service.rs @@ -15,6 +15,7 @@ use reqwest::Url; use route_recognizer::Router; use tokio::sync::RwLock; use tracing::{debug, error, info, warn}; +use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder}; use crate::balancer::{BalancerContext, BalancerLink}; use crate::client::client_entry; @@ -83,7 +84,26 @@ impl Service> for BalancerService { .join("\n"); mk_response(rendered) } - "metrics" => mk_response("TODO: prometheus metrics".to_owned()), + "metrics" => { + // Create a Counter. + let counter_opts = Opts::new("test_counter", "test counter help"); + let counter = Counter::with_opts(counter_opts).unwrap(); + + // Create a Registry and register Counter. + let r = Registry::new(); + r.register(Box::new(counter.clone())).unwrap(); + + // Inc. + counter.inc(); + + // Gather the metrics. + let mut buffer = vec![]; + let encoder = TextEncoder::new(); + let metric_families = r.gather(); + encoder.encode(&metric_families, &mut buffer).unwrap(); + + mk_response(String::from_utf8(buffer).unwrap().to_owned()) + }, "room" => { let Some(room_name) = route.params().find("room_name") else { return Ok(not_found());