[improve][test] Prevent "Failed to register Collector of type Counter"
@@ -25,6 +25,8 @@ import com.google.common.base.MoreObjects;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
+import io.prometheus.client.Collector;
+import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import java.util.List;
import java.util.Map;
@@ -87,17 +89,22 @@ public class ProxyPrometheusMetricsTest extends MockedPulsarServiceBaseTest {
@AfterClass(alwaysRun = true)
protected void cleanup() throws Exception {
internalCleanup();
+ if (proxyWebServer != null) {
proxyWebServer.stop();
+ }
+ if (proxyService != null) {
proxyService.close();
}
+ }
/**
* Validates proxy Prometheus endpoint.
*/
@Test
public void testMetrics() {
- Counter.build("test_counter", "a test counter").create().register();
-
+ Counter counter = Counter.build("test_counter", "a test counter").create();
+ Collector collector = counter.register();
+ try {
Client httpClient = ClientBuilder.newClient(new ClientConfig().register(LoggingFeature.class));
Response r = httpClient.target(proxyWebServer.getServiceUri()).path("/metrics").request()
.get();
@@ -120,6 +127,9 @@ public class ProxyPrometheusMetricsTest extends MockedPulsarServiceBaseTest {
List<Metric> cm3 = (List<Metric>) metrics.get("test_counter_total");
assertEquals(cm3.size(), 1);
assertEquals(cm3.get(0).tags.get("cluster"), TEST_CLUSTER);
+ } finally {
+ CollectorRegistry.defaultRegistry.unregister(collector);
+ }
}
/**
