Skip to content

Commit

Permalink
[improve][test] Prevent "Failed to register Collector of type Counter" (
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari authored Jan 12, 2023
1 parent 7f75993 commit 3c58491
Showing 1 changed file with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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;
Expand Down Expand Up @@ -87,39 +89,47 @@ protected void setup() throws Exception {
@AfterClass(alwaysRun = true)
protected void cleanup() throws Exception {
internalCleanup();
proxyWebServer.stop();
proxyService.close();
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();

Client httpClient = ClientBuilder.newClient(new ClientConfig().register(LoggingFeature.class));
Response r = httpClient.target(proxyWebServer.getServiceUri()).path("/metrics").request()
.get();
Assert.assertEquals(r.getStatus(), Response.Status.OK.getStatusCode());
String response = r.readEntity(String.class).trim();

Multimap<String, Metric> metrics = parseMetrics(response);

// Check that ProxyService metrics are present
List<Metric> cm = (List<Metric>) metrics.get("pulsar_proxy_binary_bytes_total");
assertEquals(cm.size(), 1);
assertEquals(cm.get(0).tags.get("cluster"), TEST_CLUSTER);

// Check that any Prometheus metric registered in the default CollectorRegistry is present
List<Metric> cm2 = (List<Metric>) metrics.get("test_metrics");
assertEquals(cm2.size(), 1);
assertEquals(cm2.get(0).tags.get("label1"), "xyz");

// Check that PrometheusRawMetricsProvider metrics are present
List<Metric> cm3 = (List<Metric>) metrics.get("test_counter_total");
assertEquals(cm3.size(), 1);
assertEquals(cm3.get(0).tags.get("cluster"), TEST_CLUSTER);
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();
Assert.assertEquals(r.getStatus(), Response.Status.OK.getStatusCode());
String response = r.readEntity(String.class).trim();

Multimap<String, Metric> metrics = parseMetrics(response);

// Check that ProxyService metrics are present
List<Metric> cm = (List<Metric>) metrics.get("pulsar_proxy_binary_bytes_total");
assertEquals(cm.size(), 1);
assertEquals(cm.get(0).tags.get("cluster"), TEST_CLUSTER);

// Check that any Prometheus metric registered in the default CollectorRegistry is present
List<Metric> cm2 = (List<Metric>) metrics.get("test_metrics");
assertEquals(cm2.size(), 1);
assertEquals(cm2.get(0).tags.get("label1"), "xyz");

// Check that PrometheusRawMetricsProvider metrics are present
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);
}
}

/**
Expand Down

0 comments on commit 3c58491

Please sign in to comment.