use std::net::SocketAddr;
use abscissa_core::{Component, FrameworkError};
use serde::{Deserialize, Serialize};
#[derive(Debug, Component)]
pub struct MetricsEndpoint {}
impl MetricsEndpoint {
#[cfg(feature = "prometheus")]
pub fn new(config: &Config) -> Result<Self, FrameworkError> {
if let Some(addr) = config.endpoint_addr {
info!("Trying to open metrics endpoint at {}...", addr);
let endpoint_result = metrics_exporter_prometheus::PrometheusBuilder::new()
.with_http_listener(addr)
.install();
match endpoint_result {
Ok(()) => {
info!("Opened metrics endpoint at {}", addr);
metrics::counter!(
format!("{}.build.info", env!("CARGO_PKG_NAME")),
"version" => env!("CARGO_PKG_VERSION")
)
.increment(1);
}
Err(e) => panic!(
"Opening metrics endpoint listener {addr:?} failed: {e:?}. \
Hint: Check if another zebrad or zcashd process is running. \
Try changing the metrics endpoint_addr in the Zebra config.",
),
}
}
Ok(Self {})
}
#[cfg(not(feature = "prometheus"))]
pub fn new(config: &Config) -> Result<Self, FrameworkError> {
if let Some(addr) = config.endpoint_addr {
warn!(
?addr,
"unable to activate configured metrics endpoint: \
enable the 'prometheus' feature when compiling zebrad",
);
}
Ok(Self {})
}
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
pub endpoint_addr: Option<SocketAddr>,
}
#[allow(unknown_lints)]
#[allow(clippy::derivable_impls)]
impl Default for Config {
fn default() -> Self {
Self {
endpoint_addr: None,
}
}
}