Morning improvements

This commit is contained in:
Andras Schmelczer 2026-03-17 13:29:03 +00:00
parent 3e9fba5303
commit 53fff3efaa
41 changed files with 2438 additions and 637 deletions

View file

@ -4,12 +4,47 @@ use axum::http::StatusCode;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use metrics::{counter, gauge, histogram};
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle};
use std::time::Instant;
/// Initialize the Prometheus metrics exporter and return a handle for rendering metrics.
///
/// Configures histogram bucket boundaries so the exporter renders Prometheus histograms
/// (with `_bucket` suffix) instead of summaries. Without this, `histogram_quantile()`
/// queries in Grafana find no `_bucket` metrics and return empty.
pub fn init_metrics() -> PrometheusHandle {
// Standard Prometheus buckets for HTTP latencies (seconds)
const LATENCY_BUCKETS: &[f64] = &[
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
];
// Wider buckets for screenshot generation (can take 30s+)
const SCREENSHOT_BUCKETS: &[f64] = &[0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0, 60.0];
// Count-based buckets for response sizes (number of hexagons/postcodes returned)
const RESPONSE_COUNT_BUCKETS: &[f64] = &[
10.0, 50.0, 100.0, 250.0, 500.0, 1000.0, 2500.0, 5000.0, 10000.0,
];
PrometheusBuilder::new()
.set_buckets_for_metric(
Matcher::Full("http_request_duration_seconds".to_string()),
LATENCY_BUCKETS,
)
.expect("Failed to set HTTP latency buckets")
.set_buckets_for_metric(
Matcher::Full("screenshot_duration_seconds".to_string()),
SCREENSHOT_BUCKETS,
)
.expect("Failed to set screenshot duration buckets")
.set_buckets_for_metric(
Matcher::Full("hexagons_response_count".to_string()),
RESPONSE_COUNT_BUCKETS,
)
.expect("Failed to set hexagons response count buckets")
.set_buckets_for_metric(
Matcher::Full("postcodes_response_count".to_string()),
RESPONSE_COUNT_BUCKETS,
)
.expect("Failed to set postcodes response count buckets")
.install_recorder()
.expect("Failed to install Prometheus recorder")
}