| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package x provides an OpenTelemetry declarative configuration SDK. |
| 5 |
package x // import "go.opentelemetry.io/contrib/otelconf/x" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"os" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
nooplog "go.opentelemetry.io/otel/log/noop" |
| 14 |
"go.opentelemetry.io/otel/metric" |
| 15 |
noopmetric "go.opentelemetry.io/otel/metric/noop" |
| 16 |
"go.opentelemetry.io/otel/propagation" |
| 17 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 18 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 19 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 20 |
"go.opentelemetry.io/otel/trace" |
| 21 |
nooptrace "go.opentelemetry.io/otel/trace/noop" |
| 22 |
yaml "go.yaml.in/yaml/v3" |
| 23 |
|
| 24 |
"go.opentelemetry.io/contrib/otelconf/internal/provider" |
| 25 |
) |
| 26 |
|
| 27 |
const envVarConfigFile = "OTEL_EXPERIMENTAL_CONFIG_FILE" |
| 28 |
|
| 29 |
// SDK is a struct that contains all the providers |
| 30 |
// configured via the configuration model. |
| 31 |
type SDK struct { |
| 32 |
meterProvider metric.MeterProvider |
| 33 |
tracerProvider trace.TracerProvider |
| 34 |
loggerProvider log.LoggerProvider |
| 35 |
propagator propagation.TextMapPropagator |
| 36 |
shutdown shutdownFunc |
| 37 |
} |
| 38 |
|
| 39 |
// TracerProvider returns a configured trace.TracerProvider. |
| 40 |
func (s *SDK) TracerProvider() trace.TracerProvider { |
| 41 |
return s.tracerProvider |
| 42 |
} |
| 43 |
|
| 44 |
// MeterProvider returns a configured metric.MeterProvider. |
| 45 |
func (s *SDK) MeterProvider() metric.MeterProvider { |
| 46 |
return s.meterProvider |
| 47 |
} |
| 48 |
|
| 49 |
// LoggerProvider returns a configured log.LoggerProvider. |
| 50 |
func (s *SDK) LoggerProvider() log.LoggerProvider { |
| 51 |
return s.loggerProvider |
| 52 |
} |
| 53 |
|
| 54 |
// Propagator returns a configured propagation.TextMapPropagator. |
| 55 |
func (s *SDK) Propagator() propagation.TextMapPropagator { |
| 56 |
return s.propagator |
| 57 |
} |
| 58 |
|
| 59 |
// Shutdown calls shutdown on all configured providers. |
| 60 |
func (s *SDK) Shutdown(ctx context.Context) error { |
| 61 |
return s.shutdown(ctx) |
| 62 |
} |
| 63 |
|
| 64 |
var noopSDK = SDK{ |
| 65 |
loggerProvider: nooplog.LoggerProvider{}, |
| 66 |
meterProvider: noopmetric.MeterProvider{}, |
| 67 |
tracerProvider: nooptrace.TracerProvider{}, |
| 68 |
propagator: propagation.NewCompositeTextMapPropagator(), |
| 69 |
shutdown: func(context.Context) error { return nil }, |
| 70 |
} |
| 71 |
|
| 72 |
func parseConfigFileFromEnvironment(filename string) (ConfigurationOption, error) { |
| 73 |
b, err := os.ReadFile(filename) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
// Parse a configuration file into an OpenTelemetryConfiguration model. |
| 79 |
c, err := ParseYAML(b) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
|
| 84 |
// Create SDK components with the parsed configuration. |
| 85 |
return WithOpenTelemetryConfiguration(*c), nil |
| 86 |
} |
| 87 |
|
| 88 |
// NewSDK creates SDK providers based on the configuration model. It checks the local environment and |
| 89 |
// uses the file set in the variable `OTEL_EXPERIMENTAL_CONFIG_FILE` to configure the SDK automatically. |
| 90 |
// Any file defined by `OTEL_EXPERIMENTAL_CONFIG_FILE` will supersede all files passed with |
| 91 |
// [WithOpenTelemetryConfiguration]. |
| 92 |
func NewSDK(opts ...ConfigurationOption) (SDK, error) { |
| 93 |
filename, ok := os.LookupEnv(envVarConfigFile) |
| 94 |
if ok { |
| 95 |
opt, err := parseConfigFileFromEnvironment(filename) |
| 96 |
if err != nil { |
| 97 |
return noopSDK, err |
| 98 |
} |
| 99 |
opts = append(opts, opt) |
| 100 |
} |
| 101 |
o := configOptions{ |
| 102 |
ctx: context.Background(), |
| 103 |
} |
| 104 |
for _, opt := range opts { |
| 105 |
o = opt.apply(o) |
| 106 |
} |
| 107 |
if o.opentelemetryConfig.Disabled != nil && *o.opentelemetryConfig.Disabled { |
| 108 |
return noopSDK, nil |
| 109 |
} |
| 110 |
|
| 111 |
r, err := newResource(o.opentelemetryConfig.Resource) |
| 112 |
if err != nil { |
| 113 |
return noopSDK, err |
| 114 |
} |
| 115 |
|
| 116 |
p, err := newPropagator(o.opentelemetryConfig.Propagator) |
| 117 |
if err != nil { |
| 118 |
return noopSDK, err |
| 119 |
} |
| 120 |
|
| 121 |
mp, mpShutdown, err := meterProvider(o, r) |
| 122 |
if err != nil { |
| 123 |
return noopSDK, err |
| 124 |
} |
| 125 |
|
| 126 |
tp, tpShutdown, err := tracerProvider(o, r) |
| 127 |
if err != nil { |
| 128 |
return noopSDK, err |
| 129 |
} |
| 130 |
|
| 131 |
lp, lpShutdown, err := loggerProvider(o, r) |
| 132 |
if err != nil { |
| 133 |
return noopSDK, err |
| 134 |
} |
| 135 |
|
| 136 |
return SDK{ |
| 137 |
meterProvider: mp, |
| 138 |
tracerProvider: tp, |
| 139 |
loggerProvider: lp, |
| 140 |
propagator: p, |
| 141 |
shutdown: func(ctx context.Context) error { |
| 142 |
return errors.Join(mpShutdown(ctx), tpShutdown(ctx), lpShutdown(ctx)) |
| 143 |
}, |
| 144 |
}, nil |
| 145 |
} |
| 146 |
|
| 147 |
// ConfigurationOption configures options for providers. |
| 148 |
type ConfigurationOption interface { |
| 149 |
apply(configOptions) configOptions |
| 150 |
} |
| 151 |
|
| 152 |
type configurationOptionFunc func(configOptions) configOptions |
| 153 |
|
| 154 |
func (fn configurationOptionFunc) apply(cfg configOptions) configOptions { |
| 155 |
return fn(cfg) |
| 156 |
} |
| 157 |
|
| 158 |
// WithContext sets the context.Context for the SDK. |
| 159 |
func WithContext(ctx context.Context) ConfigurationOption { |
| 160 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 161 |
c.ctx = ctx |
| 162 |
return c |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
// WithOpenTelemetryConfiguration sets the OpenTelemetryConfiguration used |
| 167 |
// to produce the SDK. |
| 168 |
func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) ConfigurationOption { |
| 169 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 170 |
c.opentelemetryConfig = cfg |
| 171 |
return c |
| 172 |
}) |
| 173 |
} |
| 174 |
|
| 175 |
// WithLoggerProviderOptions appends LoggerProviderOptions used for constructing |
| 176 |
// the LoggerProvider. OpenTelemetryConfiguration takes precedence over these options. |
| 177 |
func WithLoggerProviderOptions(opts ...sdklog.LoggerProviderOption) ConfigurationOption { |
| 178 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 179 |
c.loggerProviderOptions = append(c.loggerProviderOptions, opts...) |
| 180 |
return c |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
// WithMeterProviderOptions appends metric.Options used for constructing the |
| 185 |
// MeterProvider. OpenTelemetryConfiguration takes precedence over these options. |
| 186 |
func WithMeterProviderOptions(opts ...sdkmetric.Option) ConfigurationOption { |
| 187 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 188 |
c.meterProviderOptions = append(c.meterProviderOptions, opts...) |
| 189 |
return c |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
// WithTracerProviderOptions appends TracerProviderOptions used for constructing |
| 194 |
// the TracerProvider. OpenTelemetryConfiguration takes precedence over these options. |
| 195 |
func WithTracerProviderOptions(opts ...sdktrace.TracerProviderOption) ConfigurationOption { |
| 196 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 197 |
c.tracerProviderOptions = append(c.tracerProviderOptions, opts...) |
| 198 |
return c |
| 199 |
}) |
| 200 |
} |
| 201 |
|
| 202 |
// ParseYAML parses a YAML configuration file into an OpenTelemetryConfiguration. |
| 203 |
func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) { |
| 204 |
file, err := provider.ReplaceEnvVars(file) |
| 205 |
if err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
var cfg OpenTelemetryConfiguration |
| 209 |
err = yaml.Unmarshal(file, &cfg) |
| 210 |
if err != nil { |
| 211 |
return nil, err |
| 212 |
} |
| 213 |
|
| 214 |
return &cfg, nil |
| 215 |
} |