-
Notifications
You must be signed in to change notification settings - Fork 0
/
traces.go
61 lines (51 loc) · 1.62 KB
/
traces.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
package oteltraces
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/trace"
otelagent "github.com/sdinsure/agent/pkg/opentelemetry"
)
type OTELTraces struct {
shutdownFunc func(context.Context) error
}
func NewOTELTraces(optionFuncs ...otelagent.OTELOptionFunc) (*OTELTraces, error) {
options := otelagent.DefaultOption()
for _, optFunc := range optionFuncs {
optFunc(options)
}
if err := options.Validate(); err != nil {
return nil, err
}
traceExporter, err := otlptracegrpc.New(
options.Context(),
otlptracegrpc.WithGRPCConn(options.Conn()),
)
if err != nil {
return nil, err
}
traceProviderOptions := []trace.TracerProviderOption{
trace.WithResource(options.Resource()),
trace.WithBatcher(traceExporter,
trace.WithBatchTimeout(5*time.Second)),
}
if options.Stdout() {
stdoutTraceExporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return nil, err
}
traceProviderOptions = append(traceProviderOptions,
trace.WithBatcher(stdoutTraceExporter,
trace.WithBatchTimeout(5*time.Second)))
}
tracerProvider := trace.NewTracerProvider(traceProviderOptions...)
otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return &OTELTraces{shutdownFunc: tracerProvider.Shutdown}, nil
}
func (o *OTELTraces) Shutdown(ctx context.Context) error {
return o.shutdownFunc(ctx)
}