Debian Codegraph @

otelconf/v0.3.0/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/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/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, errors.New("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, errors.New("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 if exporter.Console != nil && exporter.OTLP != nil {
72 return nil, errors.New("must not specify multiple exporters")
73 }
74
75 if exporter.Console != nil {
76 return stdoutlog.New(
77 stdoutlog.WithPrettyPrint(),
78 )
79 }
80
81 if exporter.OTLP != nil && exporter.OTLP.Protocol != nil {
82 switch *exporter.OTLP.Protocol {
83 case protocolProtobufHTTP:
84 return otlpHTTPLogExporter(ctx, exporter.OTLP)
85 case protocolProtobufGRPC:
86 return otlpGRPCLogExporter(ctx, exporter.OTLP)
87 default:
88 return nil, fmt.Errorf("unsupported protocol %q", *exporter.OTLP.Protocol)
89 }
90 }
91 return nil, errors.New("no valid log exporter")
92 }
93
94 func batchLogProcessor(blp *BatchLogRecordProcessor, exp sdklog.Exporter) (*sdklog.BatchProcessor, error) {
95 var opts []sdklog.BatchProcessorOption
96 if blp.ExportTimeout != nil {
97 if *blp.ExportTimeout < 0 {
98 return nil, fmt.Errorf("invalid export timeout %d", *blp.ExportTimeout)
99 }
100 opts = append(opts, sdklog.WithExportTimeout(time.Millisecond*time.Duration(*blp.ExportTimeout)))
101 }
102 if blp.MaxExportBatchSize != nil {
103 if *blp.MaxExportBatchSize < 0 {
104 return nil, fmt.Errorf("invalid batch size %d", *blp.MaxExportBatchSize)
105 }
106 opts = append(opts, sdklog.WithExportMaxBatchSize(*blp.MaxExportBatchSize))
107 }
108 if blp.MaxQueueSize != nil {
109 if *blp.MaxQueueSize < 0 {
110 return nil, fmt.Errorf("invalid queue size %d", *blp.MaxQueueSize)
111 }
112 opts = append(opts, sdklog.WithMaxQueueSize(*blp.MaxQueueSize))
113 }
114
115 if blp.ScheduleDelay != nil {
116 if *blp.ScheduleDelay < 0 {
117 return nil, fmt.Errorf("invalid schedule delay %d", *blp.ScheduleDelay)
118 }
119 opts = append(opts, sdklog.WithExportInterval(time.Millisecond*time.Duration(*blp.ScheduleDelay)))
120 }
121
122 return sdklog.NewBatchProcessor(exp, opts...), nil
123 }
124
125 func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter, error) {
126 var opts []otlploghttp.Option
127
128 if otlpConfig.Endpoint != nil {
129 u, err := url.ParseRequestURI(*otlpConfig.Endpoint)
130 if err != nil {
131 return nil, err
132 }
133 opts = append(opts, otlploghttp.WithEndpoint(u.Host))
134
135 if u.Scheme == "http" {
136 opts = append(opts, otlploghttp.WithInsecure())
137 } else {
138 tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
139 if err != nil {
140 return nil, err
141 }
142 opts = append(opts, otlploghttp.WithTLSClientConfig(tlsConfig))
143 }
144 if u.Path != "" {
145 opts = append(opts, otlploghttp.WithURLPath(u.Path))
146 }
147 }
148 if otlpConfig.Compression != nil {
149 switch *otlpConfig.Compression {
150 case compressionGzip:
151 opts = append(opts, otlploghttp.WithCompression(otlploghttp.GzipCompression))
152 case compressionNone:
153 opts = append(opts, otlploghttp.WithCompression(otlploghttp.NoCompression))
154 default:
155 return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression)
156 }
157 }
158 if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
159 opts = append(opts, otlploghttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
160 }
161 headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
162 if err != nil {
163 return nil, err
164 }
165 if len(headersConfig) > 0 {
166 opts = append(opts, otlploghttp.WithHeaders(headersConfig))
167 }
168
169 return otlploghttp.New(ctx, opts...)
170 }
171
172 func otlpGRPCLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter, error) {
173 var opts []otlploggrpc.Option
174
175 if otlpConfig.Endpoint != nil {
176 u, err := url.ParseRequestURI(*otlpConfig.Endpoint)
177 if err != nil {
178 return nil, err
179 }
180 // ParseRequestURI leaves the Host field empty when no
181 // scheme is specified (i.e. localhost:4317). This check is
182 // here to support the case where a user may not specify a
183 // scheme. The code does its best effort here by using
184 // otlpConfig.Endpoint as-is in that case
185 if u.Host != "" {
186 opts = append(opts, otlploggrpc.WithEndpoint(u.Host))
187 } else {
188 opts = append(opts, otlploggrpc.WithEndpoint(*otlpConfig.Endpoint))
189 }
190 if u.Scheme == "http" || (u.Scheme != "https" && otlpConfig.Insecure != nil && *otlpConfig.Insecure) {
191 opts = append(opts, otlploggrpc.WithInsecure())
192 }
193 }
194 if otlpConfig.Compression != nil {
195 switch *otlpConfig.Compression {
196 case compressionGzip:
197 opts = append(opts, otlploggrpc.WithCompressor(*otlpConfig.Compression))
198 case compressionNone:
199 // none requires no options
200 default:
201 return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression)
202 }
203 }
204 if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
205 opts = append(opts, otlploggrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
206 }
207 headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList)
208 if err != nil {
209 return nil, err
210 }
211 if len(headersConfig) > 0 {
212 opts = append(opts, otlploggrpc.WithHeaders(headersConfig))
213 }
214
215 if otlpConfig.Certificate != nil || otlpConfig.ClientCertificate != nil || otlpConfig.ClientKey != nil {
216 tlsConfig, err := tls.CreateConfig(otlpConfig.Certificate, otlpConfig.ClientCertificate, otlpConfig.ClientKey)
217 if err != nil {
218 return nil, err
219 }
220 opts = append(opts, otlploggrpc.WithTLSCredentials(credentials.NewTLS(tlsConfig)))
221 }
222
223 return otlploggrpc.New(ctx, opts...)
224 }