merlimat commented on a change in pull request #1373: Functions metrics 
prometheus
URL: https://github.com/apache/incubator-pulsar/pull/1373#discussion_r174362199
 
 

 ##########
 File path: 
pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionsStatsGenerator.java
 ##########
 @@ -0,0 +1,93 @@
+/**
+ * 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.functions.worker;
+
+import org.apache.pulsar.functions.proto.InstanceCommunication;
+import org.apache.pulsar.functions.runtime.Runtime;
+import org.apache.pulsar.functions.runtime.RuntimeSpawner;
+import org.apache.pulsar.common.util.SimpleTextOutputStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * A class to generate stats for pulsar functions running on this broker
+ */
+public class FunctionsStatsGenerator {
+
+    private static final Logger log = 
LoggerFactory.getLogger(FunctionsStatsGenerator.class);
+
+    public static void generate(WorkerService workerService, String cluster, 
SimpleTextOutputStream out) {
+        if (workerService != null) {
+            Map<String, FunctionRuntimeInfo> functionRuntimes
+                    = 
workerService.getFunctionRuntimeManager().getFunctionRuntimeInfos();
+
+            for (Map.Entry<String, FunctionRuntimeInfo> entry : 
functionRuntimes.entrySet()) {
+                String fullyQualifiedInstanceName = entry.getKey();
+                FunctionRuntimeInfo functionRuntimeInfo = entry.getValue();
+                RuntimeSpawner functionRuntimeSpawner = 
functionRuntimeInfo.getRuntimeSpawner();
+
+                if (functionRuntimeSpawner != null) {
+                    Runtime functionRuntime = 
functionRuntimeSpawner.getRuntime();
+                    if (functionRuntime != null) {
+                        try {
+                            InstanceCommunication.MetricsData metrics = 
functionRuntime.getAndResetMetrics().get();
+                            for (Map.Entry<String, 
InstanceCommunication.MetricsData.DataDigest> metricsEntry
+                                    : metrics.getMetricsMap().entrySet()) {
+                                String metricName = metricsEntry.getKey();
+                                InstanceCommunication.MetricsData.DataDigest 
dataDigest = metricsEntry.getValue();
+
+                                String tenant = 
functionRuntimeInfo.getFunctionInstance()
+                                        
.getFunctionMetaData().getFunctionConfig().getTenant();
+                                String namespace = 
functionRuntimeInfo.getFunctionInstance()
+                                        
.getFunctionMetaData().getFunctionConfig().getNamespace();
+                                String name = 
functionRuntimeInfo.getFunctionInstance()
+                                        
.getFunctionMetaData().getFunctionConfig().getName();
+                                int instanceId = 
functionRuntimeInfo.getFunctionInstance().getInstanceId();
 
 Review comment:
   We should have some test to verify the metrics output. There is already a 
parser for the Prometheus format in the tests code. It parses the stats into a 
map. Take a look at `PrometheusMetricsTest`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to