You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by do...@apache.org on 2022/10/19 04:51:25 UTC

[inlong] branch master updated: [INLONG-6194][Agent] Support parsing metrics for different components (#6195)

This is an automated email from the ASF dual-hosted git repository.

dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b66f581d [INLONG-6194][Agent] Support parsing metrics for different components (#6195)
1b66f581d is described below

commit 1b66f581dd38d5a5e4501192da116a8534d961e3
Author: Keylchen <11...@users.noreply.github.com>
AuthorDate: Wed Oct 19 12:51:20 2022 +0800

    [INLONG-6194][Agent] Support parsing metrics for different components (#6195)
---
 .../metrics/AgentPrometheusMetricListener.java     | 71 ++++++++++++----------
 1 file changed, 39 insertions(+), 32 deletions(-)

diff --git a/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/metrics/AgentPrometheusMetricListener.java b/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/metrics/AgentPrometheusMetricListener.java
index 6e809e7eb..7c57aa83f 100644
--- a/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/metrics/AgentPrometheusMetricListener.java
+++ b/inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/metrics/AgentPrometheusMetricListener.java
@@ -17,31 +17,10 @@
 
 package org.apache.inlong.agent.metrics;
 
-import io.prometheus.client.Collector;
-import io.prometheus.client.CounterMetricFamily;
-import io.prometheus.client.exporter.HTTPServer;
-import io.prometheus.client.hotspot.DefaultExports;
-import org.apache.inlong.agent.conf.AgentConfiguration;
-import org.apache.inlong.common.metric.MetricItemValue;
-import org.apache.inlong.common.metric.MetricListener;
-import org.apache.inlong.common.metric.MetricValue;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-import java.io.IOException;
-import java.lang.management.ManagementFactory;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-
 import static org.apache.inlong.agent.constant.AgentConstants.DEFAULT_PROMETHEUS_EXPORTER_PORT;
 import static org.apache.inlong.agent.constant.AgentConstants.PROMETHEUS_EXPORTER_PORT;
+import static org.apache.inlong.agent.metrics.AgentMetricItem.KEY_COMPONENT_NAME;
+import static org.apache.inlong.agent.metrics.AgentMetricItem.KEY_PLUGIN_ID;
 import static org.apache.inlong.agent.metrics.AgentMetricItem.M_JOB_FATAL_COUNT;
 import static org.apache.inlong.agent.metrics.AgentMetricItem.M_JOB_RUNNING_COUNT;
 import static org.apache.inlong.agent.metrics.AgentMetricItem.M_PLUGIN_READ_COUNT;
@@ -60,12 +39,35 @@ import static org.apache.inlong.agent.metrics.AgentMetricItem.M_TASK_RUNNING_COU
 import static org.apache.inlong.common.metric.MetricItemMBean.DOMAIN_SEPARATOR;
 import static org.apache.inlong.common.metric.MetricRegister.JMX_DOMAIN;
 
+import io.prometheus.client.Collector;
+import io.prometheus.client.CounterMetricFamily;
+import io.prometheus.client.exporter.HTTPServer;
+import io.prometheus.client.hotspot.DefaultExports;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import org.apache.inlong.agent.conf.AgentConfiguration;
+import org.apache.inlong.common.metric.MetricItemValue;
+import org.apache.inlong.common.metric.MetricListener;
+import org.apache.inlong.common.metric.MetricValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * prometheus metric listener
  */
 public class AgentPrometheusMetricListener extends Collector implements MetricListener {
 
     public static final String DEFAULT_DIMENSION_LABEL = "dimension";
+    public static final String HYPHEN_SYMBOL = "-";
     private static final Logger LOGGER = LoggerFactory.getLogger(AgentPrometheusMetricListener.class);
     protected HTTPServer httpServer;
     private AgentMetricItem metricItem;
@@ -142,12 +144,17 @@ public class AgentPrometheusMetricListener extends Collector implements MetricLi
         mfs.add(totalCounter);
 
         // id dimension
-        List<String> dimensionIdKeys = new ArrayList<>();
-        dimensionIdKeys.add(DEFAULT_DIMENSION_LABEL);
-        dimensionIdKeys.addAll(this.dimensionKeys);
-        CounterMetricFamily idCounter = new CounterMetricFamily("id", "metrics_of_agent_dimensions", dimensionIdKeys);
         for (Entry<String, MetricItemValue> entry : this.dimensionMetricValueMap.entrySet()) {
             MetricItemValue itemValue = entry.getValue();
+            Map<String, String> dimensionMap = itemValue.getDimensions();
+            String pluginId = dimensionMap.getOrDefault(KEY_PLUGIN_ID, HYPHEN_SYMBOL);
+            String componentName = dimensionMap.getOrDefault(KEY_COMPONENT_NAME, HYPHEN_SYMBOL);
+            String counterName = pluginId.equals(HYPHEN_SYMBOL) ? componentName : pluginId;
+            List<String> dimensionIdKeys = new ArrayList<>();
+            dimensionIdKeys.add(DEFAULT_DIMENSION_LABEL);
+            dimensionIdKeys.addAll(dimensionMap.keySet());
+            CounterMetricFamily idCounter = new CounterMetricFamily(counterName,
+                    "metrics_of_agent_dimensions_" + counterName, dimensionIdKeys);
 
             addCounterMetricFamily(M_JOB_RUNNING_COUNT, itemValue, idCounter);
             addCounterMetricFamily(M_JOB_FATAL_COUNT, itemValue, idCounter);
@@ -168,8 +175,8 @@ public class AgentPrometheusMetricListener extends Collector implements MetricLi
             addCounterMetricFamily(M_PLUGIN_SEND_FAIL_COUNT, itemValue, idCounter);
             addCounterMetricFamily(M_PLUGIN_READ_SUCCESS_COUNT, itemValue, idCounter);
             addCounterMetricFamily(M_PLUGIN_SEND_SUCCESS_COUNT, itemValue, idCounter);
+            mfs.add(idCounter);
         }
-        mfs.add(idCounter);
         return mfs;
     }
 
@@ -218,11 +225,11 @@ public class AgentPrometheusMetricListener extends Collector implements MetricLi
 
     private void addCounterMetricFamily(String defaultDimension, MetricItemValue itemValue,
             CounterMetricFamily idCounter) {
-        List<String> labelValues = new ArrayList<>(this.dimensionKeys.size());
+        Map<String, String> dimensionMap = itemValue.getDimensions();
+        List<String> labelValues = new ArrayList<>(dimensionMap.size() + 1);
         labelValues.add(defaultDimension);
-        Map<String, String> dimensions = itemValue.getDimensions();
-        for (String key : this.dimensionKeys) {
-            String labelValue = dimensions.getOrDefault(key, "-");
+        for (String key : dimensionMap.keySet()) {
+            String labelValue = dimensionMap.getOrDefault(key, HYPHEN_SYMBOL);
             labelValues.add(labelValue);
         }
         long value = 0L;