/
rustwizard
/
pg_exporter
Обзор
Документация
Войти
/
rustwizard
/
pg_exporter
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/app.rs
106 строк
3 KB
Rust Wizard
feat: add instance label to scrape_errors and scrape_duration metrics
15 май 2026, 15:44
Не верифицирован
15 май 2026, 15:44
edf4453
Код
Авторство
О чём код?
use std::sync::Arc; use prometheus::{CounterVec, GaugeVec, HistogramOpts, HistogramVec, Opts, Registry}; use crate::{collectors, instance}; pub const DEFAULT_SCRAPE_TIMEOUT_MS: u64 = 30_000; /// Custom histogram buckets suited for DB scrape durations (10 ms … 30 s). const SCRAPE_DURATION_BUCKETS: &[f64] = &[0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0]; #[derive(Clone)] pub struct CollectorEntry { pub name: String, pub instance: String, pub collector: Box<dyn collectors::PG>, } #[derive(Clone)] pub struct PGEApp { pub instances: Vec<Arc<instance::PostgresDB>>, pub collectors: Vec<CollectorEntry>, pub registry: Registry, pub scrape_timeout_ms: u64, /// How long each collector's update() took on the last scrape. pub scrape_duration: HistogramVec, /// Number of times a collector's update() returned an error. pub scrape_errors: CounterVec, /// Total connections in the pool (idle + in-use) per instance. pub pool_size: GaugeVec, /// Idle connections in the pool per instance. pub pool_idle: GaugeVec, } impl PGEApp { pub fn new() -> anyhow::Result<Self> { let registry = Registry::default(); let scrape_duration = HistogramVec::new( HistogramOpts::new( "pg_exporter_scrape_duration_seconds", "Duration in seconds of each collector's update() call on the last scrape.", ) .buckets(SCRAPE_DURATION_BUCKETS.to_vec()), &["collector", "instance"], )?; registry.register(Box::new(scrape_duration.clone()))?; let scrape_errors = CounterVec::new( Opts::new( "pg_exporter_scrape_errors_total", "Total number of errors returned by each collector's update() call.", ), &["collector", "instance"], )?; registry.register(Box::new(scrape_errors.clone()))?; let pool_size = GaugeVec::new( Opts::new( "pg_exporter_pool_size", "Total number of connections in the pool (idle + in-use) per instance.", ), &["instance"], )?; registry.register(Box::new(pool_size.clone()))?; let pool_idle = GaugeVec::new( Opts::new( "pg_exporter_pool_idle", "Number of idle connections in the pool per instance.", ), &["instance"], )?; registry.register(Box::new(pool_idle.clone()))?; Ok(Self { instances: Vec::new(), collectors: Vec::new(), registry, scrape_timeout_ms: DEFAULT_SCRAPE_TIMEOUT_MS, scrape_duration, scrape_errors, pool_size, pool_idle, }) } pub fn add_collector( &mut self, name: impl Into<String>, instance: impl Into<String>, col: Box<dyn collectors::PG>, ) { let name = name.into(); let instance = instance.into(); // Pre-initialize to zero so both metrics are always visible in /metrics // output even before the first scrape or the first error. self.scrape_duration.with_label_values(&[&name, &instance]); self.scrape_errors.with_label_values(&[&name, &instance]); self.collectors.push(CollectorEntry { name, instance, collector: col, }); } }