Skip to content

Commit

Permalink
prometheus metrics
Browse files Browse the repository at this point in the history
fixes #1094
in progress
  • Loading branch information
cjrkoa committed Oct 19, 2023
1 parent cf1b5fc commit 4c6c44a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions crates/ott-balancer-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
22 changes: 21 additions & 1 deletion crates/ott-balancer-bin/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,7 +84,26 @@ impl Service<Request<IncomingBody>> 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());
Expand Down

0 comments on commit 4c6c44a

Please sign in to comment.