| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.3.0" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"time" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" |
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 15 |
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace" |
| 16 |
"go.opentelemetry.io/otel/sdk/resource" |
| 17 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 18 |
"go.opentelemetry.io/otel/trace" |
| 19 |
"go.opentelemetry.io/otel/trace/noop" |
| 20 |
"google.golang.org/grpc/credentials" |
| 21 |
|
| 22 |
"go.opentelemetry.io/contrib/otelconf/internal/tls" |
| 23 |
) |
| 24 |
|
| 25 |
var errInvalidSamplerConfiguration = errors.New("invalid sampler configuration") |
| 26 |
|
| 27 |
func tracerProvider(cfg configOptions, res *resource.Resource) (trace.TracerProvider, shutdownFunc, error) { |
| 28 |
if cfg.opentelemetryConfig.TracerProvider == nil { |
| 29 |
return noop.NewTracerProvider(), noopShutdown, nil |
| 30 |
} |
| 31 |
opts := append(cfg.tracerProviderOptions, sdktrace.WithResource(res)) |
| 32 |
|
| 33 |
var errs []error |
| 34 |
for _, processor := range cfg.opentelemetryConfig.TracerProvider.Processors { |
| 35 |
sp, err := spanProcessor(cfg.ctx, processor) |
| 36 |
if err == nil { |
| 37 |
opts = append(opts, sdktrace.WithSpanProcessor(sp)) |
| 38 |
} else { |
| 39 |
errs = append(errs, err) |
| 40 |
} |
| 41 |
} |
| 42 |
if s, err := sampler(cfg.opentelemetryConfig.TracerProvider.Sampler); err == nil { |
| 43 |
opts = append(opts, sdktrace.WithSampler(s)) |
| 44 |
} else { |
| 45 |
errs = append(errs, err) |
| 46 |
} |
| 47 |
if len(errs) > 0 { |
| 48 |
return noop.NewTracerProvider(), noopShutdown, errors.Join(errs...) |
| 49 |
} |
| 50 |
tp := sdktrace.NewTracerProvider(opts...) |
| 51 |
return tp, tp.Shutdown, nil |
| 52 |
} |
| 53 |
|
| 54 |
func parentBasedSampler(s *SamplerParentBased) (sdktrace.Sampler, error) { |
| 55 |
var rootSampler sdktrace.Sampler |
| 56 |
var opts []sdktrace.ParentBasedSamplerOption |
| 57 |
var errs []error |
| 58 |
var err error |
| 59 |
|
| 60 |
if s.Root == nil { |
| 61 |
rootSampler = sdktrace.AlwaysSample() |
| 62 |
} else { |
| 63 |
rootSampler, err = sampler(s.Root) |
| 64 |
if err != nil { |
| 65 |
errs = append(errs, err) |
| 66 |
} |
| 67 |
} |
| 68 |
if s.RemoteParentSampled != nil { |
| 69 |
remoteParentSampler, err := sampler(s.RemoteParentSampled) |
| 70 |
if err != nil { |
| 71 |
errs = append(errs, err) |
| 72 |
} else { |
| 73 |
opts = append(opts, sdktrace.WithRemoteParentSampled(remoteParentSampler)) |
| 74 |
} |
| 75 |
} |
| 76 |
if s.RemoteParentNotSampled != nil { |
| 77 |
remoteParentNotSampler, err := sampler(s.RemoteParentNotSampled) |
| 78 |
if err != nil { |
| 79 |
errs = append(errs, err) |
| 80 |
} else { |
| 81 |
opts = append(opts, sdktrace.WithRemoteParentNotSampled(remoteParentNotSampler)) |
| 82 |
} |
| 83 |
} |
| 84 |
if s.LocalParentSampled != nil { |
| 85 |
localParentSampler, err := sampler(s.LocalParentSampled) |
| 86 |
if err != nil { |
| 87 |
errs = append(errs, err) |
| 88 |
} else { |
| 89 |
opts = append(opts, sdktrace.WithLocalParentSampled(localParentSampler)) |
| 90 |
} |
| 91 |
} |
| 92 |
if s.LocalParentNotSampled != nil { |
| 93 |
localParentNotSampler, err := sampler(s.LocalParentNotSampled) |
| 94 |
if err != nil { |
| 95 |
errs = append(errs, err) |
| 96 |
} else { |
| 97 |
opts = append(opts, sdktrace.WithLocalParentNotSampled(localParentNotSampler)) |
| 98 |
} |
| 99 |
} |
| 100 |
if len(errs) > 0 { |
| 101 |
return nil, errors.Join(errs...) |
| 102 |
} |
| 103 |
return sdktrace.ParentBased(rootSampler, opts...), nil |
| 104 |
} |
| 105 |
|
| 106 |
func sampler(s *Sampler) (sdktrace.Sampler, error) { |
| 107 |
if s == nil { |
| 108 |
// If omitted, parent based sampler with a root of always_on is used. |
| 109 |
return sdktrace.ParentBased(sdktrace.AlwaysSample()), nil |
| 110 |
} |
| 111 |
if s.ParentBased != nil { |
| 112 |
return parentBasedSampler(s.ParentBased) |
| 113 |
} |
| 114 |
if s.AlwaysOff != nil { |
| 115 |
return sdktrace.NeverSample(), nil |
| 116 |
} |
| 117 |
if s.AlwaysOn != nil { |
| 118 |
return sdktrace.AlwaysSample(), nil |
| 119 |
} |
| 120 |
if s.TraceIDRatioBased != nil { |
| 121 |
if s.TraceIDRatioBased.Ratio == nil { |
| 122 |
return sdktrace.TraceIDRatioBased(1), nil |
| 123 |
} |
| 124 |
return sdktrace.TraceIDRatioBased(*s.TraceIDRatioBased.Ratio), nil |
| 125 |
} |
| 126 |
return nil, errInvalidSamplerConfiguration |
| 127 |
} |
| 128 |
|
| 129 |
func spanExporter(ctx context.Context, exporter SpanExporter) (sdktrace.SpanExporter, error) { |
| 130 |
if exporter.Console != nil && exporter.OTLP != nil { |
| 131 |
return nil, errors.New("must not specify multiple exporters") |
| 132 |
} |
| 133 |
|
| 134 |
if exporter.Console != nil { |
| 135 |
return stdouttrace.New( |
| 136 |
stdouttrace.WithPrettyPrint(), |
| 137 |
) |
| 138 |
} |
| 139 |
if exporter.OTLP != nil && exporter.OTLP.Protocol != nil { |
| 140 |
switch *exporter.OTLP.Protocol { |
| 141 |
case protocolProtobufHTTP: |
| 142 |
return otlpHTTPSpanExporter(ctx, exporter.OTLP) |
| 143 |
case protocolProtobufGRPC: |
| 144 |
return otlpGRPCSpanExporter(ctx, exporter.OTLP) |
| 145 |
default: |
| 146 |
return nil, fmt.Errorf("unsupported protocol %q", *exporter.OTLP.Protocol) |
| 147 |
} |
| 148 |
} |
| 149 |
return nil, errors.New("no valid span exporter") |
| 150 |
} |
| 151 |
|
| 152 |
func spanProcessor(ctx context.Context, processor SpanProcessor) (sdktrace.SpanProcessor, error) { |
| 153 |
if processor.Batch != nil && processor.Simple != nil { |
| 154 |
return nil, errors.New("must not specify multiple span processor type") |
| 155 |
} |
| 156 |
if processor.Batch != nil { |
| 157 |
exp, err := spanExporter(ctx, processor.Batch.Exporter) |
| 158 |
if err != nil { |
| 159 |
return nil, err |
| 160 |
} |
| 161 |
return batchSpanProcessor(processor.Batch, exp) |
| 162 |
} |
| 163 |
if processor.Simple != nil { |
| 164 |
exp, err := spanExporter(ctx, processor.Simple.Exporter) |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
return sdktrace.NewSimpleSpanProcessor(exp), nil |
| 169 |
} |
| 170 |
return nil, errors.New("unsupported span processor type, must be one of simple or batch") |
| 171 |
} |
| 172 |
|
| 173 |
func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanExporter, error) { |
| 174 |
var opts []otlptracegrpc.Option |
| 175 |
|
| 176 |
if otlpConfig.Endpoint != nil { |
| 177 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
// ParseRequestURI leaves the Host field empty when no |
| 182 |
// scheme is specified (i.e. localhost:4317). This check is |
| 183 |
// here to support the case where a user may not specify a |
| 184 |
// scheme. The code does its best effort here by using |
| 185 |
// otlpConfig.Endpoint as-is in that case. |
| 186 |
if u.Host != "" { |
| 187 |
opts = append(opts, otlptracegrpc.WithEndpoint(u.Host)) |
| 188 |
} else { |
| 189 |
opts = append(opts, otlptracegrpc.WithEndpoint(*otlpConfig.Endpoint)) |
| 190 |
} |
| 191 |
|
| 192 |
if u.Scheme == "http" || (u.Scheme != "https" && otlpConfig.Insecure != nil && *otlpConfig.Insecure) { |
| 193 |
opts = append(opts, otlptracegrpc.WithInsecure()) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if otlpConfig.Compression != nil { |
| 198 |
switch *otlpConfig.Compression { |
| 199 |
case compressionGzip: |
| 200 |
opts = append(opts, otlptracegrpc.WithCompressor(*otlpConfig.Compression)) |
| 201 |
case compressionNone: |
| 202 |
// none requires no options |
| 203 |
default: |
| 204 |
return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 205 |
} |
| 206 |
} |
| 207 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 208 |
opts = append(opts, otlptracegrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 209 |
} |
| 210 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 211 |
if err != nil { |
| 212 |
return nil, err |
| 213 |
} |
| 214 |
if len(headersConfig) > 0 { |
| 215 |
opts = append(opts, otlptracegrpc.WithHeaders(headersConfig)) |
| 216 |
} |
| 217 |
|
| 218 |
if otlpConfig.Certificate != nil || otlpConfig.ClientCertificate != nil || otlpConfig.ClientKey != nil { |
| 219 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey) |
| 220 |
if err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
opts = append(opts, otlptracegrpc.WithTLSCredentials(credentials.NewTLS(tlsConfig))) |
| 224 |
} |
| 225 |
|
| 226 |
return otlptracegrpc.New(ctx, opts...) |
| 227 |
} |
| 228 |
|
| 229 |
func otlpHTTPSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanExporter, error) { |
| 230 |
var opts []otlptracehttp.Option |
| 231 |
|
| 232 |
if otlpConfig.Endpoint != nil { |
| 233 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 234 |
if err != nil { |
| 235 |
return nil, err |
| 236 |
} |
| 237 |
opts = append(opts, otlptracehttp.WithEndpoint(u.Host)) |
| 238 |
|
| 239 |
if u.Scheme == "http" { |
| 240 |
opts = append(opts, otlptracehttp.WithInsecure()) |
| 241 |
} else { |
| 242 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey) |
| 243 |
if err != nil { |
| 244 |
return nil, err |
| 245 |
} |
| 246 |
opts = append(opts, otlptracehttp.WithTLSClientConfig(tlsConfig)) |
| 247 |
} |
| 248 |
if u.Path != "" { |
| 249 |
opts = append(opts, otlptracehttp.WithURLPath(u.Path)) |
| 250 |
} |
| 251 |
} |
| 252 |
if otlpConfig.Compression != nil { |
| 253 |
switch *otlpConfig.Compression { |
| 254 |
case compressionGzip: |
| 255 |
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.GzipCompression)) |
| 256 |
case compressionNone: |
| 257 |
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.NoCompression)) |
| 258 |
default: |
| 259 |
return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 260 |
} |
| 261 |
} |
| 262 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 263 |
opts = append(opts, otlptracehttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 264 |
} |
| 265 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 266 |
if err != nil { |
| 267 |
return nil, err |
| 268 |
} |
| 269 |
if len(headersConfig) > 0 { |
| 270 |
opts = append(opts, otlptracehttp.WithHeaders(headersConfig)) |
| 271 |
} |
| 272 |
|
| 273 |
return otlptracehttp.New(ctx, opts...) |
| 274 |
} |
| 275 |
|
| 276 |
func batchSpanProcessor(bsp *BatchSpanProcessor, exp sdktrace.SpanExporter) (sdktrace.SpanProcessor, error) { |
| 277 |
var opts []sdktrace.BatchSpanProcessorOption |
| 278 |
if bsp.ExportTimeout != nil { |
| 279 |
if *bsp.ExportTimeout < 0 { |
| 280 |
return nil, fmt.Errorf("invalid export timeout %d", *bsp.ExportTimeout) |
| 281 |
} |
| 282 |
opts = append(opts, sdktrace.WithExportTimeout(time.Millisecond*time.Duration(*bsp.ExportTimeout))) |
| 283 |
} |
| 284 |
if bsp.MaxExportBatchSize != nil { |
| 285 |
if *bsp.MaxExportBatchSize < 0 { |
| 286 |
return nil, fmt.Errorf("invalid batch size %d", *bsp.MaxExportBatchSize) |
| 287 |
} |
| 288 |
opts = append(opts, sdktrace.WithMaxExportBatchSize(*bsp.MaxExportBatchSize)) |
| 289 |
} |
| 290 |
if bsp.MaxQueueSize != nil { |
| 291 |
if *bsp.MaxQueueSize < 0 { |
| 292 |
return nil, fmt.Errorf("invalid queue size %d", *bsp.MaxQueueSize) |
| 293 |
} |
| 294 |
opts = append(opts, sdktrace.WithMaxQueueSize(*bsp.MaxQueueSize)) |
| 295 |
} |
| 296 |
if bsp.ScheduleDelay != nil { |
| 297 |
if *bsp.ScheduleDelay < 0 { |
| 298 |
return nil, fmt.Errorf("invalid schedule delay %d", *bsp.ScheduleDelay) |
| 299 |
} |
| 300 |
opts = append(opts, sdktrace.WithBatchTimeout(time.Millisecond*time.Duration(*bsp.ScheduleDelay))) |
| 301 |
} |
| 302 |
return sdktrace.NewBatchSpanProcessor(exp, opts...), nil |
| 303 |
} |