Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataEntity;
import org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity;
import org.apache.nifi.web.api.entity.VersionedFlowsEntity;
import org.apache.nifi.web.api.metrics.JsonFormatPrometheusMetricsWriter;
import org.apache.nifi.web.api.metrics.TextFormatPrometheusMetricsWriter;
import org.apache.nifi.web.api.metrics.PrometheusMetricsWriter;
import org.apache.nifi.web.api.request.BulletinBoardPatternParameter;
Expand Down Expand Up @@ -428,7 +429,11 @@ public Response getFlowMetrics(
@ApiParam(
value = "Regular Expression Pattern to be applied against the sample label value field"
)
@QueryParam("sampleLabelValue") final String sampleLabelValue
@QueryParam("sampleLabelValue") final String sampleLabelValue,
@ApiParam(
value = "Name of the first field of JSON object. Applicable for JSON producer only."
)
@QueryParam("rootFieldName") final String rootFieldName
) {

authorizeFlow();
Expand All @@ -442,6 +447,16 @@ public Response getFlowMetrics(
prometheusMetricsWriter.write(registries, outputStream);
});
return generateOkResponse(response).type(TextFormat.CONTENT_TYPE_004).build();

} else if (FlowMetricsProducer.JSON.getProducer().equals(producer)) {
final StreamingOutput output = outputStream -> {
final JsonFormatPrometheusMetricsWriter jsonPrometheusMetricsWriter = new JsonFormatPrometheusMetricsWriter(sampleName, sampleLabelValue, rootFieldName);
jsonPrometheusMetricsWriter.write(registries, outputStream);
};
return generateOkResponse(output)
.type(MediaType.APPLICATION_JSON_TYPE)
.build();

} else {
throw new ResourceNotFoundException("The specified producer is missing or invalid.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.nifi.web.api.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.apache.commons.lang3.StringUtils;
import java.util.Enumeration;
import java.util.regex.Pattern;

public abstract class AbstractPrometheusMetricsWriter implements PrometheusMetricsWriter {
private final Pattern sampleNamePattern;

private final Pattern sampleLabelValuePattern;

private final boolean filteringDisabled;

public AbstractPrometheusMetricsWriter(
final String sampleName,
final String sampleLabelValue
) {
this.sampleNamePattern = StringUtils.isBlank(sampleName) ? null : Pattern.compile(sampleName);
this.sampleLabelValuePattern = StringUtils.isBlank(sampleLabelValue) ? null : Pattern.compile(sampleLabelValue);
this.filteringDisabled = StringUtils.isAllBlank(sampleName, sampleLabelValue);
}

Enumeration<Collector.MetricFamilySamples> getSamples(final CollectorRegistry registry) {
final Enumeration<Collector.MetricFamilySamples> samples = registry.metricFamilySamples();
return filteringDisabled ? samples : new FilteringMetricFamilySamplesEnumeration(
samples,
sampleNamePattern,
sampleLabelValuePattern
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.nifi.web.api.metrics;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Enumeration;

/**
* Prometheus Metrics Writer with Json output format and optional filtering
*/
public class JsonFormatPrometheusMetricsWriter extends AbstractPrometheusMetricsWriter {
private final String firstFieldName;

public JsonFormatPrometheusMetricsWriter(final String sampleName, final String sampleLabelValue, final String firstFieldName) {
super(sampleName, sampleLabelValue);
this.firstFieldName = firstFieldName == null ? "samples" : firstFieldName;
}

@Override
public void write(final Collection<CollectorRegistry> registries, final OutputStream outputStream) throws IOException {
JsonFactory factory = new JsonFactory();
try (final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream));
final JsonGenerator generator = factory.createGenerator(writer)) {
generator.setCodec(new ObjectMapper());
generator.writeStartObject();
generator.writeFieldName(firstFieldName);
generator.writeStartArray();
for (final CollectorRegistry collectorRegistry : registries) {
final Enumeration<Collector.MetricFamilySamples> samples = getSamples(collectorRegistry);
while (samples.hasMoreElements()) {
final Collector.MetricFamilySamples samples2 = samples.nextElement();
for (Collector.MetricFamilySamples.Sample sample : samples2.samples) {
generator.writeObject(sample);
generator.flush();
}
generator.flush();
}
}
generator.writeEndArray();
generator.writeEndObject();
generator.flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedWriter;
import java.io.IOException;
Expand All @@ -28,25 +27,14 @@
import java.io.Writer;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;

/**
* Prometheus Metrics Writer supporting Prometheus Text Version 0.0.4 with optional filtering
*/
public class TextFormatPrometheusMetricsWriter implements PrometheusMetricsWriter {
private final Pattern sampleNamePattern;
public class TextFormatPrometheusMetricsWriter extends AbstractPrometheusMetricsWriter {

private final Pattern sampleLabelValuePattern;

private final boolean filteringDisabled;

public TextFormatPrometheusMetricsWriter(
final String sampleName,
final String sampleLabelValue
) {
this.sampleNamePattern = StringUtils.isBlank(sampleName) ? null : Pattern.compile(sampleName);
this.sampleLabelValuePattern = StringUtils.isBlank(sampleLabelValue) ? null : Pattern.compile(sampleLabelValue);
this.filteringDisabled = StringUtils.isAllBlank(sampleName, sampleLabelValue);
public TextFormatPrometheusMetricsWriter(final String sampleName, final String sampleLabelValue) {
super(sampleName, sampleLabelValue);
}

@Override
Expand All @@ -59,13 +47,4 @@ public void write(final Collection<CollectorRegistry> registries, final OutputSt
}
}
}

private Enumeration<Collector.MetricFamilySamples> getSamples(final CollectorRegistry registry) {
final Enumeration<Collector.MetricFamilySamples> samples = registry.metricFamilySamples();
return filteringDisabled ? samples : new FilteringMetricFamilySamplesEnumeration(
samples,
sampleNamePattern,
sampleLabelValuePattern
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
* Flow Metrics Producers supported
*/
public enum FlowMetricsProducer {
PROMETHEUS("prometheus");
PROMETHEUS("prometheus"),
JSON("json");

private final String producer;

Expand Down
Loading