-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
99 lines (80 loc) · 2.6 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
package tracing
import (
"context"
"fmt"
"io"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"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"
"go.opentelemetry.io/otel/trace/noop"
"google.golang.org/grpc"
)
// Configuration used for tracing
type Config struct {
Enabled bool `yaml:"enabled" default:"false"`
Timeout time.Duration `yaml:"timeout" default:"1s"`
Endpoint string `yaml:"endpoint" default:"otel-collector:4317"`
SamplingRatio float64 `yaml:"sampling_ratio" default:"1" split_words:"true"`
}
// NewTracingProvider creates the provider configuration from the config. If tracing is not enabled,
// a noop tracing provider is returned.
func NewTracingProvider(ctx context.Context, cfg *Config) (trace.TracerProvider, io.Closer, error) {
if !cfg.Enabled {
return noop.NewTracerProvider(), noopCloser, nil
}
r, err := resource.New(ctx,
resource.WithProcess(),
resource.WithOS(),
resource.WithContainer(),
resource.WithHost(),
resource.WithAttributes(semconv.ServiceName("saferplace")),
)
if err != nil {
return nil, nil, fmt.Errorf("unable to ")
}
exporter, err := newExporter(ctx, cfg)
if err != nil {
return nil, nil, fmt.Errorf("unable to create a span processor: %w", err)
}
// Always sample unless the ratio is set lower.
sampler := sdktrace.AlwaysSample()
if cfg.SamplingRatio < 1 {
sampler = sdktrace.ParentBased(
sdktrace.TraceIDRatioBased(cfg.SamplingRatio),
)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(exporter),
sdktrace.WithResource(r),
sdktrace.WithSampler(sampler),
)
otel.SetTracerProvider(tp)
return tp, shutdownCloser(tp.Shutdown), nil
}
func newExporter(ctx context.Context, cfg *Config) (sdktrace.SpanProcessor, error) {
ctx, cancel := context.WithTimeout(ctx, cfg.Timeout)
defer cancel()
client := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(cfg.Endpoint),
otlptracegrpc.WithDialOption(grpc.WithBlock()))
exporter, err := otlptrace.New(ctx, client)
if err != nil {
return nil, fmt.Errorf("unable to create client: %w", err)
}
return sdktrace.NewBatchSpanProcessor(exporter), nil
}
var noopCloser = closer(func() error { return nil })
type closer func() error
func (c closer) Close() error {
return c()
}
type shutdownCloser func(context.Context) error
func (c shutdownCloser) Close() error {
return c(context.Background())
}