| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package x // import "go.opentelemetry.io/contrib/otelconf/x" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"net/url" |
| 11 |
"time" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" |
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp" |
| 15 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog" |
| 16 |
"go.opentelemetry.io/otel/log" |
| 17 |
"go.opentelemetry.io/otel/log/noop" |
| 18 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 19 |
"go.opentelemetry.io/otel/sdk/resource" |
| 20 |
"google.golang.org/grpc/credentials" |
| 21 |
|
| 22 |
"go.opentelemetry.io/contrib/otelconf/internal/tls" |
| 23 |
) |
| 24 |
|
| 25 |
func loggerProvider(cfg configOptions, res *resource.Resource) (log.LoggerProvider, shutdownFunc, error) { |
| 26 |
if cfg.opentelemetryConfig.LoggerProvider == nil { |
| 27 |
return noop.NewLoggerProvider(), noopShutdown, nil |
| 28 |
} |
| 29 |
opts := append(cfg.loggerProviderOptions, sdklog.WithResource(res)) |
| 30 |
|
| 31 |
var errs []error |
| 32 |
for _, processor := range cfg.opentelemetryConfig.LoggerProvider.Processors { |
| 33 |
sp, err := logProcessor(cfg.ctx, processor) |
| 34 |
if err == nil { |
| 35 |
opts = append(opts, sdklog.WithProcessor(sp)) |
| 36 |
} else { |
| 37 |
errs = append(errs, err) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if len(errs) > 0 { |
| 42 |
return noop.NewLoggerProvider(), noopShutdown, errors.Join(errs...) |
| 43 |
} |
| 44 |
|
| 45 |
lp := sdklog.NewLoggerProvider(opts...) |
| 46 |
return lp, lp.Shutdown, nil |
| 47 |
} |
| 48 |
|
| 49 |
func logProcessor(ctx context.Context, processor LogRecordProcessor) (sdklog.Processor, error) { |
| 50 |
if processor.Batch != nil && processor.Simple != nil { |
| 51 |
return nil, newErrInvalid("must not specify multiple log processor type") |
| 52 |
} |
| 53 |
if processor.Batch != nil { |
| 54 |
exp, err := logExporter(ctx, processor.Batch.Exporter) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
return batchLogProcessor(processor.Batch, exp) |
| 59 |
} |
| 60 |
if processor.Simple != nil { |
| 61 |
exp, err := logExporter(ctx, processor.Simple.Exporter) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
return sdklog.NewSimpleProcessor(exp), nil |
| 66 |
} |
| 67 |
return nil, newErrInvalid("unsupported log processor type, must be one of simple or batch") |
| 68 |
} |
| 69 |
|
| 70 |
func logExporter(ctx context.Context, exporter LogRecordExporter) (sdklog.Exporter, error) { |
| 71 |
exportersConfigured := 0 |
| 72 |
var exportFunc func() (sdklog.Exporter, error) |
| 73 |
|
| 74 |
if exporter.Console != nil { |
| 75 |
exportersConfigured++ |
| 76 |
exportFunc = func() (sdklog.Exporter, error) { |
| 77 |
return stdoutlog.New( |
| 78 |
stdoutlog.WithPrettyPrint(), |
| 79 |
) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if exporter.OTLPHttp != nil { |
| 84 |
exportersConfigured++ |
| 85 |
exportFunc = func() (sdklog.Exporter, error) { |
| 86 |
return otlpHTTPLogExporter(ctx, exporter.OTLPHttp) |
| 87 |
} |
| 88 |
} |
| 89 |
if exporter.OTLPGrpc != nil { |
| 90 |
exportersConfigured++ |
| 91 |
exportFunc = func() (sdklog.Exporter, error) { |
| 92 |
return otlpGRPCLogExporter(ctx, exporter.OTLPGrpc) |
| 93 |
} |
| 94 |
} |
| 95 |
if exporter.OTLPFileDevelopment != nil { |
| 96 |
// TODO: implement file exporter https://github.com/open-telemetry/opentelemetry-go/issues/5408 |
| 97 |
return nil, newErrInvalid("otlp_file/development") |
| 98 |
} |
| 99 |
|
| 100 |
if exportersConfigured > 1 { |
| 101 |
return nil, newErrInvalid("must not specify multiple exporters") |
| 102 |
} |
| 103 |
|
| 104 |
if exportFunc != nil { |
| 105 |
return exportFunc() |
| 106 |
} |
| 107 |
|
| 108 |
return nil, newErrInvalid("no valid log exporter") |
| 109 |
} |
| 110 |
|
| 111 |
func batchLogProcessor(blp *BatchLogRecordProcessor, exp sdklog.Exporter) (*sdklog.BatchProcessor, error) { |
| 112 |
var opts []sdklog.BatchProcessorOption |
| 113 |
if err := validateBatchLogRecordProcessor(blp); err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
if blp.ExportTimeout != nil { |
| 117 |
opts = append(opts, sdklog.WithExportTimeout(time.Millisecond*time.Duration(*blp.ExportTimeout))) |
| 118 |
} |
| 119 |
if blp.MaxExportBatchSize != nil { |
| 120 |
opts = append(opts, sdklog.WithExportMaxBatchSize(*blp.MaxExportBatchSize)) |
| 121 |
} |
| 122 |
if blp.MaxQueueSize != nil { |
| 123 |
opts = append(opts, sdklog.WithMaxQueueSize(*blp.MaxQueueSize)) |
| 124 |
} |
| 125 |
|
| 126 |
if blp.ScheduleDelay != nil { |
| 127 |
opts = append(opts, sdklog.WithExportInterval(time.Millisecond*time.Duration(*blp.ScheduleDelay))) |
| 128 |
} |
| 129 |
|
| 130 |
return sdklog.NewBatchProcessor(exp, opts...), nil |
| 131 |
} |
| 132 |
|
| 133 |
func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLPHttpExporter) (sdklog.Exporter, error) { |
| 134 |
var opts []otlploghttp.Option |
| 135 |
|
| 136 |
if otlpConfig.Endpoint != nil { |
| 137 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 138 |
if err != nil { |
| 139 |
return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err) |
| 140 |
} |
| 141 |
opts = append(opts, otlploghttp.WithEndpoint(u.Host)) |
| 142 |
|
| 143 |
if u.Scheme == "http" { |
| 144 |
opts = append(opts, otlploghttp.WithInsecure()) |
| 145 |
} |
| 146 |
if u.Path != "" { |
| 147 |
opts = append(opts, otlploghttp.WithURLPath(u.Path)) |
| 148 |
} |
| 149 |
} |
| 150 |
if otlpConfig.Compression != nil { |
| 151 |
switch *otlpConfig.Compression { |
| 152 |
case compressionGzip: |
| 153 |
opts = append(opts, otlploghttp.WithCompression(otlploghttp.GzipCompression)) |
| 154 |
case compressionNone: |
| 155 |
opts = append(opts, otlploghttp.WithCompression(otlploghttp.NoCompression)) |
| 156 |
default: |
| 157 |
return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression)) |
| 158 |
} |
| 159 |
} |
| 160 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 161 |
opts = append(opts, otlploghttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 162 |
} |
| 163 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 164 |
if err != nil { |
| 165 |
return nil, err |
| 166 |
} |
| 167 |
if len(headersConfig) > 0 { |
| 168 |
opts = append(opts, otlploghttp.WithHeaders(headersConfig)) |
| 169 |
} |
| 170 |
|
| 171 |
if otlpConfig.Tls != nil { |
| 172 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile) |
| 173 |
if err != nil { |
| 174 |
return nil, errors.Join(newErrInvalid("tls configuration"), err) |
| 175 |
} |
| 176 |
opts = append(opts, otlploghttp.WithTLSClientConfig(tlsConfig)) |
| 177 |
} |
| 178 |
|
| 179 |
return otlploghttp.New(ctx, opts...) |
| 180 |
} |
| 181 |
|
| 182 |
func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLPGrpcExporter) (sdklog.Exporter, error) { |
| 183 |
var opts []otlploggrpc.Option |
| 184 |
|
| 185 |
if otlpConfig.Endpoint != nil { |
| 186 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 187 |
if err != nil { |
| 188 |
return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err) |
| 189 |
} |
| 190 |
// ParseRequestURI leaves the Host field empty when no |
| 191 |
// scheme is specified (i.e. localhost:4317). This check is |
| 192 |
// here to support the case where a user may not specify a |
| 193 |
// scheme. The code does its best effort here by using |
| 194 |
// otlpConfig.Endpoint as-is in that case |
| 195 |
if u.Host != "" { |
| 196 |
opts = append(opts, otlploggrpc.WithEndpoint(u.Host)) |
| 197 |
} else { |
| 198 |
opts = append(opts, otlploggrpc.WithEndpoint(*otlpConfig.Endpoint)) |
| 199 |
} |
| 200 |
if u.Scheme == "http" || (u.Scheme != "https" && otlpConfig.Tls != nil && otlpConfig.Tls.Insecure != nil && *otlpConfig.Tls.Insecure) { |
| 201 |
opts = append(opts, otlploggrpc.WithInsecure()) |
| 202 |
} |
| 203 |
} |
| 204 |
if otlpConfig.Compression != nil { |
| 205 |
switch *otlpConfig.Compression { |
| 206 |
case compressionGzip: |
| 207 |
opts = append(opts, otlploggrpc.WithCompressor(*otlpConfig.Compression)) |
| 208 |
case compressionNone: |
| 209 |
// none requires no options |
| 210 |
default: |
| 211 |
return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression)) |
| 212 |
} |
| 213 |
} |
| 214 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 215 |
opts = append(opts, otlploggrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 216 |
} |
| 217 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 218 |
if err != nil { |
| 219 |
return nil, err |
| 220 |
} |
| 221 |
if len(headersConfig) > 0 { |
| 222 |
opts = append(opts, otlploggrpc.WithHeaders(headersConfig)) |
| 223 |
} |
| 224 |
|
| 225 |
if otlpConfig.Tls != nil && (otlpConfig.Tls.CaFile != nil || otlpConfig.Tls.CertFile != nil || otlpConfig.Tls.KeyFile != nil) { |
| 226 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile) |
| 227 |
if err != nil { |
| 228 |
return nil, errors.Join(newErrInvalid("tls configuration"), err) |
| 229 |
} |
| 230 |
opts = append(opts, otlploggrpc.WithTLSCredentials(credentials.NewTLS(tlsConfig))) |
| 231 |
} |
| 232 |
|
| 233 |
return otlploggrpc.New(ctx, opts...) |
| 234 |
} |