Skip to content

Commit

Permalink
chore: add metrics labels (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev authored Nov 5, 2024
1 parent 4b820c7 commit 6041b28
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub struct Metrics {
requests_total: Counter<u64>,
requests_in_flight: UpDownCounter<i64>,
request_duration: Histogram<f64>,
prom_requests_total: prometheus::Counter,
prom_requests_in_flight: prometheus::Gauge,
prom_request_duration: prometheus::Histogram,
prom_requests_total: prometheus::IntCounterVec,
prom_requests_in_flight: prometheus::IntGaugeVec,
prom_request_duration: prometheus::HistogramVec,
registry: prometheus::Registry,
}

Expand All @@ -30,18 +30,29 @@ impl Metrics {
let meter = opentelemetry::global::meter("http_server");
let registry = prometheus::Registry::new();

// Create Prometheus metrics
let requests_total = prometheus::Counter::new("http_requests_total", "Total number of HTTP requests").unwrap();
let requests_in_flight = prometheus::Gauge::new("http_requests_in_flight", "Number of HTTP requests currently in flight").unwrap();
let request_duration = prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
"http_request_duration_seconds",
"HTTP request duration in seconds",
)).unwrap();
// Create Prometheus metrics with labels
let prom_requests_total = prometheus::IntCounterVec::new(
prometheus::Opts::new("http_requests_total", "Total number of HTTP requests"),
&["method"]
).unwrap();

let prom_requests_in_flight = prometheus::IntGaugeVec::new(
prometheus::Opts::new("http_requests_in_flight", "Number of HTTP requests currently in flight"),
&["method"]
).unwrap();

let prom_request_duration = prometheus::HistogramVec::new(
prometheus::HistogramOpts::new(
"http_request_duration_seconds",
"HTTP request duration in seconds",
),
&["method", "status"]
).unwrap();

// Register metrics with Prometheus
registry.register(Box::new(requests_total.clone())).unwrap();
registry.register(Box::new(requests_in_flight.clone())).unwrap();
registry.register(Box::new(request_duration.clone())).unwrap();
registry.register(Box::new(prom_requests_total.clone())).unwrap();
registry.register(Box::new(prom_requests_in_flight.clone())).unwrap();
registry.register(Box::new(prom_request_duration.clone())).unwrap();

// Create OpenTelemetry metrics
let otel_requests_total = meter
Expand All @@ -63,9 +74,9 @@ impl Metrics {
requests_total: otel_requests_total,
requests_in_flight: otel_requests_in_flight,
request_duration: otel_request_duration,
prom_requests_total: requests_total,
prom_requests_in_flight: requests_in_flight,
prom_request_duration: request_duration,
prom_requests_total,
prom_requests_in_flight,
prom_request_duration,
registry,
}
}
Expand All @@ -74,8 +85,9 @@ impl Metrics {
let attributes = &[KeyValue::new("method", method.to_string())];
self.requests_total.add(1, attributes);
self.requests_in_flight.add(1, attributes);
self.prom_requests_total.inc();
self.prom_requests_in_flight.inc();
// Update Prometheus metrics with labels
self.prom_requests_total.with_label_values(&[method]).inc();
self.prom_requests_in_flight.with_label_values(&[method]).inc();
}

pub fn record_response(&self, method: &str, status: u16, start: std::time::Instant) {
Expand All @@ -86,8 +98,9 @@ impl Metrics {
let duration = start.elapsed().as_secs_f64();
self.request_duration.record(duration, attributes);
self.requests_in_flight.add(-1, attributes);
self.prom_request_duration.observe(duration);
self.prom_requests_in_flight.dec();
// Update Prometheus metrics with labels
self.prom_request_duration.with_label_values(&[method, &status.to_string()]).observe(duration);
self.prom_requests_in_flight.with_label_values(&[method]).dec();
}
}

Expand Down

0 comments on commit 6041b28

Please sign in to comment.