-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.go
123 lines (105 loc) · 3.47 KB
/
telemetry.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package log
import (
"context"
"io"
"net/url"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
stdout "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/synyx/tuwat/pkg/config"
"github.com/synyx/tuwat/pkg/version"
)
func InitializeTracer(appCtx context.Context, cfg *config.Config) trace.Tracer {
var tp *tracesdk.TracerProvider
if cfg.OtelUrl != "" {
tp = otelHttpTracer(appCtx, cfg)
} else {
tp = noopTracer()
}
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.TraceContext{},
propagation.Baggage{}),
)
go tracerShutdown(appCtx, tp)
return tp.Tracer("")
}
func stdoutTracer(cfg *config.Config) (tp *tracesdk.TracerProvider) {
exporter, err := stdout.New()
if err != nil {
zap.L().Fatal("creating new stdout tracer", zap.Error(err))
}
return tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.AlwaysSample()),
tracesdk.WithSyncer(exporter),
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(version.Info.Application),
semconv.ServiceVersionKey.String(version.Info.Version),
attribute.String("environment", cfg.Environment),
attribute.String("instance", cfg.Instance),
)),
)
}
func noopTracer() *tracesdk.TracerProvider {
exporter, err := stdout.New(stdout.WithWriter(io.Discard))
if err != nil {
zap.L().Fatal("creating noop tracer", zap.Error(err))
}
return tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.NeverSample()),
tracesdk.WithBatcher(exporter),
)
}
func otelHttpTracer(ctx context.Context, cfg *config.Config) *tracesdk.TracerProvider {
u, err := url.Parse(cfg.OtelUrl)
if err != nil {
zap.L().Fatal("creating OTLP trace exporter", zap.Error(err))
}
options := []otlptracehttp.Option{otlptracehttp.WithEndpoint(u.Host)}
if u.Scheme == "http" {
options = append(options, otlptracehttp.WithInsecure())
}
if u.Path != "" && u.Path != "/" {
options = append(options, otlptracehttp.WithURLPath(u.Path))
}
headers := map[string]string{
"User-Agent": version.Info.Application + "/" + version.Info.Version,
}
options = append(options, otlptracehttp.WithHeaders(headers))
exporter, err := otlptracehttp.New(ctx, options...)
if err != nil {
zap.L().Warn("creating OTLP trace exporter", zap.Error(err))
return noopTracer()
}
return tracesdk.NewTracerProvider(
// Always be sure to batch in production.
tracesdk.WithBatcher(exporter),
// Record information about this application in a Resource.
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(version.Info.Application),
semconv.ServiceVersionKey.String(version.Info.Version),
attribute.String("environment", cfg.Environment),
attribute.String("instance", cfg.Instance),
)),
)
}
func tracerShutdown(appCtx context.Context, tp *tracesdk.TracerProvider) {
<-appCtx.Done()
// Do not make the application hang when it is shutdown.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := tp.Shutdown(ctx); err != nil {
zap.L().Fatal("shutting down tracer", zap.Error(err))
}
}