Skip to content

feat(outputs.kafka): Handle partial writes without retrying delivered metrics - #19964

Open
kVinsom wants to merge 5 commits into
influxdata:masterfrom
kVinsom:fix/10864-kafka-partial-writes
Open

kVinsom wants to merge 5 commits into
influxdata:masterfrom
kVinsom:fix/10864-kafka-partial-writes

Conversation

@kVinsom

@kVinsom kVinsom commented Oct 22, 2026 •

Copy link
Copy Markdown

Summary

When only part of a Kafka batch fails, retrying the entire batch duplicates messages that were already delivered.
Conversely, treating one permanent error as success for the entire batch can discard unrelated failed messages.

Return internal.PartialWriteError with the original metric indices: acknowledge successful deliveries, reject individual permanent failures, and retain transient failures for retry.
The indices remain correct when serialization skips metrics or Sarama reports errors out of order.
An error without per-message delivery results retains every submitted metric because its delivery status is unknown.

Permanent failures include serialization errors, broker size/timestamp errors, and per-message sarama.ConfigurationError values, including wrapped errors.
Sarama uses the latter for messages exceeding the local max_message_bytes limit and headers unsupported by the configured Kafka version.
These metrics are rejected individually instead of being retried on every flush.

Following review, this PR contains no new configuration option: topic_error_behavior, drop-mode tests, and the associated sample configuration/documentation have been removed for a separate feature change.
Unknown-topic-or-partition errors retain the existing retry behavior because they can be temporary.
A batch filled entirely with unavailable-topic messages can still block newer metrics until the topic or partition recovers; this PR does not introduce an opt-in data-dropping policy.

Regression coverage includes mixed permanent/transient errors, wrapped errors, original indices across serialization gaps, and unclassified batch errors.
Tests using a real Sarama producer against MockBroker exercise Telegraf's buffer and delivery tracking, including oversized messages in mixed/full batches, unsupported headers, later-batch progress, and recovery of a temporarily missing topic.

No dependency, delivery-timeout, or build-configuration changes are included.
#19735 addresses stalled deliveries separately; both changes touch Write() and may need reconciliation depending on merge order.

Validation

  • All three new real-producer configuration-error scenarios failed against the previous PR code and pass after the fix.
  • Windows/Go 1.27.0: go test -short ./plugins/outputs/kafka ./plugins/common/kafka ./models -count=1 -timeout=180s passes.
  • Linux/WSL/Go 1.27.0: go test -race -short ./plugins/outputs/kafka ./plugins/common/kafka ./models -count=1 -timeout=180s passes. The mock broker starts only after plugin initialization to avoid racing Sarama's global logger setup.
  • go vet ./plugins/outputs/kafka ./plugins/common/kafka ./models passes.
  • Project-pinned golangci-lint v2.15.0 on ./plugins/outputs/kafka/...: 0 issues.
  • gofmt, Markdown lint, the project's README linter, and git diff --check pass.
    The Kafka README was regenerated using the project's config includer.
  • The Docker-backed Kafka integration test was not run because the Docker daemon is unavailable.
    The real-producer MockBroker tests ran without Docker.
    The repository-wide test suite was not run locally; fresh CI is required for this revision.

Checklist

AI-assisted implementation and validation using OpenAI Codex.

Related issues

Part of #10864.

Review follow-up (4 November 2026)

Merged the configuration-error regression tests into kafka_test.go and combined the plugin configuration into one struct initializer, as requested. The logger is still initialized before starting the mock broker.

Merged current master (f6d5b919a). The Kafka output, common Kafka code and models pass Linux race tests; the project-pinned Kafka linter reports zero issues. The Docker-backed integration test remains unrun locally.

…metrics

Track per-message delivery results using PartialWriteError. Keep transient failures for retry and add an opt-in drop policy for unknown topics or partitions. Cover mixed failures, serialization gaps, buffer draining and topic recovery.

Co-Authored-By: OpenAI Codex (GPT-6) <noreply@openai.com>
@telegraf-tiger

Copy link
Copy Markdown
Contributor

Thanks so much for the pull request!
🤝 ✒️ Just a reminder that the CLA has not yet been signed, and we'll need it before merging. Please sign the CLA when you get a chance, then post a comment here saying !signed-cla

@telegraf-tiger telegraf-tiger Bot added area/kafka fix pr to fix corresponding bug plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels Oct 22, 2026
@kVinsom

kVinsom commented Oct 22, 2026

Copy link
Copy Markdown
Author

!signed-cla

Comment thread plugins/outputs/kafka/kafka.go Resolved
Comment thread plugins/outputs/kafka/kafka.go Resolved
@skartikey skartikey self-assigned this Oct 29, 2026

@skartikey skartikey left a comment

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.

@kVinsom Thanks, the client-side size and header case is covered now.
I checked sarama v1.61.1 only returns a per-message ConfigurationError for those two, so rejecting it is safe, and the MockBroker tests show the buffer draining past a full batch of them.

@skartikey skartikey assigned srebhan and unassigned skartikey Nov 2, 2026
@skartikey skartikey added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Nov 2, 2026

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution @kVinsom! Two small comments from my side. I also changed this PR to "feature" as this is how we treated it on other plugins (and because it's a feature actually). ;-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please merge this file into kafka_test.go!

Comment on lines +53 to +62
plugin := &Kafka{
Topic: "existing",
Headers: tt.headers,
Log: testutil.Logger{},
producerFunc: sarama.NewSyncProducer,
serializer: serializer,
}
plugin.Version = tt.version
plugin.MaxMessageBytes = 256
plugin.RequiredAcks = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please merge the two blocks!

@srebhan srebhan changed the title fix(outputs.kafka): handle partial writes without retrying delivered metrics feat(outputs.kafka): Handle partial writes without retrying delivered metrics Nov 2, 2026
@telegraf-tiger telegraf-tiger Bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Nov 2, 2026
@srebhan srebhan added waiting for response waiting for response from contributor and removed fix pr to fix corresponding bug labels Nov 2, 2026
@telegraf-tiger

telegraf-tiger Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

⚠️ This pull request increases the Telegraf binary size by 6.82 % for linux amd64 (new size: 329.3 MB, nightly size 308.2 MB)

📦 Click here to get additional PR build artifacts

Artifact URLs

. DEB . RPM . TAR . GZ . ZIP
amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip
arm64.deb armel.rpm darwin_arm64.tar.gz windows_arm64.zip
armel.deb armv6hl.rpm freebsd_amd64.tar.gz windows_i386.zip
armhf.deb i386.rpm freebsd_armv7.tar.gz
i386.deb ppc64le.rpm freebsd_i386.tar.gz
mips.deb riscv64.rpm linux_amd64.tar.gz
mipsel.deb s390x.rpm linux_arm64.tar.gz
ppc64el.deb x86_64.rpm linux_armel.tar.gz
riscv64.deb linux_armhf.tar.gz
s390x.deb linux_i386.tar.gz
linux_mips.tar.gz
linux_mipsel.tar.gz
linux_ppc64le.tar.gz
linux_riscv64.tar.gz
linux_s390x.tar.gz

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

Labels

area/kafka feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. waiting for response waiting for response from contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants