-
Notifications
You must be signed in to change notification settings - Fork 107
feat(output): add custom headers support to kafka #898
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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())) | ||
|
|
@@ -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), | ||
| }) | ||
| } | ||
|
|
||
| headers = append(headers, sarama.RecordHeader{ | ||
| Key: []byte("sub"), | ||
| Value: []byte(m.GetMeta()["subscription-name"]), | ||
| }) | ||
|
Comment on lines
+715
to
+718
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
| if cfg.InsertKey { | ||
| msg.Key = sarama.ByteEncoder(k.partitionKey(m.GetMeta())) | ||
|
|
||
There was a problem hiding this comment.
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.