-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotel.go
88 lines (74 loc) · 2.51 KB
/
otel.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
package telemetrylib
import (
"context"
"errors"
"go.opentelemetry.io/contrib/instrumentation/runtime"
"go.opentelemetry.io/contrib/propagators/autoprop"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/trace"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/metric"
)
const otelEndpoint = "default-collector.opentelemetry-objects:4317"
// SetupOTelSDK bootstraps the OpenTelemetry pipeline.
// If it does not return an error, make sure to call shutdown for proper cleanup.
func SetupOTelSDK(ctx context.Context, traceSampleRate float64) (shutdown func(context.Context) error, err error) {
var shutdownFuncs []func(context.Context) error
// shutdown calls cleanup functions registered via shutdownFuncs.
// The errors from the calls are joined.
// Each registered cleanup will be invoked once.
shutdown = func(ctx context.Context) error {
var err error
for _, fn := range shutdownFuncs {
err = errors.Join(err, fn(ctx))
}
shutdownFuncs = nil
return err
}
// handleErr calls shutdown for cleanup and makes sure that all errors are returned.
handleErr := func(inErr error) {
err = errors.Join(inErr, shutdown(ctx))
}
otel.SetTextMapPropagator(autoprop.NewTextMapPropagator())
meterProvider, err := newMeterProvider()
if err != nil {
handleErr(err)
return
}
shutdownFuncs = append(shutdownFuncs, meterProvider.Shutdown)
otel.SetMeterProvider(meterProvider)
tracerProvider, err := newTracerProvider(ctx, traceSampleRate)
if err != nil {
handleErr(err)
return
}
shutdownFuncs = append(shutdownFuncs, tracerProvider.Shutdown)
otel.SetTracerProvider(tracerProvider)
err = runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second))
if err != nil {
handleErr(err)
}
return shutdown, nil
}
func newMeterProvider() (*metric.MeterProvider, error) {
metricExporter, err := prometheus.New()
if err != nil {
return nil, err
}
meterProvider := metric.NewMeterProvider(metric.WithReader(metricExporter))
return meterProvider, nil
}
func newTracerProvider(ctx context.Context, traceSampleRate float64) (*trace.TracerProvider, error) {
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(otelEndpoint),
)
if err != nil {
return nil, err
}
return trace.NewTracerProvider(
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(traceSampleRate))),
trace.WithSpanProcessor(trace.NewBatchSpanProcessor(traceExporter)),
), nil
}