Debian Codegraph @

otelconf/v0.2.0/log_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelconf
5
6 import (
7 "errors"
8 "net/url"
9 "reflect"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
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 )
21
22 func TestLoggerProvider(t *testing.T) {
23 tests := []struct {
24 name string
25 cfg configOptions
26 wantProvider log.LoggerProvider
27 wantErr error
28 }{
29 {
30 name: "no-logger-provider-configured",
31 wantProvider: noop.NewLoggerProvider(),
32 },
33 {
34 name: "error-in-config",
35 cfg: configOptions{
36 opentelemetryConfig: OpenTelemetryConfiguration{
37 LoggerProvider: &LoggerProvider{
38 Processors: []LogRecordProcessor{
39 {
40 Simple: &SimpleLogRecordProcessor{},
41 Batch: &BatchLogRecordProcessor{},
42 },
43 },
44 },
45 },
46 },
47 wantProvider: noop.NewLoggerProvider(),
48 wantErr: errors.Join(errors.New("must not specify multiple log processor type")),
49 },
50 }
51 for _, tt := range tests {
52 mp, shutdown, err := loggerProvider(tt.cfg, resource.Default())
53 require.Equal(t, tt.wantProvider, mp)
54 assert.Equal(t, tt.wantErr, err)
55 require.NoError(t, shutdown(t.Context()))
56 }
57 }
58
59 func TestLogProcessor(t *testing.T) {
60 ctx := t.Context()
61
62 otlpHTTPExporter, err := otlploghttp.New(ctx)
63 require.NoError(t, err)
64
65 consoleExporter, err := stdoutlog.New(
66 stdoutlog.WithPrettyPrint(),
67 )
68 require.NoError(t, err)
69
70 testCases := []struct {
71 name string
72 processor LogRecordProcessor
73 args any
74 wantErr error
75 wantProcessor sdklog.Processor
76 }{
77 {
78 name: "no processor",
79 wantErr: errors.New("unsupported log processor type, must be one of simple or batch"),
80 },
81 {
82 name: "multiple processor types",
83 processor: LogRecordProcessor{
84 Batch: &BatchLogRecordProcessor{
85 Exporter: LogRecordExporter{},
86 },
87 Simple: &SimpleLogRecordProcessor{},
88 },
89 wantErr: errors.New("must not specify multiple log processor type"),
90 },
91 {
92 name: "batch processor invalid batch size otlphttp exporter",
93
94 processor: LogRecordProcessor{
95 Batch: &BatchLogRecordProcessor{
96 MaxExportBatchSize: ptr(-1),
97 Exporter: LogRecordExporter{
98 OTLP: &OTLP{
99 Protocol: "http/protobuf",
100 },
101 },
102 },
103 },
104 wantErr: errors.New("invalid batch size -1"),
105 },
106 {
107 name: "batch processor invalid export timeout otlphttp exporter",
108 processor: LogRecordProcessor{
109 Batch: &BatchLogRecordProcessor{
110 ExportTimeout: ptr(-2),
111 Exporter: LogRecordExporter{
112 OTLP: &OTLP{
113 Protocol: "http/protobuf",
114 },
115 },
116 },
117 },
118 wantErr: errors.New("invalid export timeout -2"),
119 },
120 {
121 name: "batch processor invalid queue size otlphttp exporter",
122
123 processor: LogRecordProcessor{
124 Batch: &BatchLogRecordProcessor{
125 MaxQueueSize: ptr(-3),
126 Exporter: LogRecordExporter{
127 OTLP: &OTLP{
128 Protocol: "http/protobuf",
129 },
130 },
131 },
132 },
133 wantErr: errors.New("invalid queue size -3"),
134 },
135 {
136 name: "batch processor invalid schedule delay console exporter",
137 processor: LogRecordProcessor{
138 Batch: &BatchLogRecordProcessor{
139 ScheduleDelay: ptr(-4),
140 Exporter: LogRecordExporter{
141 OTLP: &OTLP{
142 Protocol: "http/protobuf",
143 },
144 },
145 },
146 },
147 wantErr: errors.New("invalid schedule delay -4"),
148 },
149 {
150 name: "batch processor invalid exporter",
151 processor: LogRecordProcessor{
152 Batch: &BatchLogRecordProcessor{
153 Exporter: LogRecordExporter{},
154 },
155 },
156 wantErr: errors.New("no valid log exporter"),
157 },
158 {
159 name: "batch/console",
160 processor: LogRecordProcessor{
161 Batch: &BatchLogRecordProcessor{
162 MaxExportBatchSize: ptr(0),
163 ExportTimeout: ptr(0),
164 MaxQueueSize: ptr(0),
165 ScheduleDelay: ptr(0),
166 Exporter: LogRecordExporter{
167 Console: map[string]any{},
168 },
169 },
170 },
171 wantProcessor: sdklog.NewBatchProcessor(consoleExporter),
172 },
173 {
174 name: "batch/otlp-http-exporter",
175 processor: LogRecordProcessor{
176 Batch: &BatchLogRecordProcessor{
177 MaxExportBatchSize: ptr(0),
178 ExportTimeout: ptr(0),
179 MaxQueueSize: ptr(0),
180 ScheduleDelay: ptr(0),
181 Exporter: LogRecordExporter{
182 OTLP: &OTLP{
183 Protocol: "http/protobuf",
184 Endpoint: "http://localhost:4318",
185 Compression: ptr("gzip"),
186 Timeout: ptr(1000),
187 Headers: map[string]string{
188 "test": "test1",
189 },
190 },
191 },
192 },
193 },
194 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
195 },
196 {
197 name: "batch/otlp-http-exporter-with-path",
198 processor: LogRecordProcessor{
199 Batch: &BatchLogRecordProcessor{
200 MaxExportBatchSize: ptr(0),
201 ExportTimeout: ptr(0),
202 MaxQueueSize: ptr(0),
203 ScheduleDelay: ptr(0),
204 Exporter: LogRecordExporter{
205 OTLP: &OTLP{
206 Protocol: "http/protobuf",
207 Endpoint: "http://localhost:4318/path/123",
208 Compression: ptr("none"),
209 Timeout: ptr(1000),
210 Headers: map[string]string{
211 "test": "test1",
212 },
213 },
214 },
215 },
216 },
217 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
218 },
219 {
220 name: "batch/otlp-http-exporter-no-endpoint",
221 processor: LogRecordProcessor{
222 Batch: &BatchLogRecordProcessor{
223 MaxExportBatchSize: ptr(0),
224 ExportTimeout: ptr(0),
225 MaxQueueSize: ptr(0),
226 ScheduleDelay: ptr(0),
227 Exporter: LogRecordExporter{
228 OTLP: &OTLP{
229 Protocol: "http/protobuf",
230 Compression: ptr("gzip"),
231 Timeout: ptr(1000),
232 Headers: map[string]string{
233 "test": "test1",
234 },
235 },
236 },
237 },
238 },
239 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
240 },
241 {
242 name: "batch/otlp-http-exporter-no-scheme",
243 processor: LogRecordProcessor{
244 Batch: &BatchLogRecordProcessor{
245 MaxExportBatchSize: ptr(0),
246 ExportTimeout: ptr(0),
247 MaxQueueSize: ptr(0),
248 ScheduleDelay: ptr(0),
249 Exporter: LogRecordExporter{
250 OTLP: &OTLP{
251 Protocol: "http/protobuf",
252 Endpoint: "localhost:4318",
253 Compression: ptr("gzip"),
254 Timeout: ptr(1000),
255 Headers: map[string]string{
256 "test": "test1",
257 },
258 },
259 },
260 },
261 },
262 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
263 },
264 {
265 name: "batch/otlp-http-invalid-protocol",
266 processor: LogRecordProcessor{
267 Batch: &BatchLogRecordProcessor{
268 MaxExportBatchSize: ptr(0),
269 ExportTimeout: ptr(0),
270 MaxQueueSize: ptr(0),
271 ScheduleDelay: ptr(0),
272 Exporter: LogRecordExporter{
273 OTLP: &OTLP{
274 Protocol: "invalid",
275 Endpoint: "https://10.0.0.0:443",
276 Compression: ptr("gzip"),
277 Timeout: ptr(1000),
278 Headers: map[string]string{
279 "test": "test1",
280 },
281 },
282 },
283 },
284 },
285 wantErr: errors.New("unsupported protocol \"invalid\""),
286 },
287 {
288 name: "batch/otlp-http-invalid-endpoint",
289 processor: LogRecordProcessor{
290 Batch: &BatchLogRecordProcessor{
291 MaxExportBatchSize: ptr(0),
292 ExportTimeout: ptr(0),
293 MaxQueueSize: ptr(0),
294 ScheduleDelay: ptr(0),
295 Exporter: LogRecordExporter{
296 OTLP: &OTLP{
297 Protocol: "http/protobuf",
298 Endpoint: " ",
299 Compression: ptr("gzip"),
300 Timeout: ptr(1000),
301 Headers: map[string]string{
302 "test": "test1",
303 },
304 },
305 },
306 },
307 },
308 wantErr: &url.Error{Op: "parse", URL: " ", Err: errors.New("invalid URI for request")},
309 },
310 {
311 name: "batch/otlp-http-none-compression",
312 processor: LogRecordProcessor{
313 Batch: &BatchLogRecordProcessor{
314 MaxExportBatchSize: ptr(0),
315 ExportTimeout: ptr(0),
316 MaxQueueSize: ptr(0),
317 ScheduleDelay: ptr(0),
318 Exporter: LogRecordExporter{
319 OTLP: &OTLP{
320 Protocol: "http/protobuf",
321 Endpoint: "localhost:4318",
322 Compression: ptr("none"),
323 Timeout: ptr(1000),
324 Headers: map[string]string{
325 "test": "test1",
326 },
327 },
328 },
329 },
330 },
331 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
332 },
333 {
334 name: "batch/otlp-http-invalid-compression",
335 processor: LogRecordProcessor{
336 Batch: &BatchLogRecordProcessor{
337 MaxExportBatchSize: ptr(0),
338 ExportTimeout: ptr(0),
339 MaxQueueSize: ptr(0),
340 ScheduleDelay: ptr(0),
341 Exporter: LogRecordExporter{
342 OTLP: &OTLP{
343 Protocol: "http/protobuf",
344 Endpoint: "localhost:4318",
345 Compression: ptr("invalid"),
346 Timeout: ptr(1000),
347 Headers: map[string]string{
348 "test": "test1",
349 },
350 },
351 },
352 },
353 },
354 wantErr: errors.New("unsupported compression \"invalid\""),
355 },
356 {
357 name: "simple/no-exporter",
358 processor: LogRecordProcessor{
359 Simple: &SimpleLogRecordProcessor{
360 Exporter: LogRecordExporter{},
361 },
362 },
363 wantErr: errors.New("no valid log exporter"),
364 },
365 {
366 name: "simple/console",
367 processor: LogRecordProcessor{
368 Simple: &SimpleLogRecordProcessor{
369 Exporter: LogRecordExporter{
370 Console: map[string]any{},
371 },
372 },
373 },
374 wantProcessor: sdklog.NewSimpleProcessor(consoleExporter),
375 },
376 {
377 name: "simple/otlp-exporter",
378 processor: LogRecordProcessor{
379 Simple: &SimpleLogRecordProcessor{
380 Exporter: LogRecordExporter{
381 OTLP: &OTLP{
382 Protocol: "http/protobuf",
383 Endpoint: "localhost:4318",
384 Compression: ptr("gzip"),
385 Timeout: ptr(1000),
386 Headers: map[string]string{
387 "test": "test1",
388 },
389 },
390 },
391 },
392 },
393 wantProcessor: sdklog.NewSimpleProcessor(otlpHTTPExporter),
394 },
395 }
396 for _, tt := range testCases {
397 t.Run(tt.name, func(t *testing.T) {
398 got, err := logProcessor(t.Context(), tt.processor)
399 require.Equal(t, tt.wantErr, err)
400 if tt.wantProcessor == nil {
401 require.Nil(t, got)
402 } else {
403 require.Equal(t, reflect.TypeOf(tt.wantProcessor), reflect.TypeOf(got))
404 wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantProcessor)).FieldByName("exporter").Elem().Type()
405 gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("exporter").Elem().Type()
406 require.Equal(t, wantExporterType.String(), gotExporterType.String())
407 }
408 })
409 }
410 }