-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtracing_config.go
146 lines (121 loc) · 3.85 KB
/
tracing_config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ops
import (
"context"
"errors"
"strings"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"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"
"go.opentelemetry.io/otel/semconv/v1.17.0"
"github.com/valkyrie-fnd/valkyrie/configs"
)
type ExporterType string
const (
StdOut ExporterType = "stdout"
OTLPTraceHTTP ExporterType = "otlptracehttp"
None ExporterType = ""
)
// noTracingConfig default empty TracingConfig
var noTracingConfig = TracingConfig{}
type TracingConfig struct {
Exporter ExporterType
Version string
ServiceName string
Namespace string
configs.TraceConfig
}
// Tracing returns a TracingConfig based on the provided Valkyrie config
func Tracing(vConf *configs.ValkyrieConfig) *TracingConfig {
cfg := TracingConfig{}
cfg.TraceConfig = vConf.Telemetry.Tracing
cfg.Version = vConf.Version
cfg.ServiceName = vConf.Telemetry.ServiceName
cfg.Namespace = vConf.Telemetry.Namespace
switch ExporterType(cfg.TraceType) {
case StdOut:
cfg.Exporter = StdOut
case OTLPTraceHTTP:
cfg.Exporter = OTLPTraceHTTP
case None:
cfg.Exporter = None
return &noTracingConfig
default:
log.Warn().Msgf("unsupported tracing type [%s]", cfg.TraceType)
return &noTracingConfig
}
log.Info().Str("traceType", cfg.TraceType).Msg("Configured tracing")
return &cfg
}
// ConfigureTracing configures the tracing framework based on TracingConfig.
func ConfigureTracing(cfg *TracingConfig) error {
// No config - no setup
if *cfg == noTracingConfig {
return errors.New("no tracing config")
}
exp, err := createProviderExporter(cfg)
if err != nil {
log.Warn().Err(err).Msg("failed to setup tracing provider")
return err
}
// Always be sure to batch in production.
bsp := tracesdk.NewBatchSpanProcessor(exp)
tp := tracesdk.NewTracerProvider(
tracesdk.WithSpanProcessor(bsp),
// Record information about this application in a Resource.
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(cfg.ServiceName),
semconv.ServiceNamespace(cfg.Namespace),
semconv.ServiceVersion(cfg.Version),
)),
// Set sampling based on upstream
tracesdk.WithSampler(tracesdk.ParentBased(tracesdk.TraceIDRatioBased(cfg.SampleRatio))),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
if cfg.GoogleProjectID != "" {
// Add googleErrorHook to global logger, so that it's inherited by child loggers
log.Logger = log.Hook(googleErrorHook{})
}
return nil
}
func createProviderExporter(cfg *TracingConfig) (tracesdk.SpanExporter, error) {
var (
exp tracesdk.SpanExporter
err error
)
switch cfg.Exporter {
case OTLPTraceHTTP:
exp, err = otlptracehttp.New(context.Background(), getOTLPTraceOptions(cfg)...)
case StdOut:
exp, err = stdouttrace.New()
}
return exp, err
}
// getOTLPTraceOptions returns options given a tracing config
func getOTLPTraceOptions(cfg *TracingConfig) []otlptracehttp.Option {
options := []otlptracehttp.Option{
otlptracehttp.WithCompression(otlptracehttp.GzipCompression), // enable compression by default
}
url := cfg.URL
if url == "" {
options = append(options, otlptracehttp.WithInsecure()) // use HTTP by default
return options
}
if scheme, remainder, found := strings.Cut(url, "://"); found {
if scheme == "http" {
options = append(options, otlptracehttp.WithInsecure())
}
url = remainder
}
if endpoint, path, found := strings.Cut(url, "/"); found {
options = append(options, otlptracehttp.WithEndpoint(endpoint), otlptracehttp.WithURLPath(path))
} else {
options = append(options, otlptracehttp.WithEndpoint(endpoint))
}
return options
}