You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/09/27 11:21:33 UTC

[GitHub] [pulsar] tisonkun commented on a diff in pull request #17850: [broker][monitoring] Add per-thread CPU usage metrics.

tisonkun commented on code in PR #17850:
URL: https://github.com/apache/pulsar/pull/17850#discussion_r981110610


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/PrometheusMetricsTest.java:
##########
@@ -1586,6 +1589,46 @@ public void testMetricsGroupedByTypeDefinitions() throws Exception {
         p2.close();
     }
 
+
+    @Test
+    public void testBrokerThreadsCpuUsageMetrics() throws Exception {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        PrometheusMetricsGenerator.generate(pulsar, false, false, false, false, output);
+        Multimap<String, Metric> metricsMap = parseMetrics(output.toString("UTF-8"));
+        Collection<Metric> total = metricsMap.get("pulsar_broker_thread_total_cpu_usage_ns");
+        Collection<Metric> user = metricsMap.get("pulsar_broker_thread_user_cpu_usage_ns");
+        Collection<Metric> system = metricsMap.get("pulsar_broker_thread_system_cpu_usage_ns");
+
+        Map<String, Metric> threadName2User =
+                user.stream().collect(Collectors.toMap(m -> m.tags.get("thread_name"), m -> m));
+        Map<String, Metric> threadName2System =
+                system.stream().collect(Collectors.toMap(m -> m.tags.get("thread_name"), m -> m));
+
+        ThreadMXBean mxbean = ManagementFactory.getThreadMXBean();
+        if (!mxbean.isThreadCpuTimeSupported()) {
+            return;
+        }
+
+        Assert.assertNotNull(total);
+        Assert.assertTrue(total.size() > 0);
+
+        for (Metric total0 : total) {
+            String cluster = total0.tags.get("cluster");
+            Assert.assertNotNull(cluster);
+            Assert.assertEquals(cluster, "test");
+            String threadName = total0.tags.get("thread_name");
+            Assert.assertNotNull(threadName);
+            Assert.assertTrue(total0.value >= 0);

Review Comment:
   You can try to use assertj's assertion for better error messages. Like:
   
   ```java
   import static org.assertj.core.api.Assertions.assertThat;
   
   assertThat(total0.value).isGreaterThanOrEqualTo(0);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org