Skip to content

NIFI-15644 Added ConsumeKafka consumer lag gauge record#10934

Closed
dariuszseweryn wants to merge 1 commit intoapache:mainfrom
dariuszseweryn:NIFI-15644
Closed

NIFI-15644 Added ConsumeKafka consumer lag gauge record#10934
dariuszseweryn wants to merge 1 commit intoapache:mainfrom
dariuszseweryn:NIFI-15644

Conversation

@dariuszseweryn
Copy link
Contributor

@dariuszseweryn dariuszseweryn commented Feb 25, 2026

Summary

NIFI-15644

As of NIFI-15545, the KafkaConsumerService exposes a currentLag(TopicPartitionSummary) method that returns the current consumer lag (in records) for a given topic-partition. However, this information is not currently utilized by the ConsumeKafka processor.

This ticket adds gauge metric recording to ConsumeKafka so that per-partition consumer lag is reported through the NiFi metrics framework via ProcessSession.recordGauge(). This enables operators to monitor consumer lag through any configured ComponentMetricReporter (e.g., OpenTelemetry).

Changes:

  • OffsetTracker: Added getTrackedPartitions() method to expose the set of TopicPartitionSummary keys accumulated during consumption.
  • ConsumeKafka: After the poll loop completes and before the session is committed, the processor queries KafkaConsumerService.currentLag() for each tracked topic-partition and records a gauge with CommitTiming.SESSION_COMMITTED.

Gauge naming format:

consumer.lag[topic="",partition=""]
For example: consumer.lag[topic="orders",partition="3"]

Behavior:

One gauge is recorded per topic-partition that was consumed during the onTrigger invocation.
Gauges are only emitted when currentLag returns a value (it may be unavailable in some Kafka client states).
Gauges use SESSION_COMMITTED timing, meaning they are only reported when the session commits successfully.
No gauges are recorded when no records are consumed (the processor returns early before reaching the gauge recording path).

Testing:

Unit tests added for OffsetTracker.getTrackedPartitions() covering empty, single, multi-partition, and deduplication scenarios.
Integration-style tests added to ConsumeKafkaTest using mocked KafkaConsumerService verifying gauge recording for single partition, multiple partitions, unavailable lag, and no-records-consumed cases.

Additional fix:

MockProcessSession did not clear gauges recorded with SESSION_COMMITTED on Session commit.

Tracking

Please complete the following tracking steps prior to pull request creation.

Issue Tracking

Pull Request Tracking

  • Pull Request title starts with Apache NiFi Jira issue number, such as NIFI-00000
  • Pull Request commit message starts with Apache NiFi Jira issue number, as such NIFI-00000
  • Pull request contains commits signed with a registered key indicating Verified status

Pull Request Formatting

  • Pull Request based on current revision of the main branch
  • Pull Request refers to a feature branch with one commit containing changes

Verification

Please indicate the verification steps performed prior to pull request creation.

Build

  • Build completed using ./mvnw clean install -P contrib-check
    • JDK 21
    • JDK 25

Licensing

  • (no new) New dependencies are compatible with the Apache License 2.0 according to the License Policy
  • (no new) New dependencies are documented in applicable LICENSE and NOTICE files

Documentation

  • (no changes) Documentation formatting appears as expected in rendered files

Copy link
Contributor

@exceptionfactory exceptionfactory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dariuszseweryn This pull request appears to overlap with the current pull request at add consumer lag tracking in #10880.

@exceptionfactory
Copy link
Contributor

Closing in favor of #10880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants