NIFI-9385 Add Flow Metrics producer for Cloudera Manager

This closes #5528

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Timea Barna 2021-11-02 13:05:45 +01:00 committed by exceptionfactory
parent 605346ae0f
commit 785021dade
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
1 changed files with 24 additions and 0 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.nifi.web.api;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import io.swagger.annotations.Api;
@ -140,6 +141,7 @@ import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -442,6 +444,28 @@ public class FlowResource extends ApplicationResource {
return generateOkResponse(response)
.type(MediaType.TEXT_PLAIN_TYPE)
.build();
} else if ("clouderamanager".equalsIgnoreCase(producer)) {
final Collection<CollectorRegistry> allRegistries = serviceFacade.generateFlowMetrics();
final Map<String, List<Collector.MetricFamilySamples.Sample>> response = new HashMap<>();
//Output format is the JMX format based of the Cloudera Manager metric descriptor can be found here:
//https://github.com/cloudera/cm_ext/wiki/Service-Monitoring-Descriptor-Language-Reference#-metrics
//Metric samples are filtered to jvm and root samples only
for (CollectorRegistry collectorRegistry : allRegistries) {
Enumeration<Collector.MetricFamilySamples> e = collectorRegistry.metricFamilySamples();
while (e.hasMoreElements()) {
Collector.MetricFamilySamples metricFamilySamples = e.nextElement();
for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
if (sample.name.contains("_jvm_") || sample.labelValues.contains("RootProcessGroup")) {
response.computeIfAbsent("beans", v -> new ArrayList<>()).add(sample);
}
}
}
}
return generateOkResponse(response)
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
} else {
throw new ResourceNotFoundException("The specified producer is missing or invalid.");
}