//! Prometheus metrics. //! //! Label sets are deliberately small. A metric keyed on repo id would give a //! registry serving a few hundred models a few hundred time series per //! counter, which is a cost paid forever for a question the manifests already //! answer. use metrics::{counter, gauge, histogram}; use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; use rustingface_core::Source; use rustingface_entities::error::{Error, Result}; /// Install the recorder and return the handle `/metrics` renders from. pub fn install() -> Result { PrometheusBuilder::new() .set_buckets(&[0.005, 0.025, 0.1, 0.5, 1.0, 5.0, 30.0, 120.0, 600.0, 3600.0]) .map_err(|e| Error::Config(format!("metrics buckets: {e}")))? .install_recorder() .map_err(|e| Error::Config(format!("metrics recorder: {e}"))) } /// Record a completed request. pub fn request(route: &'static str, status: u16) { counter!("rustingface_requests_total", "route" => route, "status" => status.to_string()) .increment(1); } /// Record where a served body came from. pub fn served(source: Source, bytes: u64) { let label = match source { Source::Bucket => "bucket", Source::Upstream => "upstream", }; counter!("rustingface_bytes_served_total", "source" => label).increment(bytes); } /// Record a blob response that ended before its `Content-Length`. /// /// The bytes leave through hyper rather than through a handler that can return /// a status, so a cut mid-body is otherwise invisible on this side: the client /// retries forever and the service logs a clean 200. pub fn truncated(source: Source, bytes: u64) { let label = match source { Source::Bucket => "bucket", Source::Upstream => "upstream", }; counter!("rustingface_blob_truncations_total", "source" => label).increment(1); counter!("rustingface_bytes_truncated_total", "source" => label).increment(bytes); } /// Record the outcome of an upstream fetch. pub fn upstream_fetch(result: &'static str, seconds: f64) { counter!("rustingface_upstream_fetches_total", "result" => result).increment(1); histogram!("rustingface_upstream_fetch_seconds").record(seconds); } /// Publish how many fetches are in flight. pub fn in_flight(count: usize) { gauge!("rustingface_fetches_in_flight").set(count as f64); }