Skip to content
Open
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
91 changes: 61 additions & 30 deletions pkg/outputs/kafka_output/kafka_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,33 @@ type dynConfig struct {

// config //
type config struct {
Address string `mapstructure:"address,omitempty"`
Topic string `mapstructure:"topic,omitempty"`
TopicPrefix string `mapstructure:"topic-prefix,omitempty"`
Name string `mapstructure:"name,omitempty"`
SASL *types.SASL `mapstructure:"sasl,omitempty"`
TLS *types.TLSConfig `mapstructure:"tls,omitempty"`
MaxRetry int `mapstructure:"max-retry,omitempty"`
Timeout time.Duration `mapstructure:"timeout,omitempty"`
RecoveryWaitTime time.Duration `mapstructure:"recovery-wait-time,omitempty"`
FlushFrequency time.Duration `mapstructure:"flush-frequency,omitempty"`
SyncProducer bool `mapstructure:"sync-producer,omitempty"`
RequiredAcks string `mapstructure:"required-acks,omitempty"`
Format string `mapstructure:"format,omitempty"`
InsertKey bool `mapstructure:"insert-key,omitempty"`
AddTarget string `mapstructure:"add-target,omitempty"`
TargetTemplate string `mapstructure:"target-template,omitempty"`
MsgTemplate string `mapstructure:"msg-template,omitempty"`
SplitEvents bool `mapstructure:"split-events,omitempty"`
NumWorkers int `mapstructure:"num-workers,omitempty"`
CompressionCodec string `mapstructure:"compression-codec,omitempty"`
KafkaVersion string `mapstructure:"kafka-version,omitempty"`
Debug bool `mapstructure:"debug,omitempty"`
BufferSize int `mapstructure:"buffer-size,omitempty"`
OverrideTimestamps bool `mapstructure:"override-timestamps,omitempty"`
EnableMetrics bool `mapstructure:"enable-metrics,omitempty"`
EventProcessors []string `mapstructure:"event-processors,omitempty"`
Address string `mapstructure:"address,omitempty"`
Topic string `mapstructure:"topic,omitempty"`
TopicPrefix string `mapstructure:"topic-prefix,omitempty"`
Name string `mapstructure:"name,omitempty"`
SASL *types.SASL `mapstructure:"sasl,omitempty"`
TLS *types.TLSConfig `mapstructure:"tls,omitempty"`
MaxRetry int `mapstructure:"max-retry,omitempty"`
Timeout time.Duration `mapstructure:"timeout,omitempty"`
RecoveryWaitTime time.Duration `mapstructure:"recovery-wait-time,omitempty"`
FlushFrequency time.Duration `mapstructure:"flush-frequency,omitempty"`
SyncProducer bool `mapstructure:"sync-producer,omitempty"`
RequiredAcks string `mapstructure:"required-acks,omitempty"`
Format string `mapstructure:"format,omitempty"`
InsertKey bool `mapstructure:"insert-key,omitempty"`
AddTarget string `mapstructure:"add-target,omitempty"`
TargetTemplate string `mapstructure:"target-template,omitempty"`
MsgTemplate string `mapstructure:"msg-template,omitempty"`
SplitEvents bool `mapstructure:"split-events,omitempty"`
NumWorkers int `mapstructure:"num-workers,omitempty"`
CompressionCodec string `mapstructure:"compression-codec,omitempty"`
KafkaVersion string `mapstructure:"kafka-version,omitempty"`
Debug bool `mapstructure:"debug,omitempty"`
BufferSize int `mapstructure:"buffer-size,omitempty"`
OverrideTimestamps bool `mapstructure:"override-timestamps,omitempty"`
EnableMetrics bool `mapstructure:"enable-metrics,omitempty"`
EventProcessors []string `mapstructure:"event-processors,omitempty"`
AddHeaders map[string]string `mapstructure:"add-headers,omitempty"`
}

func (c *config) LogValue() slog.Value {
Expand Down Expand Up @@ -612,10 +613,25 @@ CRPROD:
}
}

var headers []sarama.RecordHeader
for k, v := range cfg.AddHeaders {
headers = append(headers, sarama.RecordHeader{
Key: []byte(k),
Value: []byte(v),
})
}

headers = append(headers, sarama.RecordHeader{
Key: []byte("sub"),
Value: []byte(m.GetMeta()["subscription-name"]),
})

topic := k.selectTopic(m.GetMeta())
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(b),
Topic: topic,
Value: sarama.ByteEncoder(b),
Headers: headers,
Timestamp: time.Now(),
}
if cfg.InsertKey {
msg.Key = sarama.ByteEncoder(k.partitionKey(m.GetMeta()))
Expand Down Expand Up @@ -688,10 +704,25 @@ CRPROD:
}
}

var headers []sarama.RecordHeader
for k, v := range cfg.AddHeaders {
headers = append(headers, sarama.RecordHeader{
Key: []byte(k),
Value: []byte(v),
})
}
Comment on lines +708 to +713

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Duplicated code in sync and async producers. Good to create a helper and call in both locations.
Static headers don't need to be computed for every message. Compute once, store as part of dynConfig.


headers = append(headers, sarama.RecordHeader{
Key: []byte("sub"),
Value: []byte(m.GetMeta()["subscription-name"]),
})
Comment on lines +715 to +718

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is added unconditionally, either remove it or gate it behind a config knob.


topic := k.selectTopic(m.GetMeta())
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(b),
Topic: topic,
Value: sarama.ByteEncoder(b),
Headers: headers,
Timestamp: time.Now(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Timestamp is not required, it is broker assigned and unrelated to the stated PR scope.
Create another PR to add it if required (behind a config knob)

}
if cfg.InsertKey {
msg.Key = sarama.ByteEncoder(k.partitionKey(m.GetMeta()))
Expand Down
Loading