Debian Codegraph @

otelconf/log.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelconf // import "go.opentelemetry.io/contrib/otelconf"
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 if cfg.opentelemetryConfig.LoggerProvider.Limits != nil {
46 opts = logProcessorLimits(opts, *cfg.opentelemetryConfig.LoggerProvider.Limits)
47 }
48 lp := sdklog.NewLoggerProvider(opts...)
49 return lp, lp.Shutdown, nil
50 }
51
52 func logProcessorLimits(opts []sdklog.LoggerProviderOption, limits LogRecordLimits) []sdklog.LoggerProviderOption {
53 if limits.AttributeCountLimit != nil {
54 opts = append(opts, sdklog.WithAttributeCountLimit(*limits.AttributeCountLimit))
55 }
56 if limits.AttributeValueLengthLimit != nil {
57 opts = append(opts, sdklog.WithAttributeValueLengthLimit(*limits.AttributeValueLengthLimit))
58 }
59 return opts
60 }
61
62 func logProcessor(ctx context.Context, processor LogRecordProcessor) (sdklog.Processor, error) {
63 if processor.Batch != nil && processor.Simple != nil {
64 return nil, newErrInvalid("must not specify multiple log processor type")
65 }
66 if processor.Batch != nil {
67 exp, err := logExporter(ctx, processor.Batch.Exporter)
68 if err != nil {
69 return nil, err
70 }
71 return batchLogProcessor(processor.Batch, exp)
72 }
73 if processor.Simple != nil {
74 exp, err := logExporter(ctx, processor.Simple.Exporter)
75 if err != nil {
76 return nil, err
77 }
78 return sdklog.NewSimpleProcessor(exp), nil
79 }
80 return nil, newErrInvalid("unsupported log processor type, must be one of simple or batch")
81 }
82
83 func logExporter(ctx context.Context, exporter LogRecordExporter) (sdklog.Exporter, error) {
84 exportersConfigured := 0
85 var exportFunc func() (sdklog.Exporter, error)
86
87 if exporter.Console != nil {
88 exportersConfigured++
89 exportFunc = func() (sdklog.Exporter, error) {
90 return stdoutlog.New(
91 stdoutlog.WithPrettyPrint(),
92 )
93 }
94 }
95
96 if exporter.OTLPHttp != nil {
97 exportersConfigured++
98 exportFunc = func() (sdklog.Exporter, error) {
99 return otlpHTTPLogExporter(ctx, exporter.OTLPHttp)
100 }
101 }
102 if exporter.OTLPGrpc != nil {
103 exportersConfigured++
104 exportFunc = func() (sdklog.Exporter, error) {
105 return otlpGRPCLogExporter(ctx, exporter.OTLPGrpc)
106 }
107 }
108
109 if exportersConfigured > 1 {
110 return nil, newErrInvalid("must not specify multiple exporters")
111 }
112
113 if exportFunc != nil {
114 return exportFunc()
115 }
116
117 return nil, newErrInvalid("no valid log exporter")
118 }
119
120 func batchLogProcessor(blp *BatchLogRecordProcessor, exp sdklog.Exporter) (*sdklog.BatchProcessor, error) {
121 var opts []sdklog.BatchProcessorOption
122 if err := validateBatchLogRecordProcessor(blp); err != nil {
123 return nil, err
124 }
125 if blp.ExportTimeout != nil {
126 opts = append(opts, sdklog.WithExportTimeout(time.Millisecond*time.Duration(*blp.ExportTimeout)))
127 }
128 if blp.MaxExportBatchSize != nil {
129 opts = append(opts, sdklog.WithExportMaxBatchSize(*blp.MaxExportBatchSize))
130 }
131 if blp.MaxQueueSize != nil {
132 opts = append(opts, sdklog.WithMaxQueueSize(*blp.MaxQueueSize))
133 }
134
135 if blp.ScheduleDelay != nil {
136 opts = append(opts, sdklog.WithExportInterval(time.Millisecond*time.Duration(*blp.ScheduleDelay)))
137 }
138
139 return sdklog.NewBatchProcessor(exp, opts...), nil
140 }
141
142 func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLPHttpExporter) (sdklog.Exporter, error) {
143 var opts []otlploghttp.Option
144
145 if otlpConfig.Endpoint != nil {
146 u, err := url.ParseRequestURI(*otlpConfig.Endpoint)
147 if err != nil {
148 return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err)
149 }
150 opts = append(opts, otlploghttp.WithEndpoint(u.Host))
151
152 if u.Scheme == "http" {
153 opts = append(opts, otlploghttp.WithInsecure())
154 }
155 if u.Path != "" {
156 opts = append(opts, otlploghttp.WithURLPath(u.Path))
157 }
158 }
159 if otlpConfig.Compression != nil {
160 switch *otlpConfig.Compression {
161 case compressionGzip:
162 opts = append(opts, otlploghttp.WithCompression(otlploghttp.GzipCompression))
163 case compressionNone:
164 opts = append(opts, otlploghttp.WithCompression(otlploghttp.NoCompression))
165 default:
166 return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression))
167 }
168 }
169 if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
170 opts = append(opts, otlploghttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
171 }
172 headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
173 if err != nil {
174 return nil, err
175 }
176 if len(headersConfig) > 0 {
177 opts = append(opts, otlploghttp.WithHeaders(headersConfig))
178 }
179
180 if otlpConfig.Tls != nil {
181 tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile)
182 if err != nil {
183 return nil, errors.Join(newErrInvalid("tls configuration"), err)
184 }
185 opts = append(opts, otlploghttp.WithTLSClientConfig(tlsConfig))
186 }
187
188 return otlploghttp.New(ctx, opts...)
189 }
190
191 func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLPGrpcExporter) (sdklog.Exporter, error) {
192 var opts []otlploggrpc.Option
193
194 if otlpConfig.Endpoint != nil {
195 u, err := url.ParseRequestURI(*otlpConfig.Endpoint)
196 if err != nil {
197 return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err)
198 }
199 // ParseRequestURI leaves the Host field empty when no
200 // scheme is specified (i.e. localhost:4317). This check is
201 // here to support the case where a user may not specify a
202 // scheme. The code does its best effort here by using
203 // otlpConfig.Endpoint as-is in that case
204 if u.Host != "" {
205 opts = append(opts, otlploggrpc.WithEndpoint(u.Host))
206 } else {
207 opts = append(opts, otlploggrpc.WithEndpoint(*otlpConfig.Endpoint))
208 }
209 if u.Scheme == "http" || (u.Scheme != "https" && otlpConfig.Tls != nil && otlpConfig.Tls.Insecure != nil && *otlpConfig.Tls.Insecure) {
210 opts = append(opts, otlploggrpc.WithInsecure())
211 }
212 }
213 if otlpConfig.Compression != nil {
214 switch *otlpConfig.Compression {
215 case compressionGzip:
216 opts = append(opts, otlploggrpc.WithCompressor(*otlpConfig.Compression))
217 case compressionNone:
218 // none requires no options
219 default:
220 return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression))
221 }
222 }
223 if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
224 opts = append(opts, otlploggrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
225 }
226 headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
227 if err != nil {
228 return nil, err
229 }
230 if len(headersConfig) > 0 {
231 opts = append(opts, otlploggrpc.WithHeaders(headersConfig))
232 }
233
234 if otlpConfig.Tls != nil && (otlpConfig.Tls.CaFile != nil || otlpConfig.Tls.CertFile != nil || otlpConfig.Tls.KeyFile != nil) {
235 tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile)
236 if err != nil {
237 return nil, errors.Join(newErrInvalid("tls configuration"), err)
238 }
239 opts = append(opts, otlploggrpc.WithTLSCredentials(credentials.NewTLS(tlsConfig)))
240 }
241
242 return otlploggrpc.New(ctx, opts...)
243 }