init4_bin_base/utils/
metrics.rs1use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
2use metrics_exporter_prometheus::PrometheusBuilder;
3
4use super::from_env::EnvItemInfo;
5
6const METRICS_PORT: &str = "METRICS_PORT";
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct MetricsConfig {
17 pub port: u16,
20}
21
22impl Default for MetricsConfig {
23 fn default() -> Self {
24 Self { port: 9000 }
25 }
26}
27
28impl From<Option<u16>> for MetricsConfig {
29 fn from(port: Option<u16>) -> Self {
30 Self {
31 port: port.unwrap_or(9000),
32 }
33 }
34}
35
36impl From<u16> for MetricsConfig {
37 fn from(port: u16) -> Self {
38 Self { port }
39 }
40}
41
42impl FromEnv for MetricsConfig {
43 type Error = std::num::ParseIntError;
44
45 fn inventory() -> Vec<&'static EnvItemInfo> {
46 vec![&EnvItemInfo {
47 var: METRICS_PORT,
48 description: "Port on which to serve metrics, u16, defaults to 9000",
49 optional: true,
50 }]
51 }
52
53 fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
54 match u16::from_env_var(METRICS_PORT).map(Self::from) {
55 Ok(cfg) => Ok(cfg),
56 Err(_) => Ok(Self::default()),
57 }
58 }
59}
60
61pub fn init_metrics() {
74 let cfg = MetricsConfig::from_env().unwrap();
75
76 PrometheusBuilder::new()
77 .with_http_listener(([0, 0, 0, 0], cfg.port))
78 .install()
79 .expect("failed to install prometheus exporter");
80}