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/08/02 09:54:03 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_r935251401


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricStreams.java:
##########
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.stats.prometheus;
+
+import io.netty.buffer.ByteBufAllocator;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pulsar.common.util.SimpleTextOutputStream;
+
+/**
+ * Helper class to ensure that metrics of the same name are grouped together under the same TYPE header when written.
+ * Those are the requirements of the
+ * <a href="https://github.com/prometheus/docs/blob/master/content/docs/instrumenting/exposition_formats.md#grouping-and-sorting">Prometheus Exposition Format</a>.
+ */
+public class PrometheusMetricStreams {
+    private final Map<String, SimpleTextOutputStream> metricStreamMap = new HashMap<>();
+
+    /**
+     * Write the given metric and sample value to the stream. Will write #TYPE header if metric not seen before.
+     * @param metricName name of the metric.
+     * @param value value of the sample
+     * @param labelsAndValuesArray varargs of label and label value
+     */
+    void writeSample(String metricName, Number value, String... labelsAndValuesArray) {
+        SimpleTextOutputStream stream = initGaugeType(metricName);
+        stream.write(metricName).write('{');
+        for (int i = 0; i < labelsAndValuesArray.length; i += 2) {
+            stream.write(labelsAndValuesArray[i]).write("=\"").write(labelsAndValuesArray[i + 1]).write('\"');
+            if (i + 2 != labelsAndValuesArray.length) {
+                stream.write(',');
+            }
+        }
+        stream.write("\"} ").write(value).write(' ').write(System.currentTimeMillis()).write('\n');

Review Comment:
   Just double checking here - the `stream.write("\"{ ")` - what's `"` closing here? 



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/NamespaceStatsAggregator.java:
##########
@@ -296,161 +301,192 @@ private static void getTopicStats(Topic topic, TopicStats stats, boolean include
                 });
     }
 
