-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
97 lines (83 loc) · 2.48 KB
/
option.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
package otelkafka
import (
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"net"
"strings"
)
const defaultTracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/confluentinc/confluent-kafka-go/otelkafka"
type config struct {
TracerProvider trace.TracerProvider
Propagators propagation.TextMapPropagator
Tracer trace.Tracer
consumerGroupID string
bootstrapServers string
attributeInjectFunc func(msg *kafka.Message) []attribute.KeyValue
}
// newConfig returns a config with all Options set.
func newConfig(opts ...Option) config {
cfg := config{
Propagators: otel.GetTextMapPropagator(),
TracerProvider: otel.GetTracerProvider(),
}
for _, opt := range opts {
opt.apply(&cfg)
}
cfg.Tracer = cfg.TracerProvider.Tracer(
defaultTracerName,
trace.WithInstrumentationVersion(Version()),
)
return cfg
}
// Option interface used for setting optional config properties.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (fn optionFunc) apply(c *config) {
fn(c)
}
// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider trace.TracerProvider) Option {
return optionFunc(func(cfg *config) {
if provider != nil {
cfg.TracerProvider = provider
}
})
}
// WithPropagators specifies propagators to use for extracting
// information from the HTTP requests. If none are specified, global
// ones will be used.
func WithPropagators(propagators propagation.TextMapPropagator) Option {
return optionFunc(func(cfg *config) {
if propagators != nil {
cfg.Propagators = propagators
}
})
}
func WithCustomAttributeInjector(fn func(msg *kafka.Message) []attribute.KeyValue) Option {
return optionFunc(func(cfg *config) {
cfg.attributeInjectFunc = fn
})
}
// withConfig extracts the config information from kafka.ConfigMap for the client
func withConfig(cg *kafka.ConfigMap) Option {
return optionFunc(func(cfg *config) {
if groupID, err := cg.Get("group.id", ""); err == nil {
cfg.consumerGroupID = groupID.(string)
}
if bs, err := cg.Get("bootstrap.servers", ""); err == nil && bs != "" {
for _, addr := range strings.Split(bs.(string), ",") {
host, _, err := net.SplitHostPort(addr)
if err == nil {
cfg.bootstrapServers = host
return
}
}
}
})
}