-
Notifications
You must be signed in to change notification settings - Fork 9
/
tracing.go
106 lines (88 loc) · 2.77 KB
/
tracing.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
// SPDX-License-Identifier: MIT
package main
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"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/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
)
var (
tracer = otel.GetTracerProvider().Tracer(
"github.com/czerwonk/ovirt_exporter",
trace.WithSchemaURL(semconv.SchemaURL),
)
)
func initTracing(ctx context.Context) (func(), error) {
if !*tracingEnabled {
return initTracingWithNoop()
}
switch *tracingProvider {
case "stdout":
return initTracingToStdOut(ctx)
case "collector":
return initTracingToCollector(ctx)
default:
log.Warnf("got invalid value for tracing.provider: %s, disable tracing", *tracingProvider)
return initTracingWithNoop()
}
}
func initTracingWithNoop() (func(), error) {
tp := trace.NewNoopTracerProvider()
otel.SetTracerProvider(tp)
return func() {}, nil
}
func initTracingToStdOut(ctx context.Context) (func(), error) {
log.Info("Initialize tracing (STDOUT)")
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return nil, fmt.Errorf("failed to create stdout exporter: %w", err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exp),
sdktrace.WithResource(resourceDefinition()),
)
otel.SetTracerProvider(tp)
return shutdownTraceProvider(ctx, tp.Shutdown), nil
}
func initTracingToCollector(ctx context.Context) (func(), error) {
log.Infof("Initialize tracing (agent: %s)", *tracingCollectorEndpoint)
cl := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(*tracingCollectorEndpoint),
)
exp, err := otlptrace.New(ctx, cl)
if err != nil {
return nil, fmt.Errorf("failed to create gRPC collector exporter: %w", err)
}
bsp := sdktrace.NewBatchSpanProcessor(exp)
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(resourceDefinition()),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})
return shutdownTraceProvider(ctx, tp.Shutdown), nil
}
func shutdownTraceProvider(ctx context.Context, shutdownFunc func(ctx context.Context) error) func() {
return func() {
if err := shutdownFunc(ctx); err != nil {
log.Errorf("failed to shutdown TracerProvider: %v", err)
}
}
}
func resourceDefinition() *resource.Resource {
return resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("ovirt_exporter"),
semconv.ServiceVersionKey.String(version),
)
}