-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
105 lines (88 loc) · 2.83 KB
/
config.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
package otelkafkakonsumer
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.13.0"
"go.opentelemetry.io/otel/trace"
)
// version to attach in the tracer
const version = "0.0.5"
// instrumentationName is the instrumentation library identifier for a Tracer.
const instrumentationName = "github.com/Trendyol/otel-kafka-konsumer"
// Config contains configuration options.
type Config struct {
defaultTracerName string
Tracer trace.Tracer
TracerProvider trace.TracerProvider
Propagator propagation.TextMapPropagator
DefaultStartOpts []trace.SpanStartOption
}
// NewConfig returns a Config for instrumentation with all options applied.
//
// If no TracerProvider or Propagator are specified with options, the default
// OpenTelemetry globals will be used.
func NewConfig(instrumentationName string, options ...Option) *Config {
c := Config{defaultTracerName: instrumentationName}
for _, o := range options {
if o != nil {
o.Apply(&c)
}
}
if c.Tracer == nil {
c.Tracer = otel.Tracer(
c.defaultTracerName,
trace.WithInstrumentationVersion(version),
trace.WithSchemaURL(semconv.SchemaURL),
)
}
if c.Propagator == nil {
c.Propagator = otel.GetTextMapPropagator()
}
return &c
}
// ResolveTracer returns an OpenTelemetry tracer from the appropriate
// TracerProvider.
//
// If the passed context contains a span, the TracerProvider that created the
// tracer that created that span will be used. Otherwise, the TracerProvider
// from c is used.
func (c *Config) ResolveTracer(ctx context.Context) trace.Tracer {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
return span.TracerProvider().Tracer(
c.defaultTracerName,
trace.WithInstrumentationVersion(version),
trace.WithSchemaURL(semconv.SchemaURL),
)
}
return c.Tracer
}
// MergedSpanStartOptions returns a copy of opts with any DefaultStartOpts
// that c is configured with prepended.
func (c *Config) MergedSpanStartOptions(opts ...trace.SpanStartOption) []trace.SpanStartOption {
if c == nil || len(c.DefaultStartOpts) == 0 {
if len(opts) == 0 {
return nil
}
cp := make([]trace.SpanStartOption, len(opts))
copy(cp, opts)
return cp
}
merged := make([]trace.SpanStartOption, len(c.DefaultStartOpts)+len(opts))
copy(merged, c.DefaultStartOpts)
copy(merged[len(c.DefaultStartOpts):], opts)
return merged
}
// WithSpan wraps the function f with a span named name.
func (c *Config) WithSpan(ctx context.Context, name string, f func(context.Context) error, opts ...trace.SpanStartOption) error {
sso := c.MergedSpanStartOptions(opts...)
ctx, span := c.ResolveTracer(ctx).Start(ctx, name, sso...)
err := f(ctx)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
return err
}