/
example_sync_producer_test.go
47 lines (37 loc) · 1.34 KB
/
example_sync_producer_test.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
// (c) Copyright IBM Corp. 2023
//go:build go1.17
// +build go1.17
package instasarama_test
import (
"github.com/IBM/sarama"
instana "github.com/instana/go-sensor"
"github.com/instana/go-sensor/instrumentation/instasarama"
"github.com/opentracing/opentracing-go/ext"
)
// This example demonstrates how to instrument a sync Kafka producer using instasarama.
// Error handling is omitted for brevity.
func Example_syncProducer() {
sensor := instana.NewSensor("my-service")
brokers := []string{"localhost:9092"}
config := sarama.NewConfig()
// sarama requires Producer.Return.Successes to be set for sync producers
config.Producer.Return.Successes = true
// enable the use record headers added in kafka v0.11.0 and used to propagate
// trace context
config.Version = sarama.V0_11_0_0
// create a new instrumented instance of sarama.SyncProducer
producer, _ := instasarama.NewSyncProducer(brokers, config, sensor)
// start a new entry span
sp := sensor.Tracer().StartSpan("my-producing-method")
ext.SpanKind.Set(sp, "entry")
msg := &sarama.ProducerMessage{
Topic: "test-topic-1",
Offset: sarama.OffsetNewest,
Value: sarama.StringEncoder("I am a message"),
// ...
}
// inject the span before passing the message to producer
msg = instasarama.ProducerMessageWithSpan(msg, sp)
// pass it to the producer
producer.SendMessage(msg)
}