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

[libbeat] kafka message support headers feature #29940

Merged
merged 6 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Add `script` processor to all beats {issue}29269[29269] {pull}29752[29752]
- Only connect to Elasticsearch instances with the same version or newer. {pull}29683[29683]
- Move umask from code to service files. {pull}29708[29708]
- Add support for kafka message headers. {pull}29940[29940]
- Add FIPS configuration option for all AWS API calls. {pull}[28899]
- Add metadata change support for some processors {pull}30183[30183]

Expand Down
19 changes: 18 additions & 1 deletion libbeat/outputs/kafka/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type client struct {

producer sarama.AsyncProducer

recordHeaders []sarama.RecordHeader

wg sync.WaitGroup
}

Expand All @@ -76,7 +78,8 @@ func newKafkaClient(
index string,
key *fmtstr.EventFormatString,
topic outil.Selector,
writer codec.Codec,
headers map[string]string,
writer codec.Codec,
rdner marked this conversation as resolved.
Show resolved Hide resolved
cfg *sarama.Config,
) (*client, error) {
c := &client{
Expand All @@ -90,6 +93,20 @@ func newKafkaClient(
config: *cfg,
done: make(chan struct{}),
}

if len(headers) != 0 {
recordHeaders := make([]sarama.RecordHeader, 0)
herbxu marked this conversation as resolved.
Show resolved Hide resolved
for k, v := range headers {
recordHeader := sarama.RecordHeader{
Key: []byte(k),
Value: []byte(v),
}

recordHeaders = append(recordHeaders, recordHeader)
}
c.recordHeaders = recordHeaders
}

return c, nil
}

Expand Down
2 changes: 2 additions & 0 deletions libbeat/outputs/kafka/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type kafkaConfig struct {
BulkMaxSize int `config:"bulk_max_size"`
BulkFlushFrequency time.Duration `config:"bulk_flush_frequency"`
MaxRetries int `config:"max_retries" validate:"min=-1,nonzero"`
Headers map[string]string `config:"headers"`
Backoff backoffConfig `config:"backoff"`
ClientID string `config:"client_id"`
ChanBufferSize int `config:"channel_buffer_size" validate:"min=1"`
Expand Down Expand Up @@ -125,6 +126,7 @@ func defaultConfig() kafkaConfig {
CompressionLevel: 4,
Version: kafka.Version("1.0.0"),
MaxRetries: 3,
Headers: nil,
Backoff: backoffConfig{
Init: 1 * time.Second,
Max: 60 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion libbeat/outputs/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func makeKafka(
return outputs.Fail(err)
}

client, err := newKafkaClient(observer, hosts, beat.IndexPrefix, config.Key, topic, codec, libCfg)
client, err := newKafkaClient(observer, hosts, beat.IndexPrefix, config.Key, topic, config.Headers, codec, libCfg)
if err != nil {
return outputs.Fail(err)
}
Expand Down
1 change: 1 addition & 0 deletions libbeat/outputs/kafka/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ func (m *message) initProducerMessage() {
Key: sarama.ByteEncoder(m.key),
Value: sarama.ByteEncoder(m.value),
Timestamp: m.ts,
Headers : m.ref.client.recordHeaders,
}
}