/
producer.go
107 lines (87 loc) · 2.84 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"flag"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/Shopify/sarama"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama"
"go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama/example"
)
var (
brokers = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The Kafka brokers to connect to, as a comma separated list")
)
func main() {
tp := example.InitTracer()
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
}()
flag.Parse()
if *brokers == "" {
flag.PrintDefaults()
os.Exit(1)
}
brokerList := strings.Split(*brokers, ",")
log.Printf("Kafka brokers: %s", strings.Join(brokerList, ", "))
producer := newAccessLogProducer(brokerList)
rand.Seed(time.Now().Unix())
// Create root span
tr := otel.Tracer("producer")
ctx, span := tr.Start(context.Background(), "produce message")
defer span.End()
// Inject tracing info into message
msg := sarama.ProducerMessage{
Topic: example.KafkaTopic,
Key: sarama.StringEncoder("random_number"),
Value: sarama.StringEncoder(fmt.Sprintf("%d", rand.Intn(1000))),
}
otel.GetTextMapPropagator().Inject(ctx, otelsarama.NewProducerMessageCarrier(&msg))
producer.Input() <- &msg
successMsg := <-producer.Successes()
log.Println("Successful to write message, offset:", successMsg.Offset)
err := producer.Close()
if err != nil {
span.SetStatus(codes.Error, err.Error())
log.Fatalln("Failed to close producer:", err)
}
}
func newAccessLogProducer(brokerList []string) sarama.AsyncProducer {
config := sarama.NewConfig()
config.Version = sarama.V2_5_0_0
// So we can know the partition and offset of messages.
config.Producer.Return.Successes = true
producer, err := sarama.NewAsyncProducer(brokerList, config)
if err != nil {
log.Fatalln("Failed to start Sarama producer:", err)
}
// Wrap instrumentation
producer = otelsarama.WrapAsyncProducer(config, producer)
// We will log to STDOUT if we're not able to produce messages.
go func() {
for err := range producer.Errors() {
log.Println("Failed to write message:", err)
}
}()
return producer
}