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/06/15 14:22:30 UTC

[GitHub] [pulsar] asafm commented on a diff in pull request #15558: [fix][broker][functions-worker] Ensure prometheus metrics are grouped by type (#8407, #13865)

asafm commented on code in PR #15558:
URL: https://github.com/apache/pulsar/pull/15558#discussion_r898048085


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/metrics/PrometheusTextFormatUtil.java:
##########
@@ -141,29 +140,32 @@ private static void writeSum(Writer w, DataSketchesOpStatsLogger opStat, String
                 .append(Double.toString(opStat.getSum(success))).append('\n');
     }
 
-    public static void writeMetricsCollectedByPrometheusClient(Writer w, CollectorRegistry registry)
-            throws IOException {
-        Enumeration<MetricFamilySamples> metricFamilySamples = registry.metricFamilySamples();
-        while (metricFamilySamples.hasMoreElements()) {
-            MetricFamilySamples metricFamily = metricFamilySamples.nextElement();
-
-            for (int i = 0; i < metricFamily.samples.size(); i++) {
-                Sample sample = metricFamily.samples.get(i);
-                w.write(sample.name);
-                w.write('{');
-                for (int j = 0; j < sample.labelNames.size(); j++) {
-                    if (j != 0) {
-                        w.write(", ");
+    public static void writeMetrics(SimpleTextOutputStream stream, Collection<MetricFamilySamples> familySamples) {
+        for (MetricFamilySamples familySample : familySamples) {
+            stream.write("# TYPE ");
+            stream.write(familySample.name);
+            stream.write(' ');
+            stream.write(familySample.type.name().toLowerCase());
+            stream.write('\n');
+            for (Collector.MetricFamilySamples.Sample sample : familySample.samples) {
+                stream.write(sample.name);
+                if (sample.labelNames.size() > 0) {
+                    stream.write('{');
+                    for (int i = 0; i < sample.labelNames.size(); ++i) {
+                        stream.write(sample.labelNames.get(i));
+                        stream.write("=\"");
+                        stream.write(sample.labelValues.get(i));
+                        stream.write("\",");
                     }
-                    w.write(sample.labelNames.get(j));
-                    w.write("=\"");
-                    w.write(sample.labelValues.get(j));
-                    w.write('"');
+                    stream.write('}');
                 }
-
-                w.write("} ");
-                w.write(Collector.doubleToGoString(sample.value));
-                w.write('\n');
+                stream.write(' ');
+                stream.write(sample.value);

Review Comment:
   Don't forget to use the method used previously
   ```  /**
      * Convert a double to its string representation in Go.
      */
     public static String doubleToGoString(double d) {
       if (d == Double.POSITIVE_INFINITY) {
         return "+Inf";
       } 
       if (d == Double.NEGATIVE_INFINITY) {
         return "-Inf";
       }
       if (Double.isNaN(d)) {
         return "NaN";
       }
       return Double.toString(d);
     }
   ```



-- 
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