This is an automated email from the ASF dual-hosted git repository.
pvillard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 039382b699 NIFI-15527 Added UpdateGauge Processor to standard bundle
039382b699 is described below
commit 039382b6994e88328ff9b9d07ab43f8b3aecdf45
Author: exceptionfactory <[email protected]>
AuthorDate: Thu Jan 29 12:22:09 2026 -0600
NIFI-15527 Added UpdateGauge Processor to standard bundle
This closes #10827.
Signed-off-by: Pierre Villard <[email protected]>
---
.../nifi/processors/standard/UpdateGauge.java | 120 +++++++++++++++++++++
.../services/org.apache.nifi.processor.Processor | 1 +
.../nifi/processors/standard/TestUpdateGauge.java | 77 +++++++++++++
3 files changed, 198 insertions(+)
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateGauge.java
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateGauge.java
new file mode 100644
index 0000000000..2ef48389ee
--- /dev/null
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateGauge.java
@@ -0,0 +1,120 @@
+/*
+ * 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.processors.standard;
+
+import org.apache.nifi.annotation.behavior.DefaultRunDuration;
+import org.apache.nifi.annotation.behavior.InputRequirement;
+import org.apache.nifi.annotation.behavior.SideEffectFree;
+import org.apache.nifi.annotation.behavior.SupportsBatching;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.PropertyValue;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.processor.AbstractProcessor;
+import org.apache.nifi.processor.ProcessContext;
+import org.apache.nifi.processor.ProcessSession;
+import org.apache.nifi.processor.Relationship;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.metrics.CommitTiming;
+import org.apache.nifi.processor.util.StandardValidators;
+
+import java.util.List;
+import java.util.Set;
+
+@SideEffectFree
+@SupportsBatching(defaultDuration = DefaultRunDuration.TWENTY_FIVE_MILLIS)
+@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
+@Tags({"gauge", "metrics", "instrumentation"})
+@CapabilityDescription(
+ """
+ Record the configured value of the named Gauge for each FlowFile
processed.
+ Supports instrumentation, debugging, and troubleshooting using
Expression Language with FlowFile attributes.
+ """
+)
+public class UpdateGauge extends AbstractProcessor {
+
+ static final PropertyDescriptor GAUGE_NAME = new
PropertyDescriptor.Builder()
+ .name("Gauge Name")
+ .description("Name of the Gauge to be recorded for each FlowFile
processed")
+ .required(true)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_EXPRESSION_LANGUAGE_VALIDATOR)
+
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+ .build();
+
+ static final PropertyDescriptor GAUGE_VALUE = new
PropertyDescriptor.Builder()
+ .name("Gauge Value")
+ .description("Numeric value of the Gauge to be recorded for each
FlowFile processed")
+ .required(true)
+ .addValidator(StandardValidators.NUMBER_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_EXPRESSION_LANGUAGE_VALIDATOR)
+
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+ .build();
+
+ static final Relationship SUCCESS = new Relationship.Builder()
+ .name("success")
+ .description("FlowFile processing completed with Gauge Value
recorded")
+ .build();
+
+ private static final List<PropertyDescriptor> PROPERTY_DESCRIPTORS =
List.of(
+ GAUGE_NAME,
+ GAUGE_VALUE
+ );
+
+ private static final Set<Relationship> RELATIONSHIPS = Set.of(
+ SUCCESS
+ );
+
+ private static final double NULL_GAUGE_VALUE = 0;
+
+ @Override
+ public Set<Relationship> getRelationships() {
+ return RELATIONSHIPS;
+ }
+
+ @Override
+ protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+ return PROPERTY_DESCRIPTORS;
+ }
+
+ @Override
+ public void onTrigger(final ProcessContext context, final ProcessSession
session) throws ProcessException {
+ final FlowFile flowFile = session.get();
+ if (flowFile == null) {
+ return;
+ }
+
+ final String gaugeName =
context.getProperty(GAUGE_NAME).evaluateAttributeExpressions(flowFile).getValue();
+ final PropertyValue gaugeValueProperty =
context.getProperty(GAUGE_VALUE).evaluateAttributeExpressions(flowFile);
+ final double gaugeValue = getGaugeValue(gaugeValueProperty);
+
+ session.recordGauge(gaugeName, gaugeValue,
CommitTiming.SESSION_COMMITTED);
+
+ session.transfer(flowFile, SUCCESS);
+ }
+
+ private double getGaugeValue(final PropertyValue gaugeValueProperty) {
+ try {
+ return gaugeValueProperty.asDouble();
+ } catch (final Exception e) {
+ getLogger().warn("Failed to read Gauge Value as number", e);
+ return NULL_GAUGE_VALUE;
+ }
+ }
+}
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor
index 6d74dc3669..67a30df5bf 100644
---
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor
@@ -119,6 +119,7 @@ org.apache.nifi.processors.standard.TransformXml
org.apache.nifi.processors.standard.UnpackContent
org.apache.nifi.processors.standard.UpdateCounter
org.apache.nifi.processors.standard.UpdateDatabaseTable
+org.apache.nifi.processors.standard.UpdateGauge
org.apache.nifi.processors.standard.UpdateRecord
org.apache.nifi.processors.standard.ValidateCsv
org.apache.nifi.processors.standard.ValidateJson
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateGauge.java
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateGauge.java
new file mode 100644
index 0000000000..4f79e857bf
--- /dev/null
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateGauge.java
@@ -0,0 +1,77 @@
+/*
+ * 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.processors.standard;
+
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+class TestUpdateGauge {
+ private static final String GAUGE_NAME =
TestUpdateGauge.class.getSimpleName();
+
+ private static final double GAUGE_VALUE = 1.2345;
+
+ private static final double INVALID_GAUGE_VALUE = 0;
+
+ private final TestRunner runner =
TestRunners.newTestRunner(UpdateGauge.class);
+
+ @Test
+ void testRunRecordGauge() {
+ runner.setProperty(UpdateGauge.GAUGE_NAME, GAUGE_NAME);
+ runner.setProperty(UpdateGauge.GAUGE_VALUE,
Double.toString(GAUGE_VALUE));
+
+ assertGaugeValueRecorded(GAUGE_VALUE);
+ }
+
+ @Test
+ void testRunRecordGaugeExpressionLanguageConfigured() {
+ runner.setProperty(UpdateGauge.GAUGE_NAME,
"${literal('%s')}".formatted(GAUGE_NAME));
+ runner.setProperty(UpdateGauge.GAUGE_VALUE, "${literal(1.2345)}");
+
+ assertGaugeValueRecorded(GAUGE_VALUE);
+ }
+
+ @Test
+ void testRunRecordGaugeExpressionLanguageInvalidGaugeValue() {
+ runner.setProperty(UpdateGauge.GAUGE_NAME,
"${literal('%s')}".formatted(GAUGE_NAME));
+ runner.setProperty(UpdateGauge.GAUGE_VALUE, "${literal('')}");
+
+ assertGaugeValueRecorded(INVALID_GAUGE_VALUE);
+ }
+
+ private void assertGaugeValueRecorded(final double expectedGaugeValue) {
+ runner.enqueue(new byte[]{});
+
+ runner.run();
+
+ runner.assertAllFlowFilesTransferred(UpdateGauge.SUCCESS);
+ final List<MockFlowFile> flowFiles =
runner.getFlowFilesForRelationship(UpdateGauge.SUCCESS);
+ assertFalse(flowFiles.isEmpty());
+
+ final List<Double> gaugeValues = runner.getGaugeValues(GAUGE_NAME);
+ assertFalse(gaugeValues.isEmpty());
+
+ final Double firstGaugeValue = gaugeValues.getFirst();
+ assertEquals(expectedGaugeValue, firstGaugeValue);
+ }
+}