Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better protect against nil topicAttributeFunc #618

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions kafka/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/twmb/franz-go/pkg/sasl/plain"
"github.com/twmb/franz-go/plugin/kzap"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -247,6 +248,12 @@ func (cfg *CommonConfig) finalize() error {
if cfg.TopicLogFieldFunc != nil {
cfg.TopicLogFieldFunc = topicFieldFunc(cfg.TopicLogFieldFunc)
}
if cfg.TopicAttributeFunc == nil {
cfg.TopicAttributeFunc = func(topic string) attribute.KeyValue {
return attribute.KeyValue{}
}
}

return errors.Join(errs...)
}

Expand Down
1 change: 1 addition & 0 deletions kafka/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestCommonConfig(t *testing.T) {
t.Helper()
err := in.finalize()
require.NoError(t, err)
in.TopicAttributeFunc = nil
in.TopicLogFieldFunc = nil
in.hooks = nil
assert.Equal(t, expected, in)
Expand Down
36 changes: 16 additions & 20 deletions kafka/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,7 @@ func TestConsumerConfigFinalizer(t *testing.T) {
}
err := cfg.finalize()
require.NoError(t, err)
assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assertNotNilOptions(t, &cfg)

assert.Equal(t, ConsumerConfig{
CommonConfig: CommonConfig{Brokers: []string{"localhost:9092"}},
Expand All @@ -805,10 +802,7 @@ func TestConsumerConfigFinalizer(t *testing.T) {
}
err := cfg.finalize()
require.NoError(t, err)
assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assertNotNilOptions(t, &cfg)

assert.Equal(t, ConsumerConfig{
CommonConfig: CommonConfig{Brokers: []string{"localhost:9092"}},
Expand All @@ -830,10 +824,7 @@ func TestConsumerConfigFinalizer(t *testing.T) {
}
err := cfg.finalize()
require.NoError(t, err)
assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assertNotNilOptions(t, &cfg)

assert.Equal(t, ConsumerConfig{
CommonConfig: CommonConfig{Brokers: []string{"localhost:9092"}},
Expand All @@ -855,10 +846,7 @@ func TestConsumerConfigFinalizer(t *testing.T) {
}
err := cfg.finalize()
require.NoError(t, err)
assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assertNotNilOptions(t, &cfg)

assert.Equal(t, ConsumerConfig{
CommonConfig: CommonConfig{Brokers: []string{"localhost:9092"}},
Expand All @@ -880,10 +868,7 @@ func TestConsumerConfigFinalizer(t *testing.T) {
}
err := cfg.finalize()
require.NoError(t, err)
assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assertNotNilOptions(t, &cfg)

assert.Equal(t, ConsumerConfig{
CommonConfig: CommonConfig{Brokers: []string{"localhost:9092"}},
Expand All @@ -909,6 +894,17 @@ func TestConsumerConfigFinalizer(t *testing.T) {
})
}

func assertNotNilOptions(t testing.TB, cfg *ConsumerConfig) {
t.Helper()

assert.NotNil(t, cfg.Processor)
cfg.Processor = nil
assert.NotNil(t, cfg.Logger)
cfg.Logger = nil
assert.NotNil(t, cfg.TopicAttributeFunc)
cfg.TopicAttributeFunc = nil
}

func newConsumer(t testing.TB, cfg ConsumerConfig) *Consumer {
if cfg.MaxPollWait <= 0 {
// Lower MaxPollWait, ShutdownGracePeriod to speed up execution.
Expand Down
6 changes: 0 additions & 6 deletions kafka/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,6 @@ func newKgoHooks(mp metric.MeterProvider, namespace, topicPrefix string,
return nil, formatMetricError(throttlingDurationKey, err)
}

if topicAttributeFunc == nil {
topicAttributeFunc = func(topic string) attribute.KeyValue {
return attribute.KeyValue{}
}
}

return &metricHooks{
namespace: namespace,
topicPrefix: topicPrefix,
Expand Down
Loading