Skip to content

Commit

Permalink
Ignore clearmodes when comparing label names
Browse files Browse the repository at this point in the history
We don't care about the clear mode label when we're comparing label names because that
label will be ultimately stripped out before merging. Thus, when we compare new and old label
names for equivalency, this commit fixes that check by simply skipping the clearmode if it
exists in the new push
  • Loading branch information
sinkingpoint committed Jul 11, 2022
1 parent 8a006e7 commit c32b2d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ fn add_extra_labels(mut exposition: MetricsExposition<PrometheusType, Prometheus
return Ok(exposition);
}

// are_label_names_equivalent checks wether two sets of label names are equivalent,
// minus label names that are irrelevant to the final push (basically just the clearmode)
fn are_label_names_equivalent(existing: &[String], new: &[String]) -> bool {
for new_label in new.iter() {
if new_label == CLEARMODE_LABEL_NAME {
continue
}

if !existing.contains(new_label) {
return false;
}
}

return true;
}

impl Aggregator {
pub fn new() -> Aggregator {
return Aggregator {
Expand All @@ -337,7 +353,7 @@ impl Aggregator {
for (name, metrics) in metrics.families {
match families.get_mut(&name) {
Some(f) => {
if f.base_family.get_label_names() != metrics.get_label_names() {
if !are_label_names_equivalent(f.base_family.get_label_names(), metrics.get_label_names()) {
// The new push has different label names - abort
return Err(AggregationError::Error("invalid push - new push has different label names than the existing family".to_string()))
}
Expand Down
3 changes: 3 additions & 0 deletions src/aggregator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,7 @@ async fn test_push_with_different_label_names() {
assert!(agg.parse_and_merge("requests_num_total{job=\"test\"} 1\n", &HashMap::new()).await.is_err(), "failed to reject invalid label name");
assert!(agg.parse_and_merge("requests_num_total{bar=\"test\"} 1\n", &HashMap::new()).await.is_err(), "failed to reject invalid label name");
assert!(agg.parse_and_merge("requests_num_total{LAMBDA_NAME=\"test_function\"} 1\n", &HashMap::new()).await.is_ok(), "failed to parse metric with same label name");

assert!(agg.parse_and_merge("requests_num_total2{clearmode=\"mean5m\"} 1\n", &HashMap::new()).await.is_ok(), "failed to add metric with clearmode");
assert!(agg.parse_and_merge("requests_num_total2{clearmode=\"mean5m\"} 1\n", &HashMap::new()).await.is_ok(), "failed to add second metric with clearmode");
}

0 comments on commit c32b2d6

Please sign in to comment.