Skip to content

Commit

Permalink
fix: better protect against nil topicAttributeFunc (#618)
Browse files Browse the repository at this point in the history
## ❓ Why is this being changed

systemtests are failing because of a nil topic attribute func. The topic
manager does not handle nil func.

See
https://github.com/elastic/apm-queue/actions/runs/12741942829/job/35509307227
 
## 🧑‍💻 What is being changed

set the default func in the common config instead of the metrics hook
 
## ✅ How to validate the change

run systemtests
  • Loading branch information
kruskall authored Jan 16, 2025
1 parent 5b48d80 commit 1822363
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
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

0 comments on commit 1822363

Please sign in to comment.