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

feat(outputs.kafka): Option to set producer message timestamp #15689

Merged
merged 1 commit into from
Jul 31, 2024
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
6 changes: 6 additions & 0 deletions plugins/outputs/kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ to use them.
## smaller than the broker's 'message.max.bytes'.
# max_message_bytes = 1000000

## Producer timestamp
## This option sets the timestamp of the kafka producer message, choose from:
## * metric: Uses the metric's timestamp
## * now: Uses the time of write
# producer_timestamp = metric

## Optional TLS Config
# enable_tls = false
# tls_ca = "/etc/telegraf/ca.pem"
Expand Down
25 changes: 17 additions & 8 deletions plugins/outputs/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ var ValidTopicSuffixMethods = []string{
var zeroTime = time.Unix(0, 0)

type Kafka struct {
Brokers []string `toml:"brokers"`
Topic string `toml:"topic"`
TopicTag string `toml:"topic_tag"`
ExcludeTopicTag bool `toml:"exclude_topic_tag"`
TopicSuffix TopicSuffix `toml:"topic_suffix"`
RoutingTag string `toml:"routing_tag"`
RoutingKey string `toml:"routing_key"`
Brokers []string `toml:"brokers"`
Topic string `toml:"topic"`
TopicTag string `toml:"topic_tag"`
ExcludeTopicTag bool `toml:"exclude_topic_tag"`
TopicSuffix TopicSuffix `toml:"topic_suffix"`
RoutingTag string `toml:"routing_tag"`
RoutingKey string `toml:"routing_key"`
ProducerTimestamp string `toml:"producer_timestamp"`

proxy.Socks5ProxyConfig

Expand Down Expand Up @@ -152,6 +153,14 @@ func (k *Kafka) Init() error {
}
k.saramaConfig = config

switch k.ProducerTimestamp {
case "":
k.ProducerTimestamp = "metric"
case "metric", "now":
default:
return fmt.Errorf("unknown producer_timestamp option: %s", k.ProducerTimestamp)
}

return nil
}

Expand Down Expand Up @@ -207,7 +216,7 @@ func (k *Kafka) Write(metrics []telegraf.Metric) error {
}

// Negative timestamps are not allowed by the Kafka protocol.
if !metric.Time().Before(zeroTime) {
if k.ProducerTimestamp == "metric" && !metric.Time().Before(zeroTime) {
m.Timestamp = metric.Time()
}

Expand Down
6 changes: 6 additions & 0 deletions plugins/outputs/kafka/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
## smaller than the broker's 'message.max.bytes'.
# max_message_bytes = 1000000

## Producer timestamp
## This option sets the timestamp of the kafka producer message, choose from:
## * metric: Uses the metric's timestamp
## * now: Uses the time of write
# producer_timestamp = metric

## Optional TLS Config
# enable_tls = false
# tls_ca = "/etc/telegraf/ca.pem"
Expand Down
Loading