| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelconf // import "go.opentelemetry.io/contrib/otelconf/v0.2.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 |
) |
| 21 |
|
| 22 |
func tracerProvider(cfg configOptions, res *resource.Resource) (trace.TracerProvider, shutdownFunc, error) { |
| 23 |
if cfg.opentelemetryConfig.TracerProvider == nil { |
| 24 |
return noop.NewTracerProvider(), noopShutdown, nil |
| 25 |
} |
| 26 |
opts := []sdktrace.TracerProviderOption{ |
| 27 |
sdktrace.WithResource(res), |
| 28 |
} |
| 29 |
var errs []error |
| 30 |
for _, processor := range cfg.opentelemetryConfig.TracerProvider.Processors { |
| 31 |
sp, err := spanProcessor(cfg.ctx, processor) |
| 32 |
if err == nil { |
| 33 |
opts = append(opts, sdktrace.WithSpanProcessor(sp)) |
| 34 |
} else { |
| 35 |
errs = append(errs, err) |
| 36 |
} |
| 37 |
} |
| 38 |
if len(errs) > 0 { |
| 39 |
return noop.NewTracerProvider(), noopShutdown, errors.Join(errs...) |
| 40 |
} |
| 41 |
tp := sdktrace.NewTracerProvider(opts...) |
| 42 |
return tp, tp.Shutdown, nil |
| 43 |
} |
| 44 |
|
| 45 |
func spanExporter(ctx context.Context, exporter SpanExporter) (sdktrace.SpanExporter, error) { |
| 46 |
if exporter.Console != nil && exporter.OTLP != nil { |
| 47 |
return nil, errors.New("must not specify multiple exporters") |
| 48 |
} |
| 49 |
|
| 50 |
if exporter.Console != nil { |
| 51 |
return stdouttrace.New( |
| 52 |
stdouttrace.WithPrettyPrint(), |
| 53 |
) |
| 54 |
} |
| 55 |
if exporter.OTLP != nil { |
| 56 |
switch exporter.OTLP.Protocol { |
| 57 |
case protocolProtobufHTTP: |
| 58 |
return otlpHTTPSpanExporter(ctx, exporter.OTLP) |
| 59 |
case protocolProtobufGRPC: |
| 60 |
return otlpGRPCSpanExporter(ctx, exporter.OTLP) |
| 61 |
default: |
| 62 |
return nil, fmt.Errorf("unsupported protocol %q", exporter.OTLP.Protocol) |
| 63 |
} |
| 64 |
} |
| 65 |
return nil, errors.New("no valid span exporter") |
| 66 |
} |
| 67 |
|
| 68 |
func spanProcessor(ctx context.Context, processor SpanProcessor) (sdktrace.SpanProcessor, error) { |
| 69 |
if processor.Batch != nil && processor.Simple != nil { |
| 70 |
return nil, errors.New("must not specify multiple span processor type") |
| 71 |
} |
| 72 |
if processor.Batch != nil { |
| 73 |
exp, err := spanExporter(ctx, processor.Batch.Exporter) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
return batchSpanProcessor(processor.Batch, exp) |
| 78 |
} |
| 79 |
if processor.Simple != nil { |
| 80 |
exp, err := spanExporter(ctx, processor.Simple.Exporter) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
return sdktrace.NewSimpleSpanProcessor(exp), nil |
| 85 |
} |
| 86 |
return nil, errors.New("unsupported span processor type, must be one of simple or batch") |
| 87 |
} |
| 88 |
|
| 89 |
func otlpGRPCSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanExporter, error) { |
| 90 |
var opts []otlptracegrpc.Option |
| 91 |
|
| 92 |
if otlpConfig.Endpoint != "" { |
| 93 |
u, err := url.ParseRequestURI(otlpConfig.Endpoint) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
// ParseRequestURI leaves the Host field empty when no |
| 98 |
// scheme is specified (i.e. localhost:4317). This check is |
| 99 |
// here to support the case where a user may not specify a |
| 100 |
// scheme. The code does its best effort here by using |
| 101 |
// otlpConfig.Endpoint as-is in that case. |
| 102 |
if u.Host != "" { |
| 103 |
opts = append(opts, otlptracegrpc.WithEndpoint(u.Host)) |
| 104 |
} else { |
| 105 |
opts = append(opts, otlptracegrpc.WithEndpoint(otlpConfig.Endpoint)) |
| 106 |
} |
| 107 |
|
| 108 |
if u.Scheme == "http" { |
| 109 |
opts = append(opts, otlptracegrpc.WithInsecure()) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if otlpConfig.Compression != nil { |
| 114 |
switch *otlpConfig.Compression { |
| 115 |
case compressionGzip: |
| 116 |
opts = append(opts, otlptracegrpc.WithCompressor(*otlpConfig.Compression)) |
| 117 |
case compressionNone: |
| 118 |
// none requires no options |
| 119 |
default: |
| 120 |
return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 121 |
} |
| 122 |
} |
| 123 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 124 |
opts = append(opts, otlptracegrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 125 |
} |
| 126 |
if len(otlpConfig.Headers) > 0 { |
| 127 |
opts = append(opts, otlptracegrpc.WithHeaders(otlpConfig.Headers)) |
| 128 |
} |
| 129 |
|
| 130 |
return otlptracegrpc.New(ctx, opts...) |
| 131 |
} |
| 132 |
|
| 133 |
func otlpHTTPSpanExporter(ctx context.Context, otlpConfig *OTLP) (sdktrace.SpanExporter, error) { |
| 134 |
var opts []otlptracehttp.Option |
| 135 |
|
| 136 |
if otlpConfig.Endpoint != "" { |
| 137 |
u, err := url.ParseRequestURI(otlpConfig.Endpoint) |
| 138 |
if err != nil { |
| 139 |
return nil, err |
| 140 |
} |
| 141 |
opts = append(opts, otlptracehttp.WithEndpoint(u.Host)) |
| 142 |
|
| 143 |
if u.Scheme == "http" { |
| 144 |
opts = append(opts, otlptracehttp.WithInsecure()) |
| 145 |
} |
| 146 |
if u.Path != "" { |
| 147 |
opts = append(opts, otlptracehttp.WithURLPath(u.Path)) |
| 148 |
} |
| 149 |
} |
| 150 |
if otlpConfig.Compression != nil { |
| 151 |
switch *otlpConfig.Compression { |
| 152 |
case compressionGzip: |
| 153 |
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.GzipCompression)) |
| 154 |
case compressionNone: |
| 155 |
opts = append(opts, otlptracehttp.WithCompression(otlptracehttp.NoCompression)) |
| 156 |
default: |
| 157 |
return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression) |
| 158 |
} |
| 159 |
} |
| 160 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 161 |
opts = append(opts, otlptracehttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 162 |
} |
| 163 |
if len(otlpConfig.Headers) > 0 { |
| 164 |
opts = append(opts, otlptracehttp.WithHeaders(otlpConfig.Headers)) |
| 165 |
} |
| 166 |
|
| 167 |
return otlptracehttp.New(ctx, opts...) |
| 168 |
} |
| 169 |
|
| 170 |
func batchSpanProcessor(bsp *BatchSpanProcessor, exp sdktrace.SpanExporter) (sdktrace.SpanProcessor, error) { |
| 171 |
var opts []sdktrace.BatchSpanProcessorOption |
| 172 |
if bsp.ExportTimeout != nil { |
| 173 |
if *bsp.ExportTimeout < 0 { |
| 174 |
return nil, fmt.Errorf("invalid export timeout %d", *bsp.ExportTimeout) |
| 175 |
} |
| 176 |
opts = append(opts, sdktrace.WithExportTimeout(time.Millisecond*time.Duration(*bsp.ExportTimeout))) |
| 177 |
} |
| 178 |
if bsp.MaxExportBatchSize != nil { |
| 179 |
if *bsp.MaxExportBatchSize < 0 { |
| 180 |
return nil, fmt.Errorf("invalid batch size %d", *bsp.MaxExportBatchSize) |
| 181 |
} |
| 182 |
opts = append(opts, sdktrace.WithMaxExportBatchSize(*bsp.MaxExportBatchSize)) |
| 183 |
} |
| 184 |
if bsp.MaxQueueSize != nil { |
| 185 |
if *bsp.MaxQueueSize < 0 { |
| 186 |
return nil, fmt.Errorf("invalid queue size %d", *bsp.MaxQueueSize) |
| 187 |
} |
| 188 |
opts = append(opts, sdktrace.WithMaxQueueSize(*bsp.MaxQueueSize)) |
| 189 |
} |
| 190 |
if bsp.ScheduleDelay != nil { |
| 191 |
if *bsp.ScheduleDelay < 0 { |
| 192 |
return nil, fmt.Errorf("invalid schedule delay %d", *bsp.ScheduleDelay) |
| 193 |
} |
| 194 |
opts = append(opts, sdktrace.WithBatchTimeout(time.Millisecond*time.Duration(*bsp.ScheduleDelay))) |
| 195 |
} |
| 196 |
return sdktrace.NewBatchSpanProcessor(exp, opts...), nil |
| 197 |
} |