-
Notifications
You must be signed in to change notification settings - Fork 0
/
initer.go
69 lines (57 loc) · 1.42 KB
/
initer.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
package trace
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
"github.com/agurinov/gopl/env/envvars"
c "github.com/agurinov/gopl/patterns/creational"
)
type (
initer struct {
cmdName string
exporterOptions []otlptracegrpc.Option
samplerOptions []SamplerOption
batcherOptions []trace.BatchSpanProcessorOption
}
IniterOption = c.Option[initer]
)
func Init(ctx context.Context, opts ...IniterOption) error {
if !envvars.OtelTraceEnabled.Present() {
return nil
}
switch enabled, err := envvars.OtelTraceEnabled.Value(); {
case err != nil:
return err
case !enabled:
return nil
}
i, err := c.NewWithValidate(opts...)
if err != nil {
return err
}
exporter, err := otlptracegrpc.New(ctx, i.exporterOptions...)
if err != nil {
return err
}
sampler, err := NewSampler(i.samplerOptions...)
if err != nil {
return err
}
provider := trace.NewTracerProvider(
trace.WithBatcher(exporter, i.batcherOptions...),
trace.WithSampler(sampler),
trace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(i.cmdName),
),
),
)
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(provider)
return nil
}