Skip to content

Commit

Permalink
Add clear() method into PrometheusRegistry to unregister all collecto…
Browse files Browse the repository at this point in the history
…rs (#1158)

* Add clear() method into PrometheusRegistry to unregister all collectors
Signed-off-by: Petr Holubec <[email protected]>

* format

Signed-off-by: Gregor Zeitlinger <[email protected]>

* test

Signed-off-by: Gregor Zeitlinger <[email protected]>

---------

Signed-off-by: Gregor Zeitlinger <[email protected]>
Co-authored-by: Petr Holubec <[email protected]>
  • Loading branch information
zeitlinger and holubec-petr authored Oct 17, 2024
1 parent 38188b9 commit 413f889
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public void unregister(MultiCollector collector) {
}
}

public void clear() {
collectors.clear();
multiCollectors.clear();
prometheusNames.clear();
}

public MetricSnapshots scrape() {
return scrape((PrometheusScrapeRequest) null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ public void registerOkMultiCollector() {
snapshots = registry.scrape();
assertThat(snapshots.size()).isZero();
}

@Test
public void clearOk() {
PrometheusRegistry registry = new PrometheusRegistry();
registry.register(counterA1);
registry.register(counterB);
registry.register(gaugeA);
assertThat(registry.scrape().size()).isEqualTo(3);

registry.clear();
assertThat(registry.scrape().size()).isZero();
}
}

0 comments on commit 413f889

Please sign in to comment.