Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
121d72d
[receiver/kafkareceiver] move receiver.kafkareceiver.UseFranzGo featu…
paulojmdias Aug 21, 2025
6fb2ecf
fix: make generate
paulojmdias Aug 21, 2025
da5da50
Merge branch 'main' into feat/42155
paulojmdias Aug 21, 2025
a002b19
fix: Update kafka_receiver_test.go
paulojmdias Aug 22, 2025
f1e6eca
fiz: context.Background()
paulojmdias Aug 22, 2025
09014ff
chore: //nolint:usetesting
paulojmdias Aug 22, 2025
a4131e8
Merge branch 'main' into feat/42155
paulojmdias Aug 22, 2025
f56fea2
chore: update readme.md
paulojmdias Aug 25, 2025
be8ae76
feat: test and consumer improvements
paulojmdias Aug 26, 2025
060d629
Merge branch 'main' into feat/42155
paulojmdias Aug 26, 2025
54afbbc
fix: improve tests and revert shutdown ignore cancellation
paulojmdias Aug 27, 2025
e5ddaf7
Merge branch 'main' into feat/42155
paulojmdias Aug 27, 2025
7b7c607
feat: add support for profiles in tests
paulojmdias Aug 27, 2025
acd16ed
Merge branch 'main' into feat/42155
paulojmdias Sep 2, 2025
0ef7943
Merge branch 'main' into feat/42155
paulojmdias Sep 25, 2025
4d81c8d
Merge branch 'main' into feat/42155
paulojmdias Sep 25, 2025
566350f
fix: fix tests
paulojmdias Sep 25, 2025
aacea05
fix: improvements on signals
paulojmdias Sep 25, 2025
2c468ed
fix: improve test for CI stability
paulojmdias Sep 25, 2025
bda6657
Merge branch 'main' into feat/42155
paulojmdias Sep 25, 2025
f9a0d04
Merge branch 'main' into feat/42155
paulojmdias Sep 25, 2025
c33bdd0
Merge branch 'main' into feat/42155
paulojmdias Sep 25, 2025
de4d9a1
Update consumer_franz_test.go
paulojmdias Sep 25, 2025
0a21c09
Merge branch 'main' into feat/42155
paulojmdias Sep 26, 2025
de5a034
Merge branch 'main' into feat/42155
paulojmdias Sep 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/receiver_kafka_franz_go_beta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: "enhancement"

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: receiver/kafkareceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Use franz-go client for Kafka receiver as default, promoting the receiver.kafkareceiver.UseFranzGo feature gate to Beta."

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [42155]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 1 addition & 1 deletion receiver/kafkareceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If used in conjunction with the `kafkaexporter` configured with `include_metadat
## Getting Started

> [!NOTE]
> You can opt-in to use [`franz-go`](https://github.com/twmb/franz-go) client by enabling the feature gate
> You can opt out of using the [`franz-go`](https://github.com/twmb/franz-go) client by disabling the feature gate
> `receiver.kafkareceiver.UseFranzGo` when you run the OpenTelemetry Collector. See the following page
> for more details: [Feature Gates](https://github.com/open-telemetry/opentelemetry-collector/tree/main/featuregate#controlling-gates)
>
Expand Down
58 changes: 54 additions & 4 deletions receiver/kafkareceiver/consumer_franz.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/twmb/franz-go/pkg/kgo"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componentstatus"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/receiver"
Expand All @@ -34,7 +35,7 @@ const franzGoConsumerFeatureGateName = "receiver.kafkareceiver.UseFranzGo"
// the Kafka receiver will use the franz-go client, which is more performant and has
// better support for modern Kafka features.
var franzGoConsumerFeatureGate = featuregate.GlobalRegistry().MustRegister(
franzGoConsumerFeatureGateName, featuregate.StageAlpha,
franzGoConsumerFeatureGateName, featuregate.StageBeta,
featuregate.WithRegisterDescription("When enabled, the Kafka receiver will use the franz-go client to consume messages."),
featuregate.WithRegisterFromVersion("v0.129.0"),
)
Expand Down Expand Up @@ -63,6 +64,11 @@ type franzConsumer struct {
client *kgo.Client
obsrecv *receiverhelper.ObsReport
assignments map[topicPartition]*pc

// ---- status reporting (parity with Sarama) ----
host component.Host
stoppingOnce sync.Once
stoppedOnce sync.Once
}

// pc represents the partition consumer shared information.
Expand Down Expand Up @@ -137,6 +143,22 @@ func newFranzKafkaConsumer(
}, nil
}

// reportStatus emits a component status event if we have a host.
func (c *franzConsumer) reportStatus(s componentstatus.Status) {
if c.host == nil {
return
}
componentstatus.ReportStatus(c.host, componentstatus.NewEvent(s))
}

// reportRecoverable reports a recoverable error status event.
func (c *franzConsumer) reportRecoverable(err error) {
if c.host == nil || err == nil {
return
}
componentstatus.ReportStatus(c.host, componentstatus.NewRecoverableErrorEvent(err))
}

func (c *franzConsumer) Start(ctx context.Context, host component.Host) error {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -149,6 +171,10 @@ func (c *franzConsumer) Start(ctx context.Context, host component.Host) error {
close(c.started)
}

// Parity with Sarama: report "Starting" as soon as Start() is called.
c.host = host
c.reportStatus(componentstatus.StatusStarting)

obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
ReceiverID: c.settings.ID,
Transport: transport,
Expand Down Expand Up @@ -205,7 +231,11 @@ func (c *franzConsumer) Start(ctx context.Context, host component.Host) error {
}

func (c *franzConsumer) consumeLoop(ctx context.Context) {
defer close(c.consumerClosed)
// Parity with Sarama: when the loop exits, report Stopped.
defer func() {
c.stoppedOnce.Do(func() { c.reportStatus(componentstatus.StatusStopped) })
close(c.consumerClosed)
}()

for {
// Consume messages until the ctx is cancelled (the client is closed).
Expand Down Expand Up @@ -237,6 +267,8 @@ func (c *franzConsumer) consume(ctx context.Context, size int) bool {
zap.String("topic", topic),
zap.Int64("partition", int64(partition)),
)
// Parity with Sarama: report recoverable error while consuming.
c.reportRecoverable(err)
if !hasError {
hasError = true
}
Expand Down Expand Up @@ -354,21 +386,31 @@ func (c *franzConsumer) consume(ctx context.Context, size int) bool {
if !c.config.AutoCommit.Enable {
if err := c.client.CommitMarkedOffsets(ctx); err != nil {
c.settings.Logger.Error("failed to commit offsets", zap.Error(err))
// Surface as recoverable error (parity with Sarama’s loop error reporting).
c.reportRecoverable(err)
}
}
return true
}

func (c *franzConsumer) Shutdown(ctx context.Context) error {
// Parity with Sarama: report Stopping at shutdown start.
c.stoppingOnce.Do(func() { c.reportStatus(componentstatus.StatusStopping) })

if !c.triggerShutdown() {
return errors.New("kafka consumer: consumer isn't running")
// Idempotent: never fail if not started.
// We still want to ensure Stopped is eventually emitted (consumeLoop defer handles it).
// However, if the loop was never started, emit Stopped here too.
c.stoppedOnce.Do(func() { c.reportStatus(componentstatus.StatusStopped) })
return nil
}

select {
case <-ctx.Done():
return context.Cause(ctx)
case <-c.consumerClosed:
}

return nil
}

Expand All @@ -387,18 +429,24 @@ func (c *franzConsumer) triggerShutdown() bool {
return true
default:
close(c.closing)
client := c.client
c.mu.Unlock()
// Close the client without holding the write mutex, otherwise, the
// Shutdown will deadlock when `franzConsumer` inevitably calls the
// lost/assigned callback.
c.client.Close()
if client != nil {
client.Close()
}
}
return true
}

// assigned must be set as kgo.OnPartitionsAssigned callback. Ensuring all
// assigned partitions to this consumer process received records.
func (c *franzConsumer) assigned(ctx context.Context, _ *kgo.Client, assigned map[string][]int32) {
// Report OK on each successful assignment so we can recover status after transient errors.
c.reportStatus(componentstatus.StatusOK)

c.mu.Lock()
defer c.mu.Unlock()
for topic, partitions := range assigned {
Expand Down Expand Up @@ -468,6 +516,8 @@ func (c *franzConsumer) lost(ctx context.Context, _ *kgo.Client,
// away from this consumer.
if err := c.client.CommitMarkedOffsets(ctx); err != nil {
c.settings.Logger.Error("failed to commit marked offsets", zap.Error(err))
// Parity with Sarama: report recoverable error on commit errors.
c.reportRecoverable(err)
}
}

Expand Down
14 changes: 12 additions & 2 deletions receiver/kafkareceiver/consumer_franz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ func TestConsumerShutdownConsuming(t *testing.T) {
consumer, e := newFranzKafkaConsumer(cfg, settings, []string{topic}, consumeFn)
require.NoError(tb, e)
require.NoError(tb, consumer.Start(ctx, componenttest.NewNopHost()))
// Wait until the group has assigned at least one partition.
require.Eventually(tb, func() bool {
consumer.mu.RLock()
n := len(consumer.assignments)
consumer.mu.RUnlock()
return n > 0
}, 10*time.Second, 10*time.Millisecond)
require.NoError(tb, kafkaClient.ProduceSync(ctx, rs...).FirstErr())

select {
Expand Down Expand Up @@ -239,9 +246,12 @@ func TestConsumerShutdownNotStarted(t *testing.T) {
require.NoError(t, err)

for range 2 {
require.EqualError(t, c.Shutdown(t.Context()),
"kafka consumer: consumer isn't running")
require.NoError(t, c.Shutdown(t.Context()))
}

// Verify internal signal that there's nothing to shut down.
// (Same package, so we can call the unexported helper.)
require.False(t, c.triggerShutdown(), "triggerShutdown should indicate no-op when never started")
}

// TestRaceLostVsConsume verifies no data race occurs between concurrent
Expand Down
Loading
Loading