-
Notifications
You must be signed in to change notification settings - Fork 764
Expand file tree
/
Copy pathconfig.go
More file actions
225 lines (193 loc) · 6.52 KB
/
config.go
File metadata and controls
225 lines (193 loc) · 6.52 KB
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package otelconf provides an OpenTelemetry declarative configuration SDK.
package otelconf // import "go.opentelemetry.io/contrib/otelconf"
import (
"context"
"errors"
"os"
"go.opentelemetry.io/otel/log"
nooplog "go.opentelemetry.io/otel/log/noop"
"go.opentelemetry.io/otel/metric"
noopmetric "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
sdklog "go.opentelemetry.io/otel/sdk/log"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
nooptrace "go.opentelemetry.io/otel/trace/noop"
yaml "go.yaml.in/yaml/v3"
"go.opentelemetry.io/contrib/otelconf/internal/provider"
)
const (
envVarConfigFileDeprecated = "OTEL_EXPERIMENTAL_CONFIG_FILE"
envVarConfigFile = "OTEL_CONFIG_FILE"
)
// SDK is a struct that contains all the providers
// configured via the configuration model.
type SDK struct {
meterProvider metric.MeterProvider
tracerProvider trace.TracerProvider
loggerProvider log.LoggerProvider
propagator propagation.TextMapPropagator
shutdown shutdownFunc
}
// TracerProvider returns a configured trace.TracerProvider.
func (s *SDK) TracerProvider() trace.TracerProvider {
return s.tracerProvider
}
// MeterProvider returns a configured metric.MeterProvider.
func (s *SDK) MeterProvider() metric.MeterProvider {
return s.meterProvider
}
// LoggerProvider returns a configured log.LoggerProvider.
func (s *SDK) LoggerProvider() log.LoggerProvider {
return s.loggerProvider
}
// Propagator returns a configured propagation.TextMapPropagator.
func (s *SDK) Propagator() propagation.TextMapPropagator {
return s.propagator
}
// Shutdown calls shutdown on all configured providers.
func (s *SDK) Shutdown(ctx context.Context) error {
return s.shutdown(ctx)
}
var (
noopSDK = SDK{
loggerProvider: nooplog.LoggerProvider{},
meterProvider: noopmetric.MeterProvider{},
tracerProvider: nooptrace.TracerProvider{},
propagator: propagation.NewCompositeTextMapPropagator(),
shutdown: func(context.Context) error { return nil },
}
errDeprecatedEnvVarUsed = errors.New("OTEL_EXPERIMENTAL_CONFIG_FILE is no longer supported, use OTEL_CONFIG_FILE instead")
)
func parseConfigFileFromEnvironment(filename string) (ConfigurationOption, error) {
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
// Parse a configuration file into an OpenTelemetryConfiguration model.
c, err := ParseYAML(b)
if err != nil {
return nil, err
}
// Create SDK components with the parsed configuration.
return WithOpenTelemetryConfiguration(*c), nil
}
// NewSDK creates SDK providers based on the configuration model. It checks the local environment and
// uses the file set in the variable `OTEL_CONFIG_FILE` to configure the SDK automatically.
// Any file defined by `OTEL_CONFIG_FILE` will supersede all files passed with
// [WithOpenTelemetryConfiguration].
func NewSDK(opts ...ConfigurationOption) (SDK, error) {
_, ok := os.LookupEnv(envVarConfigFileDeprecated)
if ok {
return noopSDK, errDeprecatedEnvVarUsed
}
filename, ok := os.LookupEnv(envVarConfigFile)
if ok {
opt, err := parseConfigFileFromEnvironment(filename)
if err != nil {
return noopSDK, err
}
opts = append(opts, opt)
}
o := configOptions{
ctx: context.Background(),
}
for _, opt := range opts {
o = opt.apply(o)
}
if o.opentelemetryConfig.Disabled != nil && *o.opentelemetryConfig.Disabled {
return noopSDK, nil
}
r, err := newResource(o.opentelemetryConfig.Resource)
if err != nil {
return noopSDK, err
}
p, err := newPropagator(o.opentelemetryConfig.Propagator)
if err != nil {
return noopSDK, err
}
mp, mpShutdown, err := meterProvider(o, r)
if err != nil {
return noopSDK, err
}
tp, tpShutdown, err := tracerProvider(o, r)
if err != nil {
return noopSDK, err
}
lp, lpShutdown, err := loggerProvider(o, r)
if err != nil {
return noopSDK, err
}
return SDK{
meterProvider: mp,
tracerProvider: tp,
loggerProvider: lp,
propagator: p,
shutdown: func(ctx context.Context) error {
return errors.Join(mpShutdown(ctx), tpShutdown(ctx), lpShutdown(ctx))
},
}, nil
}
// ConfigurationOption configures options for providers.
type ConfigurationOption interface {
apply(configOptions) configOptions
}
type configurationOptionFunc func(configOptions) configOptions
func (fn configurationOptionFunc) apply(cfg configOptions) configOptions {
return fn(cfg)
}
// WithContext sets the context.Context for the SDK.
func WithContext(ctx context.Context) ConfigurationOption {
return configurationOptionFunc(func(c configOptions) configOptions {
c.ctx = ctx
return c
})
}
// WithOpenTelemetryConfiguration sets the OpenTelemetryConfiguration used
// to produce the SDK.
func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) ConfigurationOption {
return configurationOptionFunc(func(c configOptions) configOptions {
c.opentelemetryConfig = cfg
return c
})
}
// WithLoggerProviderOptions appends LoggerProviderOptions used for constructing
// the LoggerProvider. OpenTelemetryConfiguration takes precedence over these options.
func WithLoggerProviderOptions(opts ...sdklog.LoggerProviderOption) ConfigurationOption {
return configurationOptionFunc(func(c configOptions) configOptions {
c.loggerProviderOptions = append(c.loggerProviderOptions, opts...)
return c
})
}
// WithMeterProviderOptions appends metric.Options used for constructing the
// MeterProvider. OpenTelemetryConfiguration takes precedence over these options.
func WithMeterProviderOptions(opts ...sdkmetric.Option) ConfigurationOption {
return configurationOptionFunc(func(c configOptions) configOptions {
c.meterProviderOptions = append(c.meterProviderOptions, opts...)
return c
})
}
// WithTracerProviderOptions appends TracerProviderOptions used for constructing
// the TracerProvider. OpenTelemetryConfiguration takes precedence over these options.
func WithTracerProviderOptions(opts ...sdktrace.TracerProviderOption) ConfigurationOption {
return configurationOptionFunc(func(c configOptions) configOptions {
c.tracerProviderOptions = append(c.tracerProviderOptions, opts...)
return c
})
}
// ParseYAML parses a YAML configuration file into an OpenTelemetryConfiguration.
func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) {
file, err := provider.ReplaceEnvVars(file)
if err != nil {
return nil, err
}
var cfg OpenTelemetryConfiguration
err = yaml.Unmarshal(file, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}