Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,15 @@ public D labelValues(String... labelValues) {
*/
public void remove(String... labelValues) {
data.remove(Arrays.asList(labelValues));
if (labelValues.length == 0) {
noLabels = null;
}
}

/** Remove the data points when the given function. */
public void removeIf(Function<List<String>, Boolean> f) {
data.entrySet().removeIf(entry -> f.apply(Collections.unmodifiableList(entry.getKey())));
noLabels = null;
}

/** Reset the metric (remove all data points). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ void testClear() {
assertThat(counter.collect().getDataPoints()).hasSize(1);
}

@Test
void testRemoveNoLabels() {
Counter counter = Counter.builder().name("test").build();
counter.inc();
counter.remove();

// Making inc() works correctly after remove()
counter.inc();
assertThat(counter.collect().getDataPoints()).hasSize(1);
assertThat(counter.collect().getDataPoints().get(0).getValue()).isEqualTo(1.0);
}

@Test
void testRemoveIfNoLabels() {
Counter counter = Counter.builder().name("test").build();
counter.inc();
counter.removeIf(labelValues -> true);

// Making inc() works correctly after removeIf()
counter.inc();
assertThat(counter.collect().getDataPoints()).hasSize(1);
assertThat(counter.collect().getDataPoints().get(0).getValue()).isEqualTo(1.0);
}

@Test
void testClearNoLabels() {
Counter counter = Counter.builder().name("test").build();
Expand Down
Loading