-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.go
280 lines (227 loc) · 7.56 KB
/
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package uptrace
import (
"context"
"crypto/tls"
"os"
"github.com/uptrace/uptrace-go/internal"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)
type config struct {
dsn string
// Common options
resourceAttributes []attribute.KeyValue
resourceDetectors []resource.Detector
resource *resource.Resource
tlsConf *tls.Config
// Tracing options
tracingEnabled bool
textMapPropagator propagation.TextMapPropagator
tracerProvider *sdktrace.TracerProvider
traceSampler sdktrace.Sampler
prettyPrint bool
bspOptions []sdktrace.BatchSpanProcessorOption
// Metrics options
metricsEnabled bool
metricOptions []metric.Option
}
func newConfig(opts []Option) *config {
conf := &config{
tracingEnabled: true,
metricsEnabled: true,
}
if dsn, ok := os.LookupEnv("UPTRACE_DSN"); ok {
conf.dsn = dsn
}
for _, opt := range opts {
opt.apply(conf)
}
return conf
}
func (conf *config) newResource() *resource.Resource {
if conf.resource != nil {
if len(conf.resourceAttributes) > 0 {
internal.Logger.Printf("WithResource overrides WithResourceAttributes (discarding %v)",
conf.resourceAttributes)
}
if len(conf.resourceDetectors) > 0 {
internal.Logger.Printf("WithResource overrides WithResourceDetectors (discarding %v)",
conf.resourceDetectors)
}
return conf.resource
}
ctx := context.TODO()
res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithDetectors(conf.resourceDetectors...),
resource.WithAttributes(conf.resourceAttributes...))
if err != nil {
otel.Handle(err)
return resource.Environment()
}
return res
}
//------------------------------------------------------------------------------
type Option interface {
apply(conf *config)
}
type option func(conf *config)
func (fn option) apply(conf *config) {
fn(conf)
}
// WithDSN configures a data source name that is used to connect to Uptrace, for example,
// `https://<token>@uptrace.dev/<project_id>`.
//
// The default is to use UPTRACE_DSN environment variable.
func WithDSN(dsn string) Option {
return option(func(conf *config) {
conf.dsn = dsn
})
}
// WithServiceName configures `service.name` resource attribute.
func WithServiceName(serviceName string) Option {
return option(func(conf *config) {
attr := semconv.ServiceNameKey.String(serviceName)
conf.resourceAttributes = append(conf.resourceAttributes, attr)
})
}
// WithServiceVersion configures `service.version` resource attribute, for example, `1.0.0`.
func WithServiceVersion(serviceVersion string) Option {
return option(func(conf *config) {
attr := semconv.ServiceVersionKey.String(serviceVersion)
conf.resourceAttributes = append(conf.resourceAttributes, attr)
})
}
// WithDeploymentEnvironment configures `deployment.environment` resource attribute,
// for example, `production`.
func WithDeploymentEnvironment(env string) Option {
return option(func(conf *config) {
attr := semconv.DeploymentEnvironmentKey.String(env)
conf.resourceAttributes = append(conf.resourceAttributes, attr)
})
}
// WithResourceAttributes configures resource attributes that describe an entity that produces
// telemetry, for example, such attributes as host.name, service.name, etc.
//
// The default is to use `OTEL_RESOURCE_ATTRIBUTES` env var, for example,
// `OTEL_RESOURCE_ATTRIBUTES=service.name=myservice,service.version=1.0.0`.
func WithResourceAttributes(attrs ...attribute.KeyValue) Option {
return option(func(conf *config) {
conf.resourceAttributes = append(conf.resourceAttributes, attrs...)
})
}
// WithResourceDetectors adds detectors to be evaluated for the configured resource.
func WithResourceDetectors(detectors ...resource.Detector) Option {
return option(func(conf *config) {
conf.resourceDetectors = append(conf.resourceDetectors, detectors...)
})
}
// WithResource configures a resource that describes an entity that produces telemetry,
// for example, such attributes as host.name and service.name. All produced spans and metrics
// will have these attributes.
//
// WithResource overrides and replaces any other resource attributes.
func WithResource(resource *resource.Resource) Option {
return option(func(conf *config) {
conf.resource = resource
})
}
func WithTLSConfig(tlsConf *tls.Config) Option {
return option(func(conf *config) {
conf.tlsConf = tlsConf
})
}
//------------------------------------------------------------------------------
type TracingOption interface {
Option
tracing()
}
type tracingOption func(conf *config)
var _ TracingOption = (*tracingOption)(nil)
func (fn tracingOption) apply(conf *config) {
fn(conf)
}
func (fn tracingOption) tracing() {}
// WithTracingEnabled can be used to enable/disable tracing.
func WithTracingEnabled(on bool) TracingOption {
return tracingOption(func(conf *config) {
conf.tracingEnabled = on
})
}
// WithTracingDisabled disables tracing.
func WithTracingDisabled() TracingOption {
return WithTracingEnabled(false)
}
// WithTracerProvider overwrites the default Uptrace tracer provider.
// You can use it to configure Uptrace distro to use OTLP exporter.
//
// When this option is used, you might need to call otel.SetTracerProvider
// to register the provider as the global trace provider.
func WithTracerProvider(provider *sdktrace.TracerProvider) TracingOption {
return tracingOption(func(conf *config) {
conf.tracerProvider = provider
})
}
// WithTraceSampler configures a span sampler.
func WithTraceSampler(sampler sdktrace.Sampler) TracingOption {
return tracingOption(func(conf *config) {
conf.traceSampler = sampler
})
}
// WithPropagator sets the global TextMapPropagator used by OpenTelemetry.
// The default is propagation.TraceContext and propagation.Baggage.
func WithPropagator(propagator propagation.TextMapPropagator) TracingOption {
return tracingOption(func(conf *config) {
conf.textMapPropagator = propagator
})
}
// WithTextMapPropagator is an alias for WithPropagator.
func WithTextMapPropagator(propagator propagation.TextMapPropagator) TracingOption {
return WithPropagator(propagator)
}
// WithPrettyPrintSpanExporter adds a span exproter that prints spans to stdout.
// It is useful for debugging or demonstration purposes.
func WithPrettyPrintSpanExporter() TracingOption {
return tracingOption(func(conf *config) {
conf.prettyPrint = true
})
}
// WithBatchSpanProcessorOption specifies options used to created BatchSpanProcessor.
func WithBatchSpanProcessorOption(opts ...sdktrace.BatchSpanProcessorOption) TracingOption {
return tracingOption(func(conf *config) {
conf.bspOptions = append(conf.bspOptions, opts...)
})
}
//------------------------------------------------------------------------------
type MetricsOption interface {
Option
metrics()
}
type metricsOption func(conf *config)
var _ MetricsOption = (*metricsOption)(nil)
func (fn metricsOption) apply(conf *config) {
fn(conf)
}
func (fn metricsOption) metrics() {}
// WithMetricsEnabled can be used to enable/disable metrics.
func WithMetricsEnabled(on bool) MetricsOption {
return metricsOption(func(conf *config) {
conf.metricsEnabled = on
})
}
// WithMetricsDisabled disables metrics.
func WithMetricsDisabled() MetricsOption {
return WithMetricsEnabled(false)
}
func WithMetricOption(options ...metric.Option) MetricsOption {
return metricsOption(func(conf *config) {
conf.metricOptions = append(conf.metricOptions, options...)
})
}