You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pv...@apache.org on 2019/10/03 09:34:14 UTC

[nifi] branch master updated: NIFI-6715: Metrics of removed/renamed components continues to remain in PrometheusReportingTask

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a0a6683  NIFI-6715: Metrics of removed/renamed components continues to remain in PrometheusReportingTask
a0a6683 is described below

commit a0a66839c4edafff410d82c7bb9cbcd5e80aacb3
Author: Kotaro Terada <ko...@yahoo-corp.jp>
AuthorDate: Thu Sep 26 16:32:01 2019 +0900

    NIFI-6715: Metrics of removed/renamed components continues to remain in PrometheusReportingTask
    
    Signed-off-by: Pierre Villard <pi...@gmail.com>
    
    This closes #3766.
---
 .../prometheus/api/PrometheusMetricsUtil.java      | 15 +++++++++++++
 .../prometheus/TestPrometheusReportingTask.java    | 26 ++++++++++++++++++----
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/api/PrometheusMetricsUtil.java b/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/api/PrometheusMetricsUtil.java
index fcf199e..74b0ccc 100644
--- a/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/api/PrometheusMetricsUtil.java
+++ b/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/main/java/org/apache/nifi/reporting/prometheus/api/PrometheusMetricsUtil.java
@@ -17,8 +17,11 @@
 
 package org.apache.nifi.reporting.prometheus.api;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.util.Map;
 
+import io.prometheus.client.SimpleCollector;
 import org.apache.nifi.components.AllowableValue;
 import org.apache.nifi.controller.status.ConnectionStatus;
 import org.apache.nifi.controller.status.PortStatus;
@@ -254,6 +257,18 @@ public class PrometheusMetricsUtil {
         final String componentId = status.getId();
         final String componentName = status.getName();
 
+        // Clear all collectors to deal with removed/renamed components
+        try {
+            for (final Field field : PrometheusMetricsUtil.class.getDeclaredFields()) {
+                if (Modifier.isStatic(field.getModifiers()) && (field.get(null) instanceof SimpleCollector)) {
+                    SimpleCollector sc = (SimpleCollector)(field.get(null));
+                    sc.clear();
+                }
+            }
+        } catch (IllegalAccessException e) {
+            // ignore
+        }
+
         AMOUNT_FLOWFILES_SENT.labels(instanceId, componentType, componentName, componentId, parentPGId).set(status.getFlowFilesSent());
         AMOUNT_FLOWFILES_TRANSFERRED.labels(instanceId, componentType, componentName, componentId, parentPGId).set(status.getFlowFilesTransferred());
         AMOUNT_FLOWFILES_RECEIVED.labels(instanceId, componentType, componentName, componentId, parentPGId).set(status.getFlowFilesReceived());
diff --git a/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusReportingTask.java b/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusReportingTask.java
index 8cb18b2..8fa9237 100644
--- a/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusReportingTask.java
+++ b/nifi-nar-bundles/nifi-prometheus-bundle/nifi-prometheus-reporting-task/src/test/java/org/apache/nifi/reporting/prometheus/TestPrometheusReportingTask.java
@@ -91,6 +91,26 @@ public class TestPrometheusReportingTask {
         reportingContextStub.getEventAccess().setProcessGroupStatus(rootGroupStatus);
         testedReportingTask.onTrigger(reportingContextStub);
 
+        String content = getMetrics();
+        Assert.assertTrue(content.contains(
+                "nifi_amount_flowfiles_received{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+        Assert.assertTrue(content.contains(
+                "nifi_amount_threads_active{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+
+        // Rename the component
+        rootGroupStatus.setName("rootroot");
+        content = getMetrics();
+        Assert.assertFalse(content.contains(
+                "nifi_amount_flowfiles_received{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+        Assert.assertFalse(content.contains(
+                "nifi_amount_threads_active{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+        Assert.assertTrue(content.contains(
+                "nifi_amount_flowfiles_received{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"rootroot\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+        Assert.assertTrue(content.contains(
+                "nifi_amount_threads_active{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"rootroot\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+    }
+
+    private String getMetrics() throws IOException {
         URL url = new URL("http://localhost:9092/metrics");
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con.setRequestMethod("GET");
@@ -102,10 +122,8 @@ public class TestPrometheusReportingTask {
         HttpResponse response = client.execute(request);
         HttpEntity entity = response.getEntity();
         String content = EntityUtils.toString(entity);
-        Assert.assertEquals(true, content.contains(
-                "nifi_amount_flowfiles_received{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
-        Assert.assertEquals(true, content.contains(
-                "nifi_amount_threads_active{instance=\"localhost\",component_type=\"RootProcessGroup\",component_name=\"root\",component_id=\"1234\",parent_id=\"\",} 5.0"));
+
+        return content;
     }
 
 }