Skip to content

Commit 413f889

Browse files
Add clear() method into PrometheusRegistry to unregister all collectors (#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]>
1 parent 38188b9 commit 413f889

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/PrometheusRegistry.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public void unregister(MultiCollector collector) {
5656
}
5757
}
5858

59+
public void clear() {
60+
collectors.clear();
61+
multiCollectors.clear();
62+
prometheusNames.clear();
63+
}
64+
5965
public MetricSnapshots scrape() {
6066
return scrape((PrometheusScrapeRequest) null);
6167
}

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/PrometheusRegistryTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,16 @@ public void registerOkMultiCollector() {
145145
snapshots = registry.scrape();
146146
assertThat(snapshots.size()).isZero();
147147
}
148+
149+
@Test
150+
public void clearOk() {
151+
PrometheusRegistry registry = new PrometheusRegistry();
152+
registry.register(counterA1);
153+
registry.register(counterB);
154+
registry.register(gaugeA);
155+
assertThat(registry.scrape().size()).isEqualTo(3);
156+
157+
registry.clear();
158+
assertThat(registry.scrape().size()).isZero();
159+
}
148160
}

0 commit comments

Comments
 (0)