-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
66 lines (56 loc) · 2.14 KB
/
provider.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
package otelwrapper
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"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.7.0"
"sync"
)
// InitTracerProvider returns an OpenTelemetry TracerProvider configured to use
// the exporters (OTLP by default) that will send spans to the provided url. The returned
// TracerProvider will also use a Resource configured with all the information
// about the application.
func InitTracerProvider(serviceName, namespace string, exporters ...tracesdk.SpanExporter) (*tracesdk.TracerProvider, error) {
opts := make([]tracesdk.TracerProviderOption, 0)
if len(exporters) == 0 {
client := otlptracehttp.NewClient()
exp, err := otlptrace.New(context.Background(), client)
if err != nil {
return nil, err
}
exporters = append(exporters, exp)
}
// Always be sure to batch in production.
for _, exp := range exporters {
opts = append(opts, tracesdk.WithBatcher(exp))
}
// Record information about this application in an Resource.
opts = append(opts, tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(serviceName),
semconv.ServiceNamespaceKey.String(namespace),
)))
tp := tracesdk.NewTracerProvider(opts...)
// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}))
return tp, nil
}
func ShutdownWaiting(tp *tracesdk.TracerProvider, ctx context.Context, wg *sync.WaitGroup) {
childCtx := context.WithValue(ctx, "tracer provider shutdown", nil)
wg.Add(1)
<-childCtx.Done()
fmt.Println("Shutting down OTel tracer provider...")
if err := tp.Shutdown(context.Background()); err != nil {
fmt.Printf("OTel tracer provider forced to shutdown: %v\n", err)
} else {
fmt.Println("OTel tracer provider shutdown.")
}
wg.Done()
}