From a0a66839c4edafff410d82c7bb9cbcd5e80aacb3 Mon Sep 17 00:00:00 2001 From: Kotaro Terada Date: Thu, 26 Sep 2019 16:32:01 +0900 Subject: [PATCH] NIFI-6715: Metrics of removed/renamed components continues to remain in PrometheusReportingTask Signed-off-by: Pierre Villard This closes #3766. --- .../prometheus/api/PrometheusMetricsUtil.java | 15 +++++++++++ .../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 fcf199e96b..74b0ccc649 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 8cb18b254c..8fa9237022 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; } }