This is an automated email from the ASF dual-hosted git repository.
exceptionfactory 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 7c0dabb4d19 NIFI-16415 Added Dynamic Property Attributes to
UpdateGauge and UpdateCounter (#11748)
7c0dabb4d19 is described below
commit 7c0dabb4d196753c9591e801ea3d7276c43598cc
Author: boguszj <[email protected]>
AuthorDate: Tue Oct 6 18:54:05 2026 +0200
NIFI-16415 Added Dynamic Property Attributes to UpdateGauge and
UpdateCounter (#11748)
Signed-off-by: David Handermann <[email protected]>
---
.../nifi/processors/standard/UpdateCounter.java | 54 +++++++++++++++--
.../nifi/processors/standard/UpdateGauge.java | 44 +++++++++++++-
.../processors/standard/TestUpdateCounter.java | 69 +++++++++++++++++-----
.../nifi/processors/standard/TestUpdateGauge.java | 37 +++++++++++-
4 files changed, 182 insertions(+), 22 deletions(-)
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateCounter.java
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateCounter.java
index 395bb6b3443..347f315c60d 100644
---
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateCounter.java
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateCounter.java
@@ -17,6 +17,7 @@
package org.apache.nifi.processors.standard;
import org.apache.nifi.annotation.behavior.DefaultRunDuration;
+import org.apache.nifi.annotation.behavior.DynamicProperty;
import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.SideEffectFree;
import org.apache.nifi.annotation.behavior.SupportsBatching;
@@ -31,9 +32,12 @@ 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.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Set;
@SideEffectFree
@@ -41,6 +45,12 @@ import java.util.Set;
@InputRequirement(InputRequirement.Requirement.INPUT_REQUIRED)
@Tags({"counter", "debug", "instrumentation"})
@CapabilityDescription("This processor allows users to set specific counters
and key points in their flow. It is useful for debugging and basic counting
functions.")
+@DynamicProperty(
+ name = "Counter attribute name",
+ value = "Counter attribute value",
+ description = "Specifies an attribute recorded with the Counter.",
+ expressionLanguageScope = ExpressionLanguageScope.FLOWFILE_ATTRIBUTES
+)
public class UpdateCounter extends AbstractProcessor {
static final PropertyDescriptor COUNTER_NAME = new
PropertyDescriptor.Builder()
@@ -87,16 +97,31 @@ public class UpdateCounter extends AbstractProcessor {
}
@Override
- public void onTrigger(ProcessContext context, ProcessSession session)
throws ProcessException {
- FlowFile flowFile = session.get();
+ protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(final
String propertyDescriptorName) {
+ return new PropertyDescriptor.Builder()
+ .name(propertyDescriptorName)
+ .description("Value of the Counter attribute identified by the
property name. Supports Expression Language using FlowFile attributes.")
+ .required(false)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_EXPRESSION_LANGUAGE_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_KEY_PROPERTY_NAME_VALIDATOR)
+
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+ .dynamic(true)
+ .build();
+ }
+
+ @Override
+ public void onTrigger(final ProcessContext context, final ProcessSession
session) throws ProcessException {
+ final FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
-
session.adjustCounter(context.getProperty(COUNTER_NAME).evaluateAttributeExpressions(flowFile).getValue(),
-
Long.parseLong(context.getProperty(DELTA).evaluateAttributeExpressions(flowFile).getValue()),
- false
- );
+ final String counterName =
context.getProperty(COUNTER_NAME).evaluateAttributeExpressions(flowFile).getValue();
+ final long delta =
Long.parseLong(context.getProperty(DELTA).evaluateAttributeExpressions(flowFile).getValue());
+ final Map<String, String> counterAttributes =
getMetricAttributes(context, flowFile);
+
+ session.adjustCounter(counterName, delta, counterAttributes,
CommitTiming.SESSION_COMMITTED);
session.transfer(flowFile, SUCCESS);
}
@@ -105,4 +130,21 @@ public class UpdateCounter extends AbstractProcessor {
config.renameProperty("counter-name", COUNTER_NAME.getName());
config.renameProperty("delta", DELTA.getName());
}
+
+ private Map<String, String> getMetricAttributes(final ProcessContext
context, final FlowFile flowFile) {
+ final Map<String, String> metricAttributes = new HashMap<>();
+ for (final Map.Entry<PropertyDescriptor, String> entry :
context.getProperties().entrySet()) {
+ final PropertyDescriptor descriptor = entry.getKey();
+ if (!descriptor.isDynamic()) {
+ continue;
+ }
+
+ final String attributeValue =
context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue();
+ if (attributeValue != null && !attributeValue.isBlank()) {
+ metricAttributes.put(descriptor.getName(), attributeValue);
+ }
+ }
+
+ return metricAttributes;
+ }
}
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
index 2ef48389ee8..03bc3783790 100644
---
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
@@ -17,6 +17,7 @@
package org.apache.nifi.processors.standard;
import org.apache.nifi.annotation.behavior.DefaultRunDuration;
+import org.apache.nifi.annotation.behavior.DynamicProperty;
import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.SideEffectFree;
import org.apache.nifi.annotation.behavior.SupportsBatching;
@@ -34,7 +35,9 @@ import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.metrics.CommitTiming;
import org.apache.nifi.processor.util.StandardValidators;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Set;
@SideEffectFree
@@ -45,8 +48,15 @@ import java.util.Set;
"""
Record the configured value of the named Gauge for each FlowFile
processed.
Supports instrumentation, debugging, and troubleshooting using
Expression Language with FlowFile attributes.
+ Dynamic properties are recorded as attributes of the Gauge.
"""
)
+@DynamicProperty(
+ name = "Gauge attribute name",
+ value = "Gauge attribute value",
+ description = "Specifies an attribute recorded with the Gauge.",
+ expressionLanguageScope = ExpressionLanguageScope.FLOWFILE_ATTRIBUTES
+)
public class UpdateGauge extends AbstractProcessor {
static final PropertyDescriptor GAUGE_NAME = new
PropertyDescriptor.Builder()
@@ -93,6 +103,20 @@ public class UpdateGauge extends AbstractProcessor {
return PROPERTY_DESCRIPTORS;
}
+ @Override
+ protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(final
String propertyDescriptorName) {
+ return new PropertyDescriptor.Builder()
+ .name(propertyDescriptorName)
+ .description("Value of the Gauge attribute identified by the
property name. Supports Expression Language using FlowFile attributes.")
+ .required(false)
+ .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_EXPRESSION_LANGUAGE_VALIDATOR)
+
.addValidator(StandardValidators.ATTRIBUTE_KEY_PROPERTY_NAME_VALIDATOR)
+
.expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES)
+ .dynamic(true)
+ .build();
+ }
+
@Override
public void onTrigger(final ProcessContext context, final ProcessSession
session) throws ProcessException {
final FlowFile flowFile = session.get();
@@ -103,12 +127,30 @@ public class UpdateGauge extends AbstractProcessor {
final String gaugeName =
context.getProperty(GAUGE_NAME).evaluateAttributeExpressions(flowFile).getValue();
final PropertyValue gaugeValueProperty =
context.getProperty(GAUGE_VALUE).evaluateAttributeExpressions(flowFile);
final double gaugeValue = getGaugeValue(gaugeValueProperty);
+ final Map<String, String> gaugeAttributes =
getMetricAttributes(context, flowFile);
- session.recordGauge(gaugeName, gaugeValue,
CommitTiming.SESSION_COMMITTED);
+ session.recordGauge(gaugeName, gaugeValue, gaugeAttributes,
CommitTiming.SESSION_COMMITTED);
session.transfer(flowFile, SUCCESS);
}
+ private Map<String, String> getMetricAttributes(final ProcessContext
context, final FlowFile flowFile) {
+ final Map<String, String> metricAttributes = new HashMap<>();
+ for (final Map.Entry<PropertyDescriptor, String> entry :
context.getProperties().entrySet()) {
+ final PropertyDescriptor descriptor = entry.getKey();
+ if (!descriptor.isDynamic()) {
+ continue;
+ }
+
+ final String attributeValue =
context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue();
+ if (attributeValue != null && !attributeValue.isBlank()) {
+ metricAttributes.put(descriptor.getName(), attributeValue);
+ }
+ }
+
+ return metricAttributes;
+ }
+
private double getGaugeValue(final PropertyValue gaugeValueProperty) {
try {
return gaugeValueProperty.asDouble();
diff --git
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateCounter.java
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateCounter.java
index 3a5ef77218a..8eed5b1d307 100644
---
a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateCounter.java
+++
b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUpdateCounter.java
@@ -24,11 +24,28 @@ import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
-import java.util.Optional;
+import static java.util.Collections.emptyMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
public class TestUpdateCounter {
+
+ private static final String COUNTER_NAME = "firewall";
+ private static final String DELTA = "1";
+
+ private static final String FILENAME_ATTRIBUTE = "filename";
+ private static final String FILENAME_VALUE = "test";
+
+ private static final String NUM_ATTRIBUTE = "num";
+ private static final String NUM_ATTRIBUTES_VALUE = "5";
+
+ private static final String EMPTY_ATTRIBUTE = "emptyAttribute";
+ private static final String EMPTY_VALUE = "emptyValue";
+
+ private static final String LEGACY_COUNTER_NAME_PROPERTY = "counter-name";
+ private static final String LEGACY_DELTA_PROPERTY = "delta";
+
private TestRunner runner;
@BeforeEach
@@ -37,36 +54,60 @@ public class TestUpdateCounter {
}
@Test
- public void testwithFileName() {
- runner.setProperty(UpdateCounter.COUNTER_NAME, "firewall");
- runner.setProperty(UpdateCounter.DELTA, "1");
- Map<String, String> attributes = new HashMap<>();
- runner.enqueue("", attributes);
+ public void testBaseScenario() {
+ runner.setProperty(UpdateCounter.COUNTER_NAME, COUNTER_NAME);
+ runner.setProperty(UpdateCounter.DELTA, DELTA);
+
+ runner.enqueue(new byte[0], emptyMap());
runner.run();
+
+ final Long counterValue = runner.getCounterValue(COUNTER_NAME);
+ assertEquals(counterValue, Long.valueOf(DELTA));
runner.assertAllFlowFilesTransferred(UpdateCounter.SUCCESS, 1);
}
@Test
public void testExpressionLanguage() {
- runner.setProperty(UpdateCounter.COUNTER_NAME, "${filename}");
- runner.setProperty(UpdateCounter.DELTA, "${num}");
+ runner.setProperty(UpdateCounter.COUNTER_NAME,
"${%s}".formatted(FILENAME_ATTRIBUTE));
+ runner.setProperty(UpdateCounter.DELTA,
"${%s}".formatted(NUM_ATTRIBUTE));
final Map<String, String> attributes = new HashMap<>();
- attributes.put("filename", "test");
- attributes.put("num", "40");
+ attributes.put(FILENAME_ATTRIBUTE, FILENAME_VALUE);
+ attributes.put(NUM_ATTRIBUTE, NUM_ATTRIBUTES_VALUE);
runner.enqueue(new byte[0], attributes);
runner.run();
- Long counter = runner.getCounterValue("test");
- assertEquals(Optional.ofNullable(counter), Optional.of(40L));
+
+ final Long counterValue = runner.getCounterValue(FILENAME_VALUE);
+ assertEquals(counterValue, Long.valueOf(NUM_ATTRIBUTES_VALUE));
+ runner.assertAllFlowFilesTransferred(UpdateCounter.SUCCESS, 1);
+ }
+
+ @Test
+ public void testAttributes() {
+ runner.setProperty(UpdateCounter.COUNTER_NAME, COUNTER_NAME);
+ runner.setProperty(UpdateCounter.DELTA, DELTA);
+ runner.setProperty(FILENAME_ATTRIBUTE,
"${%s}".formatted(FILENAME_ATTRIBUTE));
+ runner.setProperty(EMPTY_ATTRIBUTE, "${%s}".formatted(EMPTY_VALUE));
+ runner.setProperty(NUM_ATTRIBUTE, NUM_ATTRIBUTES_VALUE);
+
+ final Map<String, String> attributes = new HashMap<>();
+ attributes.put(FILENAME_ATTRIBUTE, FILENAME_VALUE);
+
+ runner.enqueue(new byte[0], attributes);
+ runner.run();
+
+ final Long counterValue = runner.getCounterValue(COUNTER_NAME,
Map.of(FILENAME_ATTRIBUTE, FILENAME_VALUE, NUM_ATTRIBUTE,
NUM_ATTRIBUTES_VALUE));
+ assertEquals(Long.valueOf(DELTA), counterValue);
+ assertNull(runner.getCounterValue(COUNTER_NAME, Map.of()));
runner.assertAllFlowFilesTransferred(UpdateCounter.SUCCESS, 1);
}
@Test
void testMigrateProperties() {
final Map<String, String> expectedRenamed = Map.of(
- "counter-name", UpdateCounter.COUNTER_NAME.getName(),
- "delta", UpdateCounter.DELTA.getName()
+ LEGACY_COUNTER_NAME_PROPERTY,
UpdateCounter.COUNTER_NAME.getName(),
+ LEGACY_DELTA_PROPERTY, UpdateCounter.DELTA.getName()
);
final PropertyMigrationResult propertyMigrationResult =
runner.migrateProperties();
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
index 4f79e857bf3..75bda54212e 100644
---
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
@@ -21,10 +21,13 @@ import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.Test;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class TestUpdateGauge {
private static final String GAUGE_NAME =
TestUpdateGauge.class.getSimpleName();
@@ -33,6 +36,17 @@ class TestUpdateGauge {
private static final double INVALID_GAUGE_VALUE = 0;
+ private static final String VALUE_ATTRIBUTE = "value";
+
+ private static final String FILENAME_ATTRIBUTE = "filename";
+ private static final String FILENAME_VALUE = "test";
+
+ private static final String SERVICE_ATTRIBUTE = "service";
+ private static final String SERVICE_VALUE = "payments";
+
+ private static final String EMPTY_ATTRIBUTE = "emptyAttribute";
+ private static final String EMPTY_VALUE = "emptyValue";
+
private final TestRunner runner =
TestRunners.newTestRunner(UpdateGauge.class);
@Test
@@ -46,7 +60,7 @@ class TestUpdateGauge {
@Test
void testRunRecordGaugeExpressionLanguageConfigured() {
runner.setProperty(UpdateGauge.GAUGE_NAME,
"${literal('%s')}".formatted(GAUGE_NAME));
- runner.setProperty(UpdateGauge.GAUGE_VALUE, "${literal(1.2345)}");
+ runner.setProperty(UpdateGauge.GAUGE_VALUE,
"${literal(%s)}".formatted(GAUGE_VALUE));
assertGaugeValueRecorded(GAUGE_VALUE);
}
@@ -59,6 +73,27 @@ class TestUpdateGauge {
assertGaugeValueRecorded(INVALID_GAUGE_VALUE);
}
+ @Test
+ void testRunRecordGaugeAttributes() {
+ runner.setProperty(UpdateGauge.GAUGE_NAME, GAUGE_NAME);
+ runner.setProperty(UpdateGauge.GAUGE_VALUE,
"${%s}".formatted(VALUE_ATTRIBUTE));
+ runner.setProperty(FILENAME_ATTRIBUTE,
"${%s}".formatted(FILENAME_ATTRIBUTE));
+ runner.setProperty(EMPTY_ATTRIBUTE, "${%s}".formatted(EMPTY_VALUE));
+ runner.setProperty(SERVICE_ATTRIBUTE, SERVICE_VALUE);
+
+ final Map<String, String> attributes = new HashMap<>();
+ attributes.put(VALUE_ATTRIBUTE, Double.toString(GAUGE_VALUE));
+ attributes.put(FILENAME_ATTRIBUTE, FILENAME_VALUE);
+
+ runner.enqueue(new byte[0], attributes);
+ runner.run();
+
+ runner.assertAllFlowFilesTransferred(UpdateGauge.SUCCESS);
+ final Map<String, String> gaugeAttributes = Map.of(FILENAME_ATTRIBUTE,
FILENAME_VALUE, SERVICE_ATTRIBUTE, SERVICE_VALUE);
+ assertEquals(List.of(GAUGE_VALUE), runner.getGaugeValues(GAUGE_NAME,
gaugeAttributes));
+ assertTrue(runner.getGaugeValues(GAUGE_NAME, Map.of()).isEmpty());
+ }
+
private void assertGaugeValueRecorded(final double expectedGaugeValue) {
runner.enqueue(new byte[]{});