Skip to content

Add metrics for Kafka offset commit failures#38613

Open
Kriti-dev07 wants to merge 3 commits into
apache:masterfrom
Kriti-dev07:add-kafka-offset-metrics
Open

Add metrics for Kafka offset commit failures#38613
Kriti-dev07 wants to merge 3 commits into
apache:masterfrom
Kriti-dev07:add-kafka-offset-metrics

Conversation

@Kriti-dev07
Copy link
Copy Markdown

Summary

Added Beam metrics in KafkaCommitOffset.CommitOffsetDoFn to improve visibility into offset commit failures.

Added metrics

  • commit-failures
  • retries-exhausted

These counters help monitor failed offset commits through existing Beam monitoring dashboards.

Retry-attempt metrics can be added once retry logic is implemented.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the monitoring capabilities of the Kafka IO connector by adding specific metrics to track offset commit failures. By integrating Beam's counter metrics, it provides better visibility into the health of Kafka offset commits, facilitating easier debugging and alerting for production pipelines.

Highlights

  • Added Beam Metrics: Introduced 'commit-failures' and 'retries-exhausted' counters to the KafkaCommitOffset.CommitOffsetDoFn class to track offset commit issues.
  • Improved Observability: These metrics allow users to monitor Kafka offset commit failures directly through existing Beam monitoring dashboards.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces metrics to track Kafka offset commit failures within the KafkaCommitOffset class. The feedback suggests removing the retriesExhausted metric and its associated increments, as no retry logic is currently implemented, making the metric redundant. Additionally, the reviewer recommends removing the excessive vertical whitespace introduced throughout the file to maintain consistency with the project's coding style and improve readability.

Comment on lines +68 to +73
private final Counter commitFailures =
Metrics.counter(CommitOffsetDoFn.class, "commit-failures");

private final Counter retriesExhausted =
Metrics.counter(CommitOffsetDoFn.class, "retries-exhausted");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The retriesExhausted metric is currently redundant with commitFailures because no retry logic is implemented yet. It is better to introduce this metric when retries are actually added. Additionally, there is an excessive amount of vertical whitespace being introduced throughout the file which deviates from the existing code style.

Suggested change
private final Counter commitFailures =
Metrics.counter(CommitOffsetDoFn.class, "commit-failures");
private final Counter retriesExhausted =
Metrics.counter(CommitOffsetDoFn.class, "retries-exhausted");
private final Counter commitFailures =
Metrics.counter(CommitOffsetDoFn.class, "commit-failures");

Comment on lines +89 to 109

Map<String, Object> updatedConsumerConfig =
overrideBootstrapServersConfig(consumerConfig, element.getKey());

try (Consumer<byte[], byte[]> consumer = consumerFactoryFn.apply(updatedConsumerConfig)) {

try {
consumer.commitSync(
Collections.singletonMap(
element.getKey().getTopicPartition(),
new OffsetAndMetadata(element.getValue() + 1)));

} catch (Exception e) {
// TODO: consider retrying.

commitFailures.inc();
retriesExhausted.inc();

// TODO: consider retrying and increment retry-attempt metrics.
LOG.warn("Getting exception when committing offset: {}", e.getMessage());
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Removing the redundant metric increment and cleaning up the excessive vertical whitespace to improve code density and readability.

      Map<String, Object> updatedConsumerConfig =
          overrideBootstrapServersConfig(consumerConfig, element.getKey());
      try (Consumer<byte[], byte[]> consumer = consumerFactoryFn.apply(updatedConsumerConfig)) {
        try {
          consumer.commitSync(
              Collections.singletonMap(
                  element.getKey().getTopicPartition(),
                  new OffsetAndMetadata(element.getValue() + 1)));
        } catch (Exception e) {
          commitFailures.inc();
          // TODO: consider retrying and increment retry-attempt metrics.
          LOG.warn("Getting exception when committing offset: {}", e.getMessage());
        }
      }

Comment on lines +114 to +128

checkState(
currentConfig.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG)
|| description.getBootStrapServers() != null);

Map<String, Object> config = new HashMap<>(currentConfig);

if (description.getBootStrapServers() != null
&& !description.getBootStrapServers().isEmpty()) {

config.put(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
String.join(",", description.getBootStrapServers()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Removing unnecessary blank lines to maintain consistency with the project's coding style.

      checkState(
          currentConfig.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG)
              || description.getBootStrapServers() != null);
      Map<String, Object> config = new HashMap<>(currentConfig);
      if (description.getBootStrapServers() != null
          && !description.getBootStrapServers().isEmpty()) {
        config.put(
            ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
            String.join(",", description.getBootStrapServers()));
      }

Comment on lines +198 to +217

try {

PCollection<KV<KafkaSourceDescriptor, Long>> offsets;

if (use259implementation) {

offsets =
input.apply(
MapElements.into(new TypeDescriptor<KV<KafkaSourceDescriptor, Long>>() {})
.via(element -> KV.of(element.getKey(), element.getValue().getOffset())));

} else {

// Reduce the amount of data to combine by calculating a max within the generally dense
// bundles of reading
// from a Kafka partition.
offsets = input.apply(ParDo.of(new MaxOffsetFn<>()));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Cleaning up excessive whitespace in the expand method to improve readability.

    try {
      PCollection<KV<KafkaSourceDescriptor, Long>> offsets;
      if (use259implementation) {
        offsets =
            input.apply(
                MapElements.into(new TypeDescriptor<KV<KafkaSourceDescriptor, Long>>() {})
                    .via(element -> KV.of(element.getKey(), element.getValue().getOffset())));
      } else {
        // Reduce the amount of data to combine by calculating a max within the generally dense
        // bundles of reading
        // from a Kafka partition.
        offsets = input.apply(ParDo.of(new MaxOffsetFn<>()));
      }

@codecov
Copy link
Copy Markdown

codecov Bot commented May 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.07%. Comparing base (0c9b272) to head (6d2afca).
⚠️ Report is 17 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #38613   +/-   ##
=========================================
  Coverage     58.07%   58.07%           
- Complexity    12941    12945    +4     
=========================================
  Files          2511     2511           
  Lines        262223   262265   +42     
  Branches      10652    10655    +3     
=========================================
+ Hits         152282   152308   +26     
- Misses       104243   104258   +15     
- Partials       5698     5699    +1     
Flag Coverage Δ
java 64.17% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @Abacn for label java.
R: @Dippatel98 for label kafka.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

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.

1 participant