Skip to content

Commit

Permalink
Make the metrics configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
potkae committed Aug 9, 2023
1 parent 6a84851 commit 6e687ae
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ pub struct PrometheusMetricsBuilder {
exclude: HashSet<String>,
exclude_regex: RegexSet,
exclude_status: HashSet<StatusCode>,
metrics_configuration: ActixMetricsConfiguration,
}

impl PrometheusMetricsBuilder {
Expand All @@ -264,6 +265,7 @@ impl PrometheusMetricsBuilder {
exclude: HashSet::new(),
exclude_regex: RegexSet::empty(),
exclude_status: HashSet::new(),
metrics_configuration: ActixMetricsConfiguration::default(),
}
}

Expand Down Expand Up @@ -315,18 +317,24 @@ impl PrometheusMetricsBuilder {
self
}

/// Set metrics configuration
pub fn metrics_configuration(mut self, value: ActixMetricsConfiguration) -> Self {
self.metrics_configuration = value;
self
}

/// Instantiate PrometheusMetrics struct
pub fn build(self) -> Result<PrometheusMetrics, Box<dyn std::error::Error + Send + Sync>> {
let http_requests_total_opts =
Opts::new("http_requests_total", "Total number of HTTP requests")
Opts::new(self.metrics_configuration.http_requests_total.name.to_owned(), "Total number of HTTP requests")
.namespace(&self.namespace)
.const_labels(self.const_labels.clone());

let http_requests_total =
IntCounterVec::new(http_requests_total_opts, &["endpoint", "method", "status"])?;
IntCounterVec::new(http_requests_total_opts, &self.metrics_configuration.http_requests_total.labels.iter().map(|s| s.as_str()).collect::<Vec<&str>>())?;

let http_requests_duration_seconds_opts = HistogramOpts::new(
"http_requests_duration_seconds",
self.metrics_configuration.http_requests_duration_seconds.name.to_owned(),
"HTTP request duration in seconds for all requests",
)
.namespace(&self.namespace)
Expand All @@ -335,7 +343,7 @@ impl PrometheusMetricsBuilder {

let http_requests_duration_seconds = HistogramVec::new(
http_requests_duration_seconds_opts,
&["endpoint", "method", "status"],
&self.metrics_configuration.http_requests_duration_seconds.labels.iter().map(|s| s.as_str()).collect::<Vec<&str>>()
)?;

self.registry
Expand All @@ -357,6 +365,59 @@ impl PrometheusMetricsBuilder {
}
}

#[derive(Debug)]
/// Configuration for a single metric
///
/// Allows configuring name and labels set for the metric
pub struct ActixMetric {
name: String,
labels: Vec<String>,
}

impl ActixMetric {

/// Create a new metric configuration
pub fn new(name: &str, labels: Vec<&str>) -> ActixMetric{
ActixMetric {
name: name.to_string(),
labels: labels.into_iter().map(|s| s.to_string()).collect(),
}
}
}

#[derive(Debug)]
/// Configuration for the collected metrics
///
/// Stores individual metric configuration objects
pub struct ActixMetricsConfiguration {
http_requests_total: ActixMetric,
http_requests_duration_seconds: ActixMetric,
}

impl ActixMetricsConfiguration {

/// Create the default metrics configuration
fn default() -> ActixMetricsConfiguration {
ActixMetricsConfiguration {
http_requests_total: ActixMetric::new("http_requests_total", vec!["endpoint", "method", "status"]),
http_requests_duration_seconds: ActixMetric::new("http_requests_duration_seconds", vec!["endpoint", "method", "status"]),
}
}

/// Set configs for http_requests_total metric
pub fn http_requests_total(mut self, value: ActixMetric) -> Self {
self.http_requests_total = value;
self
}

/// Set configs for http_requests_duration_seconds metric
pub fn http_requests_duration_seconds(mut self, value: ActixMetric) -> Self {
self.http_requests_duration_seconds = value;
self
}

}

#[derive(Clone)]
#[must_use = "must be set up as middleware for actix-web"]
/// By default two metrics are tracked (this assumes the namespace `actix_web_prom`):
Expand Down

0 comments on commit 6e687ae

Please sign in to comment.