-    private static void printDefaultBrokerStats(SimpleTextOutputStream stream, String cluster) {
+    private static void printDefaultBrokerStats(PrometheusMetricStreams stream, String cluster) {
         // Print metrics with 0 values. This is necessary to have the available brokers being
         // reported in the brokers dashboard even if they don't have any topic or traffic
-        metric(stream, cluster, "pulsar_topics_count", 0);
-        metric(stream, cluster, "pulsar_subscriptions_count", 0);
-        metric(stream, cluster, "pulsar_producers_count", 0);
-        metric(stream, cluster, "pulsar_consumers_count", 0);
-        metric(stream, cluster, "pulsar_rate_in", 0);
-        metric(stream, cluster, "pulsar_rate_out", 0);
-        metric(stream, cluster, "pulsar_throughput_in", 0);
-        metric(stream, cluster, "pulsar_throughput_out", 0);
-        metric(stream, cluster, "pulsar_storage_size", 0);
-        metric(stream, cluster, "pulsar_storage_logical_size", 0);
-        metric(stream, cluster, "pulsar_storage_write_rate", 0);
-        metric(stream, cluster, "pulsar_storage_read_rate", 0);
-        metric(stream, cluster, "pulsar_msg_backlog", 0);
+        writeMetric(stream, "pulsar_topics_count", 0, cluster);
+        writeMetric(stream, "pulsar_subscriptions_count", 0, cluster);
+        writeMetric(stream, "pulsar_producers_count", 0, cluster);
+        writeMetric(stream, "pulsar_consumers_count", 0, cluster);
+        writeMetric(stream, "pulsar_rate_in", 0, cluster);
+        writeMetric(stream, "pulsar_rate_out", 0, cluster);
+        writeMetric(stream, "pulsar_throughput_in", 0, cluster);
+        writeMetric(stream, "pulsar_throughput_out", 0, cluster);
+        writeMetric(stream, "pulsar_storage_size", 0, cluster);
+        writeMetric(stream, "pulsar_storage_logical_size", 0, cluster);
+        writeMetric(stream, "pulsar_storage_write_rate", 0, cluster);
+        writeMetric(stream, "pulsar_storage_read_rate", 0, cluster);
+        writeMetric(stream, "pulsar_msg_backlog", 0, cluster);
     }
 
-    private static void printTopicsCountStats(SimpleTextOutputStream stream, String cluster, String namespace,
-                                              LongAdder topicsCount) {
-        metric(stream, cluster, namespace, "pulsar_topics_count", topicsCount.sum());
+    private static void printTopicsCountStats(PrometheusMetricStreams stream, Map<String, Long> namespaceTopicsCount,
+                                              String cluster) {
+        namespaceTopicsCount.forEach(
+                (ns, topicCount) -> writeMetric(stream, "pulsar_topics_count", topicCount, cluster, ns)
+        );
     }
 
-    private static void printNamespaceStats(SimpleTextOutputStream stream, String cluster, String namespace,
-                                            AggregatedNamespaceStats stats) {
-        metric(stream, cluster, namespace, "pulsar_topics_count", stats.topicsCount);
-        metric(stream, cluster, namespace, "pulsar_subscriptions_count", stats.subscriptionsCount);
-        metric(stream, cluster, namespace, "pulsar_producers_count", stats.producersCount);
-        metric(stream, cluster, namespace, "pulsar_consumers_count", stats.consumersCount);
-
-        metric(stream, cluster, namespace, "pulsar_rate_in", stats.rateIn);
-        metric(stream, cluster, namespace, "pulsar_rate_out", stats.rateOut);
-        metric(stream, cluster, namespace, "pulsar_throughput_in", stats.throughputIn);
-        metric(stream, cluster, namespace, "pulsar_throughput_out", stats.throughputOut);
-        metric(stream, cluster, namespace, "pulsar_consumer_msg_ack_rate", stats.messageAckRate);
-
-        metric(stream, cluster, namespace, "pulsar_in_bytes_total", stats.bytesInCounter);
-        metric(stream, cluster, namespace, "pulsar_in_messages_total", stats.msgInCounter);
-        metric(stream, cluster, namespace, "pulsar_out_bytes_total", stats.bytesOutCounter);
-        metric(stream, cluster, namespace, "pulsar_out_messages_total", stats.msgOutCounter);
-
-        metric(stream, cluster, namespace, "pulsar_storage_size", stats.managedLedgerStats.storageSize);
-        metric(stream, cluster, namespace, "pulsar_storage_logical_size", stats.managedLedgerStats.storageLogicalSize);
-        metric(stream, cluster, namespace, "pulsar_storage_backlog_size", stats.managedLedgerStats.backlogSize);
-        metric(stream, cluster, namespace, "pulsar_storage_offloaded_size",
-                stats.managedLedgerStats.offloadedStorageUsed);
-
-        metric(stream, cluster, namespace, "pulsar_storage_write_rate", stats.managedLedgerStats.storageWriteRate);
-        metric(stream, cluster, namespace, "pulsar_storage_read_rate", stats.managedLedgerStats.storageReadRate);
-
-        metric(stream, cluster, namespace, "pulsar_subscription_delayed", stats.msgDelayed);
-
-        metricWithRemoteCluster(stream, cluster, namespace, "pulsar_msg_backlog", "local", stats.msgBacklog);
+    private static void printNamespaceStats(PrometheusMetricStreams stream, AggregatedNamespaceStats stats,
+                                            String cluster, String namespace) {
+        writeMetric(stream, "pulsar_topics_count", stats.topicsCount, cluster, namespace);
+        writeMetric(stream, "pulsar_subscriptions_count", stats.subscriptionsCount, cluster,
+                namespace);
+        writeMetric(stream, "pulsar_producers_count", stats.producersCount, cluster, namespace);
+        writeMetric(stream, "pulsar_consumers_count", stats.consumersCount, cluster, namespace);
+
+        writeMetric(stream, "pulsar_rate_in", stats.rateIn, cluster, namespace);
+        writeMetric(stream, "pulsar_rate_out", stats.rateOut, cluster, namespace);
+        writeMetric(stream, "pulsar_throughput_in", stats.throughputIn, cluster, namespace);
+        writeMetric(stream, "pulsar_throughput_out", stats.throughputOut, cluster, namespace);
+        writeMetric(stream, "pulsar_consumer_msg_ack_rate", stats.messageAckRate, cluster, namespace);
+
+        writeMetric(stream, "pulsar_in_bytes_total", stats.bytesInCounter, cluster, namespace);
+        writeMetric(stream, "pulsar_in_messages_total", stats.msgInCounter, cluster, namespace);
+        writeMetric(stream, "pulsar_out_bytes_total", stats.bytesOutCounter, cluster, namespace);
+        writeMetric(stream, "pulsar_out_messages_total", stats.msgOutCounter, cluster, namespace);
+
+        writeMetric(stream, "pulsar_storage_size", stats.managedLedgerStats.storageSize, cluster,
+                namespace);
+        writeMetric(stream, "pulsar_storage_logical_size",
+                stats.managedLedgerStats.storageLogicalSize, cluster, namespace);
+        writeMetric(stream, "pulsar_storage_backlog_size", stats.managedLedgerStats.backlogSize, cluster,
+                namespace);
+        writeMetric(stream, "pulsar_storage_offloaded_size",
+                stats.managedLedgerStats.offloadedStorageUsed, cluster, namespace);
+
+        writeMetric(stream, "pulsar_storage_write_rate", stats.managedLedgerStats.storageWriteRate,
+                cluster, namespace);
+        writeMetric(stream, "pulsar_storage_read_rate", stats.managedLedgerStats.storageReadRate,
+                cluster, namespace);
+
+        writeMetric(stream, "pulsar_subscription_delayed", stats.msgDelayed, cluster, namespace);
+
+        writePulsarMsgBacklog(stream, stats.msgBacklog, cluster, namespace);
 
         stats.managedLedgerStats.storageWriteLatencyBuckets.refresh();
-        long[] latencyBuckets = stats.managedLedgerStats.storageWriteLatencyBuckets.getBuckets();
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_0_5", latencyBuckets[0]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_1", latencyBuckets[1]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_5", latencyBuckets[2]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_10", latencyBuckets[3]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_20", latencyBuckets[4]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_50", latencyBuckets[5]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_100", latencyBuckets[6]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_200", latencyBuckets[7]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_le_1000", latencyBuckets[8]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_overflow", latencyBuckets[9]);
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_count",
-                stats.managedLedgerStats.storageWriteLatencyBuckets.getCount());
-        metric(stream, cluster, namespace, "pulsar_storage_write_latency_sum",
-                stats.managedLedgerStats.storageWriteLatencyBuckets.getSum());
+        writeMetric(stream, "pulsar_storage_write_latency_le_0_5",
+                stats.managedLedgerStats.storageWriteLatencyBuckets.getBuckets()[0], cluster, namespace);

Review Comment:
   Why not call `getBuckets()` once and re-use it below like it was? I think it's much easier to read that way. Relevant to all get buckets below



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricStreams.java:
##########
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.stats.prometheus;
+
+import io.netty.buffer.ByteBufAllocator;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.pulsar.common.util.SimpleTextOutputStream;
+
+/**
+ * Helper class to ensure that metrics of the same name are grouped together under the same TYPE header when written.
+ */
+public class PrometheusMetricStreams {
+    private final Map<String, SimpleTextOutputStream> metricStreamMap = new HashMap<>();
+
+    /**
+     * Write the given metric and sample value to the stream. Will write #TYPE header if metric not seen before.
+     * @param metricName name of the metric.
+     * @param value value of the sample
+     * @param labelsAndValuesArray varargs of label and label value
+     */
+    void writeSample(String metricName, Number value, String... labelsAndValuesArray) {
+        SimpleTextOutputStream stream = initGaugeType(metricName);
+        stream.write(metricName).write('{');
+        for (int i = 0; i < labelsAndValuesArray.length; i += 2) {
+            stream.write(labelsAndValuesArray[i]).write("=\"").write(labelsAndValuesArray[i + 1]).write('\"');
+            if (i + 2 != labelsAndValuesArray.length) {
+                stream.write(',');
+            }
+        }
+        stream.write("\"} ").write(value).write(' ').write(System.currentTimeMillis()).write('\n');

Review Comment:
   @tjiuming WDYT



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