forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.go
34 lines (29 loc) · 902 Bytes
/
producer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package kafka
import (
"github.com/IBM/sarama"
"github.com/sirupsen/logrus"
)
var (
Topic = "orders"
ProtocolVersion = sarama.V3_0_0_0
)
func CreateKafkaProducer(brokers []string, log *logrus.Logger) (sarama.AsyncProducer, error) {
saramaConfig := sarama.NewConfig()
saramaConfig.Version = ProtocolVersion
// So we can know the partition and offset of messages.
saramaConfig.Producer.Return.Successes = true
saramaConfig.Producer.Interceptors = []sarama.ProducerInterceptor{NewOTelInterceptor()}
producer, err := sarama.NewAsyncProducer(brokers, saramaConfig)
if err != nil {
return nil, err
}
// We will log to STDOUT if we're not able to produce messages.
go func() {
for err := range producer.Errors() {
log.Errorf("Failed to write message: %+v", err)
}
}()
return producer, nil
}