| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package otelconf |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/baggage" |
| 13 |
"go.opentelemetry.io/otel/log" |
| 14 |
nooplog "go.opentelemetry.io/otel/log/noop" |
| 15 |
"go.opentelemetry.io/otel/metric" |
| 16 |
noopmetric "go.opentelemetry.io/otel/metric/noop" |
| 17 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 18 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 19 |
sdkresource "go.opentelemetry.io/otel/sdk/resource" |
| 20 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 21 |
"go.opentelemetry.io/otel/trace" |
| 22 |
nooptrace "go.opentelemetry.io/otel/trace/noop" |
| 23 |
yaml "go.yaml.in/yaml/v3" |
| 24 |
|
| 25 |
"go.opentelemetry.io/contrib/otelconf/internal/provider" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
protocolProtobufHTTP = "http/protobuf" |
| 30 |
protocolProtobufGRPC = "grpc" |
| 31 |
|
| 32 |
compressionGzip = "gzip" |
| 33 |
compressionNone = "none" |
| 34 |
) |
| 35 |
|
| 36 |
type configOptions struct { |
| 37 |
ctx context.Context |
| 38 |
opentelemetryConfig OpenTelemetryConfiguration |
| 39 |
loggerProviderOptions []sdklog.LoggerProviderOption |
| 40 |
meterProviderOptions []sdkmetric.Option |
| 41 |
tracerProviderOptions []sdktrace.TracerProviderOption |
| 42 |
} |
| 43 |
|
| 44 |
type shutdownFunc func(context.Context) error |
| 45 |
|
| 46 |
func noopShutdown(context.Context) error { |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
type SDK struct { |
| 53 |
meterProvider metric.MeterProvider |
| 54 |
tracerProvider trace.TracerProvider |
| 55 |
loggerProvider log.LoggerProvider |
| 56 |
resource *sdkresource.Resource |
| 57 |
shutdown shutdownFunc |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func (s *SDK) TracerProvider() trace.TracerProvider { |
| 62 |
return s.tracerProvider |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (s *SDK) MeterProvider() metric.MeterProvider { |
| 67 |
return s.meterProvider |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (s *SDK) LoggerProvider() log.LoggerProvider { |
| 72 |
return s.loggerProvider |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (s *SDK) Resource() *sdkresource.Resource { |
| 78 |
if s.resource == nil { |
| 79 |
return nil |
| 80 |
} |
| 81 |
res := *s.resource |
| 82 |
return &res |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (s *SDK) Shutdown(ctx context.Context) error { |
| 87 |
return s.shutdown(ctx) |
| 88 |
} |
| 89 |
|
| 90 |
var noopSDK = SDK{ |
| 91 |
loggerProvider: nooplog.LoggerProvider{}, |
| 92 |
meterProvider: noopmetric.MeterProvider{}, |
| 93 |
tracerProvider: nooptrace.TracerProvider{}, |
| 94 |
resource: sdkresource.Empty(), |
| 95 |
shutdown: func(context.Context) error { return nil }, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func NewSDK(opts ...ConfigurationOption) (SDK, error) { |
| 100 |
o := configOptions{ |
| 101 |
ctx: context.Background(), |
| 102 |
} |
| 103 |
for _, opt := range opts { |
| 104 |
o = opt.apply(o) |
| 105 |
} |
| 106 |
if o.opentelemetryConfig.Disabled != nil && *o.opentelemetryConfig.Disabled { |
| 107 |
return noopSDK, nil |
| 108 |
} |
| 109 |
|
| 110 |
r := newResource(o.opentelemetryConfig.Resource) |
| 111 |
|
| 112 |
mp, mpShutdown, err := meterProvider(o, r) |
| 113 |
if err != nil { |
| 114 |
return noopSDK, err |
| 115 |
} |
| 116 |
|
| 117 |
tp, tpShutdown, err := tracerProvider(o, r) |
| 118 |
if err != nil { |
| 119 |
return noopSDK, err |
| 120 |
} |
| 121 |
|
| 122 |
lp, lpShutdown, err := loggerProvider(o, r) |
| 123 |
if err != nil { |
| 124 |
return noopSDK, err |
| 125 |
} |
| 126 |
|
| 127 |
return SDK{ |
| 128 |
meterProvider: mp, |
| 129 |
tracerProvider: tp, |
| 130 |
loggerProvider: lp, |
| 131 |
resource: r, |
| 132 |
shutdown: func(ctx context.Context) error { |
| 133 |
return errors.Join(mpShutdown(ctx), tpShutdown(ctx), lpShutdown(ctx)) |
| 134 |
}, |
| 135 |
}, nil |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
type ConfigurationOption interface { |
| 140 |
apply(configOptions) configOptions |
| 141 |
} |
| 142 |
|
| 143 |
type configurationOptionFunc func(configOptions) configOptions |
| 144 |
|
| 145 |
func (fn configurationOptionFunc) apply(cfg configOptions) configOptions { |
| 146 |
return fn(cfg) |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
func WithContext(ctx context.Context) ConfigurationOption { |
| 151 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 152 |
c.ctx = ctx |
| 153 |
return c |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) ConfigurationOption { |
| 160 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 161 |
c.opentelemetryConfig = cfg |
| 162 |
return c |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func WithLoggerProviderOptions(opts ...sdklog.LoggerProviderOption) ConfigurationOption { |
| 169 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 170 |
c.loggerProviderOptions = append(c.loggerProviderOptions, opts...) |
| 171 |
return c |
| 172 |
}) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
func WithMeterProviderOptions(opts ...sdkmetric.Option) ConfigurationOption { |
| 178 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 179 |
c.meterProviderOptions = append(c.meterProviderOptions, opts...) |
| 180 |
return c |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
func WithTracerProviderOptions(opts ...sdktrace.TracerProviderOption) ConfigurationOption { |
| 187 |
return configurationOptionFunc(func(c configOptions) configOptions { |
| 188 |
c.tracerProviderOptions = append(c.tracerProviderOptions, opts...) |
| 189 |
return c |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) { |
| 195 |
file, err := provider.ReplaceEnvVars(file) |
| 196 |
if err != nil { |
| 197 |
return nil, err |
| 198 |
} |
| 199 |
|
| 200 |
var cfg OpenTelemetryConfiguration |
| 201 |
err = yaml.Unmarshal(file, &cfg) |
| 202 |
if err != nil { |
| 203 |
return nil, err |
| 204 |
} |
| 205 |
|
| 206 |
return &cfg, nil |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func createHeadersConfig(headers []NameStringValuePair, headersList *string) (map[string]string, error) { |
| 211 |
result := make(map[string]string) |
| 212 |
if headersList != nil { |
| 213 |
|
| 214 |
headerslist, err := baggage.Parse(*headersList) |
| 215 |
if err != nil { |
| 216 |
return nil, fmt.Errorf("invalid headers list: %w", err) |
| 217 |
} |
| 218 |
for _, kv := range headerslist.Members() { |
| 219 |
result[kv.Key()] = kv.Value() |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if len(headers) > 0 { |
| 224 |
for _, kv := range headers { |
| 225 |
if kv.Value != nil { |
| 226 |
result[kv.Name] = *kv.Value |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
return result, nil |
| 231 |
} |