generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracerProvider.go
216 lines (191 loc) · 6.58 KB
/
tracerProvider.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package candlelight
import (
"context"
"errors"
"fmt"
"io"
"strings"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/jaeger" // nolint:staticcheck
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
stdout "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)
var (
ErrTracerProviderNotFound = errors.New("TracerProvider builder could not be found")
ErrTracerProviderBuildFailed = errors.New("Failed building TracerProvider")
ErrInvalidParentBasedValue = errors.New("Invalid ParentBased value provided in configuration")
ErrInvalidNoParentValue = errors.New("Invalid No Parent value provided in configuration")
)
// DefaultTracerProvider is used when no provider is given.
// The Noop tracer provider turns all tracing related operations into
// noops essentially disabling tracing.
const DefaultTracerProvider = "noop"
// ConfigureTracerProvider creates the TracerProvider based on the configuration
// provided. It has built-in support for jaeger, zipkin, stdout and noop providers.
// A different provider can be used if a constructor for it is provided in the
// config.
// If a provider name is not provided, a noop tracerProvider will be returned.
func ConfigureTracerProvider(config Config) (trace.TracerProvider, error) {
if len(config.Provider) == 0 {
config.Provider = DefaultTracerProvider
}
// Handling camelcase of provider.
config.Provider = strings.ToLower(config.Provider)
providerConfig := config.Providers[config.Provider]
parentBasedTracing := config.ParentBased
noParentTracing := config.NoParent
if providerConfig == nil {
providerConfig = providersConfig[config.Provider]
}
if providerConfig == nil {
return nil, fmt.Errorf("%w for provider %s", ErrTracerProviderNotFound, config.Provider)
}
// If parentBased value is empty, use default value
if parentBasedTracing == "" {
parentBasedTracing = "ignore"
}
// If noParent value is empty, use default value
if noParentTracing == "" {
noParentTracing = "never"
}
// Setting up trace sampler based on ParentBased and NoParent values in the config
var sampler sdktrace.Sampler
if parentBasedTracing == "ignore" {
sampler = sdktrace.NeverSample()
} else if parentBasedTracing == "honor" {
if noParentTracing == "never" {
sampler = sdktrace.ParentBased(sdktrace.NeverSample())
} else if noParentTracing == "always" {
sampler = sdktrace.ParentBased(sdktrace.AlwaysSample())
} else {
return nil, ErrInvalidNoParentValue
}
} else {
return nil, ErrInvalidParentBasedValue
}
provider, err := providerConfig(config, sampler)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTracerProviderBuildFailed, err)
}
return provider, nil
}
// ProviderConstructor is useful when client wants to add their own custom
// TracerProvider.
type ProviderConstructor func(config Config, sampler sdktrace.Sampler) (trace.TracerProvider, error)
// Created pre-defined immutable map of built-in provider's
var providersConfig = map[string]ProviderConstructor{
"otlp/grpc": func(cfg Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
// Send traces over gRPC
if cfg.Endpoint == "" {
return nil, ErrTracerProviderBuildFailed
}
exporter, err := otlptracegrpc.New(context.Background(),
otlptracegrpc.WithEndpoint(cfg.Endpoint),
otlptracegrpc.WithInsecure(),
)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTracerProviderBuildFailed, err)
}
return sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.ApplicationName),
),
),
sdktrace.WithSampler(smplr),
), nil
},
"otlp/http": func(cfg Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
// Send traces over HTTP
if cfg.Endpoint == "" {
return nil, ErrTracerProviderBuildFailed
}
exporter, err := otlptracehttp.New(context.Background(),
otlptracehttp.WithEndpoint(cfg.Endpoint),
otlptracehttp.WithInsecure(),
)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTracerProviderBuildFailed, err)
}
return sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.ApplicationName),
),
),
sdktrace.WithSampler(smplr),
), nil
},
"jaeger": func(cfg Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
if cfg.Endpoint == "" {
return nil, ErrTracerProviderBuildFailed
}
exporter, err := jaeger.New(
jaeger.WithCollectorEndpoint(
jaeger.WithEndpoint(cfg.Endpoint)))
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTracerProviderBuildFailed, err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.ApplicationName),
attribute.String("exporter", cfg.Provider),
)),
)
return tp, nil
},
"zipkin": func(cfg Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
if cfg.Endpoint == "" {
return nil, ErrTracerProviderBuildFailed
}
exporter, err := zipkin.New(cfg.Endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrTracerProviderBuildFailed, err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(cfg.ApplicationName),
attribute.String("exporter", cfg.Provider),
)),
)
return tp, nil
},
"stdout": func(cfg Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
var option stdout.Option
if cfg.SkipTraceExport {
option = stdout.WithWriter(io.Discard)
} else {
option = stdout.WithPrettyPrint()
}
exporter, err := stdout.New(option)
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
return tp, nil
},
"noop": func(config Config, smplr sdktrace.Sampler) (trace.TracerProvider, error) {
return noop.NewTracerProvider(), nil
},
}