Skip to content

Commit

Permalink
graph: Fix error in how StopwatchMetrics are created
Browse files Browse the repository at this point in the history
Since we now call StopwatchMetrics::new twice for each deployment, we need
to go through the global_ .. mechanisms in the registry. Otherwise, only
one set of metrics gets actually kept.
  • Loading branch information
lutter committed Apr 12, 2022
1 parent cfc3869 commit 764f5c1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
52 changes: 38 additions & 14 deletions core/src/metrics/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,33 @@ impl MetricsRegistry {
.expect("failed to register `registered_metrics` gauge");
gauge
}

fn global_counter_vec_internal(
&self,
name: &str,
help: &str,
deployment: Option<&str>,
variable_labels: &[&str],
) -> Result<CounterVec, PrometheusError> {
let opts = Opts::new(name, help);
let opts = match deployment {
None => opts,
Some(deployment) => opts.const_label("deployment", deployment),
};
let counters = CounterVec::new(opts, variable_labels)?;
let id = counters.desc().first().unwrap().id;
let maybe_counter = self.global_counter_vecs.read().unwrap().get(&id).cloned();
if let Some(counters) = maybe_counter {
Ok(counters)
} else {
self.register(name, Box::new(counters.clone()));
self.global_counter_vecs
.write()
.unwrap()
.insert(id, counters.clone());
Ok(counters)
}
}
}

impl MetricsRegistryTrait for MetricsRegistry {
Expand Down Expand Up @@ -160,20 +187,17 @@ impl MetricsRegistryTrait for MetricsRegistry {
help: &str,
variable_labels: &[&str],
) -> Result<CounterVec, PrometheusError> {
let opts = Opts::new(name, help);
let counters = CounterVec::new(opts, variable_labels)?;
let id = counters.desc().first().unwrap().id;
let maybe_counter = self.global_counter_vecs.read().unwrap().get(&id).cloned();
if let Some(counters) = maybe_counter {
Ok(counters)
} else {
self.register(name, Box::new(counters.clone()));
self.global_counter_vecs
.write()
.unwrap()
.insert(id, counters.clone());
Ok(counters)
}
self.global_counter_vec_internal(name, help, None, variable_labels)
}

fn global_deployment_counter_vec(
&self,
name: &str,
help: &str,
subgraph: &str,
variable_labels: &[&str],
) -> Result<CounterVec, PrometheusError> {
self.global_counter_vec_internal(name, help, Some(subgraph), variable_labels)
}

fn global_gauge(
Expand Down
8 changes: 8 additions & 0 deletions graph/src/components/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ pub trait MetricsRegistry: Send + Sync + 'static {
self.global_counter(name, help, deployment_labels(subgraph))
}

fn global_deployment_counter_vec(
&self,
name: &str,
help: &str,
subgraph: &str,
variable_labels: &[&str],
) -> Result<CounterVec, PrometheusError>;

fn global_gauge(
&self,
name: &str,
Expand Down
6 changes: 3 additions & 3 deletions graph/src/components/metrics/stopwatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ impl StopwatchMetrics {
) -> Self {
let stage = stage.to_owned();
let mut inner = StopwatchInner {
counter: *registry
.new_deployment_counter_vec(
counter: registry
.global_deployment_counter_vec(
"deployment_sync_secs",
"total time spent syncing",
subgraph_id.as_str(),
vec!["section".to_owned(), stage.clone()],
&["section", "stage"],
)
.unwrap_or_else(|_| {
panic!(
Expand Down
12 changes: 12 additions & 0 deletions mock/src/metrics_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ impl MetricsRegistryTrait for MockMetricsRegistry {
Ok(counters)
}

fn global_deployment_counter_vec(
&self,
name: &str,
help: &str,
subgraph: &str,
variable_labels: &[&str],
) -> Result<CounterVec, PrometheusError> {
let opts = Opts::new(name, help).const_label("deployment", subgraph);
let counters = CounterVec::new(opts, variable_labels)?;
Ok(counters)
}

fn global_gauge_vec(
&self,
name: &str,
Expand Down

0 comments on commit 764f5c1

Please sign in to comment.