Skip to content

Commit

Permalink
Update code for new Prometheus simpleclient version
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed Jan 8, 2024
1 parent cd99332 commit ea82edd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ private Observation convertSummarySampleNamesToLabels(
labelValues.add("sum");
} else if (sample.name.endsWith("_count")) {
labelValues.add("count");
} else if (sample.name.endsWith("_created")) {
labelValues.add("created");
} else {
labelValues.add(labelValues.size() - 1, "quantile");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ public void shouldCreateObservationFromCounter() {

counter.inc();
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 1.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 1.0, emptyList()));

counter.inc();
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 2.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 2.0, emptyList()));
}

@Test
Expand All @@ -83,26 +85,30 @@ public void shouldHandleDuplicateCounterCreation() {

counter1.labels().inc();
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 1.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 1.0, emptyList()));

counter2.labels().inc();
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 2.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 2.0, emptyList()));
}

@Test
public void shouldCreateSeparateObservationsForEachCounterLabelValue() {
final LabelledMetric<Counter> counter =
metricsSystem.createLabelledCounter(PEERS, "connected", "Some help string", "labelName");
metricsSystem.createLabelledCounter(
PEERS, "connected_total", "Some help string", "labelName");

counter.labels("value1").inc();
counter.labels("value2").inc();
counter.labels("value1").inc();

assertThat(metricsSystem.streamObservations())
.filteredOn(this::notCreatedSample)
.containsExactlyInAnyOrder(
new Observation(PEERS, "connected", 2.0, singletonList("value1")),
new Observation(PEERS, "connected", 1.0, singletonList("value2")));
new Observation(PEERS, "connected_total", 2.0, singletonList("value1")),
new Observation(PEERS, "connected_total", 1.0, singletonList("value2")));
}

@Test
Expand Down Expand Up @@ -138,11 +144,13 @@ public void shouldIncrementCounterBySpecifiedAmount() {

counter.inc(5);
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 5.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 5.0, emptyList()));

counter.inc(6);
assertThat(metricsSystem.streamObservations())
.containsExactly(new Observation(PEERS, "connected", 11.0, emptyList()));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(PEERS, "connected_total", 11.0, emptyList()));
}

@Test
Expand All @@ -162,7 +170,8 @@ public void shouldCreateObservationsFromTimer() {
new Observation(RPC, "request", null, asList("quantile", "0.99")),
new Observation(RPC, "request", null, asList("quantile", "1.0")),
new Observation(RPC, "request", null, singletonList("sum")),
new Observation(RPC, "request", null, singletonList("count")));
new Observation(RPC, "request", null, singletonList("count")),
new Observation(RPC, "request", null, singletonList("created")));
}

@Test
Expand Down Expand Up @@ -192,7 +201,8 @@ public void shouldCreateObservationsFromTimerWithLabels() {
new Observation(RPC, "request", null, asList("method", "quantile", "0.99")),
new Observation(RPC, "request", null, asList("method", "quantile", "1.0")),
new Observation(RPC, "request", null, asList("method", "sum")),
new Observation(RPC, "request", null, asList("method", "count")));
new Observation(RPC, "request", null, asList("method", "count")),
new Observation(RPC, "request", null, asList("method", "created")));
}

@Test
Expand Down Expand Up @@ -251,7 +261,8 @@ public void shouldOnlyObserveEnabledMetrics() {

counterR.labels("op").inc();
assertThat(localMetricSystem.streamObservations())
.containsExactly(new Observation(RPC, "name", 1.0, singletonList("op")));
.filteredOn(this::notCreatedSample)
.containsExactly(new Observation(RPC, "name_total", 1.0, singletonList("op")));
}

@Test
Expand Down Expand Up @@ -280,4 +291,10 @@ public void returnsNoOpMetricsWhenPushEnabled() {

assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class);
}

private boolean notCreatedSample(final Observation obs) {
// Simple client 0.10.0 add a _created sample to every counter, histogram and summary, that we
// may want to ignore
return !obs.getMetricName().endsWith("_created");
}
}

0 comments on commit ea82edd

Please sign in to comment.