Debian Codegraph @

otelconf/v0.3.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 "bytes"
8 "context"
9 "crypto/tls"
10 "crypto/x509"
11 "errors"
12 "net"
13 "net/http"
14 "net/http/httptest"
15 "os"
16 "path/filepath"
17 "reflect"
18 "runtime"
19 "testing"
20 "time"
21
22 "github.com/stretchr/testify/assert"
23 "github.com/stretchr/testify/require"
24 "go.opentelemetry.io/otel/attribute"
25 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
26 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
27 "go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
28 "go.opentelemetry.io/otel/log"
29 "go.opentelemetry.io/otel/log/noop"
30 sdklog "go.opentelemetry.io/otel/sdk/log"
31 sdklogtest "go.opentelemetry.io/otel/sdk/log/logtest"
32 "go.opentelemetry.io/otel/sdk/resource"
33 collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1"
34 "google.golang.org/grpc"
35 "google.golang.org/grpc/credentials"
36 )
37
38 func TestLoggerProvider(t *testing.T) {
39 tests := []struct {
40 name string
41 cfg configOptions
42 wantProvider log.LoggerProvider
43 wantErr error
44 }{
45 {
46 name: "no-logger-provider-configured",
47 wantProvider: noop.NewLoggerProvider(),
48 },
49 {
50 name: "error-in-config",
51 cfg: configOptions{
52 opentelemetryConfig: OpenTelemetryConfiguration{
53 LoggerProvider: &LoggerProvider{
54 Processors: []LogRecordProcessor{
55 {
56 Simple: &SimpleLogRecordProcessor{},
57 Batch: &BatchLogRecordProcessor{},
58 },
59 },
60 },
61 },
62 },
63 wantProvider: noop.NewLoggerProvider(),
64 wantErr: errors.Join(errors.New("must not specify multiple log processor type")),
65 },
66 }
67 for _, tt := range tests {
68 mp, shutdown, err := loggerProvider(tt.cfg, resource.Default())
69 require.Equal(t, tt.wantProvider, mp)
70 assert.Equal(t, tt.wantErr, err)
71 require.NoError(t, shutdown(t.Context()))
72 }
73 }
74
75 func TestLogProcessor(t *testing.T) {
76 ctx := t.Context()
77
78 otlpHTTPExporter, err := otlploghttp.New(ctx)
79 require.NoError(t, err)
80
81 otlpGRPCExporter, err := otlploggrpc.New(ctx)
82 require.NoError(t, err)
83
84 consoleExporter, err := stdoutlog.New(
85 stdoutlog.WithPrettyPrint(),
86 )
87 require.NoError(t, err)
88
89 testCases := []struct {
90 name string
91 processor LogRecordProcessor
92 args any
93 wantErr string
94 wantProcessor sdklog.Processor
95 }{
96 {
97 name: "no processor",
98 wantErr: "unsupported log processor type, must be one of simple or batch",
99 },
100 {
101 name: "multiple processor types",
102 processor: LogRecordProcessor{
103 Batch: &BatchLogRecordProcessor{
104 Exporter: LogRecordExporter{},
105 },
106 Simple: &SimpleLogRecordProcessor{},
107 },
108 wantErr: "must not specify multiple log processor type",
109 },
110 {
111 name: "batch processor invalid batch size otlphttp exporter",
112
113 processor: LogRecordProcessor{
114 Batch: &BatchLogRecordProcessor{
115 MaxExportBatchSize: ptr(-1),
116 Exporter: LogRecordExporter{
117 OTLP: &OTLP{
118 Protocol: ptr("http/protobuf"),
119 },
120 },
121 },
122 },
123 wantErr: "invalid batch size -1",
124 },
125 {
126 name: "batch processor invalid export timeout otlphttp exporter",
127 processor: LogRecordProcessor{
128 Batch: &BatchLogRecordProcessor{
129 ExportTimeout: ptr(-2),
130 Exporter: LogRecordExporter{
131 OTLP: &OTLP{
132 Protocol: ptr("http/protobuf"),
133 },
134 },
135 },
136 },
137 wantErr: "invalid export timeout -2",
138 },
139 {
140 name: "batch processor invalid queue size otlphttp exporter",
141
142 processor: LogRecordProcessor{
143 Batch: &BatchLogRecordProcessor{
144 MaxQueueSize: ptr(-3),
145 Exporter: LogRecordExporter{
146 OTLP: &OTLP{
147 Protocol: ptr("http/protobuf"),
148 },
149 },
150 },
151 },
152 wantErr: "invalid queue size -3",
153 },
154 {
155 name: "batch processor invalid schedule delay console exporter",
156 processor: LogRecordProcessor{
157 Batch: &BatchLogRecordProcessor{
158 ScheduleDelay: ptr(-4),
159 Exporter: LogRecordExporter{
160 OTLP: &OTLP{
161 Protocol: ptr("http/protobuf"),
162 },
163 },
164 },
165 },
166 wantErr: "invalid schedule delay -4",
167 },
168 {
169 name: "batch processor invalid exporter",
170 processor: LogRecordProcessor{
171 Batch: &BatchLogRecordProcessor{
172 Exporter: LogRecordExporter{},
173 },
174 },
175 wantErr: "no valid log exporter",
176 },
177 {
178 name: "batch/console",
179 processor: LogRecordProcessor{
180 Batch: &BatchLogRecordProcessor{
181 MaxExportBatchSize: ptr(0),
182 ExportTimeout: ptr(0),
183 MaxQueueSize: ptr(0),
184 ScheduleDelay: ptr(0),
185 Exporter: LogRecordExporter{
186 Console: map[string]any{},
187 },
188 },
189 },
190 wantProcessor: sdklog.NewBatchProcessor(consoleExporter),
191 },
192 {
193 name: "batch/otlp-grpc-exporter-no-endpoint",
194 processor: LogRecordProcessor{
195 Batch: &BatchLogRecordProcessor{
196 MaxExportBatchSize: ptr(0),
197 ExportTimeout: ptr(0),
198 MaxQueueSize: ptr(0),
199 ScheduleDelay: ptr(0),
200 Exporter: LogRecordExporter{
201 OTLP: &OTLP{
202 Protocol: ptr("grpc"),
203 Compression: ptr("gzip"),
204 Timeout: ptr(1000),
205 Headers: []NameStringValuePair{
206 {Name: "test", Value: ptr("test1")},
207 },
208 },
209 },
210 },
211 },
212 wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
213 },
214 {
215 name: "batch/otlp-grpc-exporter",
216 processor: LogRecordProcessor{
217 Batch: &BatchLogRecordProcessor{
218 MaxExportBatchSize: ptr(0),
219 ExportTimeout: ptr(0),
220 MaxQueueSize: ptr(0),
221 ScheduleDelay: ptr(0),
222 Exporter: LogRecordExporter{
223 OTLP: &OTLP{
224 Protocol: ptr("grpc"),
225 Endpoint: ptr("http://localhost:4317"),
226 Compression: ptr("gzip"),
227 Timeout: ptr(1000),
228 Headers: []NameStringValuePair{
229 {Name: "test", Value: ptr("test1")},
230 },
231 },
232 },
233 },
234 },
235 wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
236 },
237 {
238 name: "batch/otlp-grpc-exporter-socket-endpoint",
239 processor: LogRecordProcessor{
240 Batch: &BatchLogRecordProcessor{
241 MaxExportBatchSize: ptr(0),
242 ExportTimeout: ptr(0),
243 MaxQueueSize: ptr(0),
244 ScheduleDelay: ptr(0),
245 Exporter: LogRecordExporter{
246 OTLP: &OTLP{
247 Protocol: ptr("grpc"),
248 Endpoint: ptr("unix:collector.sock"),
249 Compression: ptr("gzip"),
250 Timeout: ptr(1000),
251 Headers: []NameStringValuePair{
252 {Name: "test", Value: ptr("test1")},
253 },
254 },
255 },
256 },
257 },
258 wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
259 },
260 {
261 name: "batch/otlp-grpc-good-ca-certificate",
262 processor: LogRecordProcessor{
263 Batch: &BatchLogRecordProcessor{
264 Exporter: LogRecordExporter{
265 OTLP: &OTLP{
266 Protocol: ptr("grpc"),
267 Endpoint: ptr("localhost:4317"),
268 Compression: ptr("gzip"),
269 Timeout: ptr(1000),
270 Certificate: ptr(filepath.Join("..", "testdata", "ca.crt")),
271 },
272 },
273 },
274 },
275 wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
276 },
277 {
278 name: "batch/otlp-grpc-bad-ca-certificate",
279 processor: LogRecordProcessor{
280 Batch: &BatchLogRecordProcessor{
281 Exporter: LogRecordExporter{
282 OTLP: &OTLP{
283 Protocol: ptr("grpc"),
284 Endpoint: ptr("localhost:4317"),
285 Compression: ptr("gzip"),
286 Timeout: ptr(1000),
287 Certificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
288 },
289 },
290 },
291 },
292 wantErr: "could not create certificate authority chain from certificate",
293 },
294 {
295 name: "batch/otlp-grpc-bad-headerslist",
296 processor: LogRecordProcessor{
297 Batch: &BatchLogRecordProcessor{
298 Exporter: LogRecordExporter{
299 OTLP: &OTLP{
300 Protocol: ptr("grpc"),
301 Endpoint: ptr("localhost:4317"),
302 Compression: ptr("gzip"),
303 Timeout: ptr(1000),
304 HeadersList: ptr("==="),
305 },
306 },
307 },
308 },
309 wantErr: "invalid headers list: invalid key: \"\"",
310 },
311 {
312 name: "batch/otlp-grpc-bad-client-certificate",
313 processor: LogRecordProcessor{
314 Batch: &BatchLogRecordProcessor{
315 Exporter: LogRecordExporter{
316 OTLP: &OTLP{
317 Protocol: ptr("grpc"),
318 Endpoint: ptr("localhost:4317"),
319 Compression: ptr("gzip"),
320 Timeout: ptr(1000),
321 ClientCertificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
322 ClientKey: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
323 },
324 },
325 },
326 },
327 wantErr: "could not use client certificate: tls: failed to find any PEM data in certificate input",
328 },
329 {
330 name: "batch/otlp-grpc-exporter-no-scheme",
331 processor: LogRecordProcessor{
332 Batch: &BatchLogRecordProcessor{
333 MaxExportBatchSize: ptr(0),
334 ExportTimeout: ptr(0),
335 MaxQueueSize: ptr(0),
336 ScheduleDelay: ptr(0),
337 Exporter: LogRecordExporter{
338 OTLP: &OTLP{
339 Protocol: ptr("grpc"),
340 Endpoint: ptr("localhost:4317"),
341 Compression: ptr("gzip"),
342 Timeout: ptr(1000),
343 Headers: []NameStringValuePair{
344 {Name: "test", Value: ptr("test1")},
345 },
346 },
347 },
348 },
349 },
350 wantProcessor: sdklog.NewBatchProcessor(otlpGRPCExporter),
351 },
352 {
353 name: "batch/otlp-grpc-invalid-endpoint",
354 processor: LogRecordProcessor{
355 Batch: &BatchLogRecordProcessor{
356 MaxExportBatchSize: ptr(0),
357 ExportTimeout: ptr(0),
358 MaxQueueSize: ptr(0),
359 ScheduleDelay: ptr(0),
360 Exporter: LogRecordExporter{
361 OTLP: &OTLP{
362 Protocol: ptr("grpc"),
363 Endpoint: ptr(" "),
364 Compression: ptr("gzip"),
365 Timeout: ptr(1000),
366 Headers: []NameStringValuePair{
367 {Name: "test", Value: ptr("test1")},
368 },
369 },
370 },
371 },
372 },
373 wantErr: "parse \" \": invalid URI for request",
374 },
375 {
376 name: "batch/otlp-grpc-invalid-compression",
377 processor: LogRecordProcessor{
378 Batch: &BatchLogRecordProcessor{
379 MaxExportBatchSize: ptr(0),
380 ExportTimeout: ptr(0),
381 MaxQueueSize: ptr(0),
382 ScheduleDelay: ptr(0),
383 Exporter: LogRecordExporter{
384 OTLP: &OTLP{
385 Protocol: ptr("grpc"),
386 Endpoint: ptr("localhost:4317"),
387 Compression: ptr("invalid"),
388 Timeout: ptr(1000),
389 Headers: []NameStringValuePair{
390 {Name: "test", Value: ptr("test1")},
391 },
392 },
393 },
394 },
395 },
396 wantErr: "unsupported compression \"invalid\"",
397 },
398 {
399 name: "batch/otlp-http-exporter",
400 processor: LogRecordProcessor{
401 Batch: &BatchLogRecordProcessor{
402 MaxExportBatchSize: ptr(0),
403 ExportTimeout: ptr(0),
404 MaxQueueSize: ptr(0),
405 ScheduleDelay: ptr(0),
406 Exporter: LogRecordExporter{
407 OTLP: &OTLP{
408 Protocol: ptr("http/protobuf"),
409 Endpoint: ptr("http://localhost:4318"),
410 Compression: ptr("gzip"),
411 Timeout: ptr(1000),
412 Headers: []NameStringValuePair{
413 {Name: "test", Value: ptr("test1")},
414 },
415 },
416 },
417 },
418 },
419 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
420 },
421 {
422 name: "batch/otlp-http-good-ca-certificate",
423 processor: LogRecordProcessor{
424 Batch: &BatchLogRecordProcessor{
425 Exporter: LogRecordExporter{
426 OTLP: &OTLP{
427 Protocol: ptr("http/protobuf"),
428 Endpoint: ptr("localhost:4317"),
429 Compression: ptr("gzip"),
430 Timeout: ptr(1000),
431 Certificate: ptr(filepath.Join("..", "testdata", "ca.crt")),
432 },
433 },
434 },
435 },
436 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
437 },
438 {
439 name: "batch/otlp-http-bad-ca-certificate",
440 processor: LogRecordProcessor{
441 Batch: &BatchLogRecordProcessor{
442 Exporter: LogRecordExporter{
443 OTLP: &OTLP{
444 Protocol: ptr("http/protobuf"),
445 Endpoint: ptr("localhost:4317"),
446 Compression: ptr("gzip"),
447 Timeout: ptr(1000),
448 Certificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
449 },
450 },
451 },
452 },
453 wantErr: "could not create certificate authority chain from certificate",
454 },
455 {
456 name: "batch/otlp-http-bad-client-certificate",
457 processor: LogRecordProcessor{
458 Batch: &BatchLogRecordProcessor{
459 Exporter: LogRecordExporter{
460 OTLP: &OTLP{
461 Protocol: ptr("http/protobuf"),
462 Endpoint: ptr("localhost:4317"),
463 Compression: ptr("gzip"),
464 Timeout: ptr(1000),
465 ClientCertificate: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
466 ClientKey: ptr(filepath.Join("..", "testdata", "bad_cert.crt")),
467 },
468 },
469 },
470 },
471 wantErr: "could not use client certificate: tls: failed to find any PEM data in certificate input",
472 },
473 {
474 name: "batch/otlp-http-bad-headerslist",
475 processor: LogRecordProcessor{
476 Batch: &BatchLogRecordProcessor{
477 Exporter: LogRecordExporter{
478 OTLP: &OTLP{
479 Protocol: ptr("http/protobuf"),
480 Endpoint: ptr("localhost:4317"),
481 Compression: ptr("gzip"),
482 Timeout: ptr(1000),
483 HeadersList: ptr("==="),
484 },
485 },
486 },
487 },
488 wantErr: "invalid headers list: invalid key: \"\"",
489 },
490 {
491 name: "batch/otlp-http-exporter-with-path",
492 processor: LogRecordProcessor{
493 Batch: &BatchLogRecordProcessor{
494 MaxExportBatchSize: ptr(0),
495 ExportTimeout: ptr(0),
496 MaxQueueSize: ptr(0),
497 ScheduleDelay: ptr(0),
498 Exporter: LogRecordExporter{
499 OTLP: &OTLP{
500 Protocol: ptr("http/protobuf"),
501 Endpoint: ptr("http://localhost:4318/path/123"),
502 Compression: ptr("none"),
503 Timeout: ptr(1000),
504 Headers: []NameStringValuePair{
505 {Name: "test", Value: ptr("test1")},
506 },
507 },
508 },
509 },
510 },
511 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
512 },
513 {
514 name: "batch/otlp-http-exporter-no-endpoint",
515 processor: LogRecordProcessor{
516 Batch: &BatchLogRecordProcessor{
517 MaxExportBatchSize: ptr(0),
518 ExportTimeout: ptr(0),
519 MaxQueueSize: ptr(0),
520 ScheduleDelay: ptr(0),
521 Exporter: LogRecordExporter{
522 OTLP: &OTLP{
523 Protocol: ptr("http/protobuf"),
524 Compression: ptr("gzip"),
525 Timeout: ptr(1000),
526 Headers: []NameStringValuePair{
527 {Name: "test", Value: ptr("test1")},
528 },
529 },
530 },
531 },
532 },
533 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
534 },
535 {
536 name: "batch/otlp-http-exporter-no-scheme",
537 processor: LogRecordProcessor{
538 Batch: &BatchLogRecordProcessor{
539 MaxExportBatchSize: ptr(0),
540 ExportTimeout: ptr(0),
541 MaxQueueSize: ptr(0),
542 ScheduleDelay: ptr(0),
543 Exporter: LogRecordExporter{
544 OTLP: &OTLP{
545 Protocol: ptr("http/protobuf"),
546 Endpoint: ptr("localhost:4318"),
547 Compression: ptr("gzip"),
548 Timeout: ptr(1000),
549 Headers: []NameStringValuePair{
550 {Name: "test", Value: ptr("test1")},
551 },
552 },
553 },
554 },
555 },
556 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
557 },
558 {
559 name: "batch/otlp-http-invalid-protocol",
560 processor: LogRecordProcessor{
561 Batch: &BatchLogRecordProcessor{
562 MaxExportBatchSize: ptr(0),
563 ExportTimeout: ptr(0),
564 MaxQueueSize: ptr(0),
565 ScheduleDelay: ptr(0),
566 Exporter: LogRecordExporter{
567 OTLP: &OTLP{
568 Protocol: ptr("invalid"),
569 Endpoint: ptr("https://10.0.0.0:443"),
570 Compression: ptr("gzip"),
571 Timeout: ptr(1000),
572 Headers: []NameStringValuePair{
573 {Name: "test", Value: ptr("test1")},
574 },
575 },
576 },
577 },
578 },
579 wantErr: "unsupported protocol \"invalid\"",
580 },
581 {
582 name: "batch/otlp-http-invalid-endpoint",
583 processor: LogRecordProcessor{
584 Batch: &BatchLogRecordProcessor{
585 MaxExportBatchSize: ptr(0),
586 ExportTimeout: ptr(0),
587 MaxQueueSize: ptr(0),
588 ScheduleDelay: ptr(0),
589 Exporter: LogRecordExporter{
590 OTLP: &OTLP{
591 Protocol: ptr("http/protobuf"),
592 Endpoint: ptr(" "),
593 Compression: ptr("gzip"),
594 Timeout: ptr(1000),
595 Headers: []NameStringValuePair{
596 {Name: "test", Value: ptr("test1")},
597 },
598 },
599 },
600 },
601 },
602 wantErr: "parse \" \": invalid URI for request",
603 },
604 {
605 name: "batch/otlp-http-none-compression",
606 processor: LogRecordProcessor{
607 Batch: &BatchLogRecordProcessor{
608 MaxExportBatchSize: ptr(0),
609 ExportTimeout: ptr(0),
610 MaxQueueSize: ptr(0),
611 ScheduleDelay: ptr(0),
612 Exporter: LogRecordExporter{
613 OTLP: &OTLP{
614 Protocol: ptr("http/protobuf"),
615 Endpoint: ptr("localhost:4318"),
616 Compression: ptr("none"),
617 Timeout: ptr(1000),
618 Headers: []NameStringValuePair{
619 {Name: "test", Value: ptr("test1")},
620 },
621 },
622 },
623 },
624 },
625 wantProcessor: sdklog.NewBatchProcessor(otlpHTTPExporter),
626 },
627 {
628 name: "batch/otlp-http-invalid-compression",
629 processor: LogRecordProcessor{
630 Batch: &BatchLogRecordProcessor{
631 MaxExportBatchSize: ptr(0),
632 ExportTimeout: ptr(0),
633 MaxQueueSize: ptr(0),
634 ScheduleDelay: ptr(0),
635 Exporter: LogRecordExporter{
636 OTLP: &OTLP{
637 Protocol: ptr("http/protobuf"),
638 Endpoint: ptr("localhost:4318"),
639 Compression: ptr("invalid"),
640 Timeout: ptr(1000),
641 Headers: []NameStringValuePair{
642 {Name: "test", Value: ptr("test1")},
643 },
644 },
645 },
646 },
647 },
648 wantErr: "unsupported compression \"invalid\"",
649 },
650 {
651 name: "simple/no-exporter",
652 processor: LogRecordProcessor{
653 Simple: &SimpleLogRecordProcessor{
654 Exporter: LogRecordExporter{},
655 },
656 },
657 wantErr: "no valid log exporter",
658 },
659 {
660 name: "simple/console",
661 processor: LogRecordProcessor{
662 Simple: &SimpleLogRecordProcessor{
663 Exporter: LogRecordExporter{
664 Console: map[string]any{},
665 },
666 },
667 },
668 wantProcessor: sdklog.NewSimpleProcessor(consoleExporter),
669 },
670 {
671 name: "simple/otlp-exporter",
672 processor: LogRecordProcessor{
673 Simple: &SimpleLogRecordProcessor{
674 Exporter: LogRecordExporter{
675 OTLP: &OTLP{
676 Protocol: ptr("http/protobuf"),
677 Endpoint: ptr("localhost:4318"),
678 Compression: ptr("gzip"),
679 Timeout: ptr(1000),
680 Headers: []NameStringValuePair{
681 {Name: "test", Value: ptr("test1")},
682 },
683 },
684 },
685 },
686 },
687 wantProcessor: sdklog.NewSimpleProcessor(otlpHTTPExporter),
688 },
689 }
690 for _, tt := range testCases {
691 t.Run(tt.name, func(t *testing.T) {
692 got, err := logProcessor(t.Context(), tt.processor)
693 if tt.wantErr != "" {
694 require.Error(t, err)
695 require.Equal(t, tt.wantErr, err.Error())
696 } else {
697 require.NoError(t, err)
698 }
699 if tt.wantProcessor == nil {
700 require.Nil(t, got)
701 } else {
702 require.Equal(t, reflect.TypeOf(tt.wantProcessor), reflect.TypeOf(got))
703 wantExporterType := reflect.Indirect(reflect.ValueOf(tt.wantProcessor)).FieldByName("exporter").Elem().Type()
704 gotExporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("exporter").Elem().Type()
705 require.Equal(t, wantExporterType.String(), gotExporterType.String())
706 }
707 })
708 }
709 }
710
711 func TestLoggerProviderOptions(t *testing.T) {
712 var calls int
713 srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
714 calls++
715 }))
716 defer srv.Close()
717
718 cfg := OpenTelemetryConfiguration{
719 LoggerProvider: &LoggerProvider{
720 Processors: []LogRecordProcessor{{
721 Simple: &SimpleLogRecordProcessor{
722 Exporter: LogRecordExporter{
723 OTLP: &OTLP{
724 Protocol: ptr("http/protobuf"),
725 Endpoint: ptr(srv.URL),
726 Insecure: ptr(true),
727 },
728 },
729 },
730 }},
731 },
732 }
733
734 var buf bytes.Buffer
735 stdoutlogExporter, err := stdoutlog.New(stdoutlog.WithWriter(&buf))
736 require.NoError(t, err)
737
738 res := resource.NewSchemaless(attribute.String("foo", "bar"))
739 sdk, err := NewSDK(
740 WithOpenTelemetryConfiguration(cfg),
741 WithLoggerProviderOptions(sdklog.WithProcessor(sdklog.NewSimpleProcessor(stdoutlogExporter))),
742 WithLoggerProviderOptions(sdklog.WithResource(res)),
743 )
744 require.NoError(t, err)
745 defer func() {
746 assert.NoError(t, sdk.Shutdown(t.Context()))
747 }()
748
749 // The exporter, which we passed in as an extra option to NewSDK,
750 // should be wired up to the provider in addition to the
751 // configuration-based OTLP exporter.
752 logger := sdk.LoggerProvider().Logger("test")
753 logger.Emit(t.Context(), log.Record{})
754 assert.NotZero(t, buf)
755 assert.Equal(t, 1, calls)
756 // Options provided by WithMeterProviderOptions may be overridden
757 // by configuration, e.g. the resource is always defined via
758 // configuration.
759 assert.NotContains(t, buf.String(), "foo")
760 }
761
762 func Test_otlpGRPCLogExporter(t *testing.T) {
763 if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
764 // TODO (#8115): Fix the flakiness on Windows and MacOS.
765 t.Skip("Test is flaky on Windows and MacOS.")
766 }
767 type args struct {
768 ctx context.Context
769 otlpConfig *OTLP
770 }
771 tests := []struct {
772 name string
773 args args
774 grpcServerOpts func() ([]grpc.ServerOption, error)
775 }{
776 {
777 name: "no TLS config",
778 args: args{
779 ctx: t.Context(),
780 otlpConfig: &OTLP{
781 Protocol: ptr("grpc"),
782 Compression: ptr("gzip"),
783 Timeout: ptr(5000),
784 Insecure: ptr(true),
785 Headers: []NameStringValuePair{
786 {Name: "test", Value: ptr("test1")},
787 },
788 },
789 },
790 grpcServerOpts: func() ([]grpc.ServerOption, error) {
791 return []grpc.ServerOption{}, nil
792 },
793 },
794 {
795 name: "with TLS config",
796 args: args{
797 ctx: t.Context(),
798 otlpConfig: &OTLP{
799 Protocol: ptr("grpc"),
800 Compression: ptr("gzip"),
801 Timeout: ptr(5000),
802 Certificate: ptr("testdata/server-certs/server.crt"),
803 Headers: []NameStringValuePair{
804 {Name: "test", Value: ptr("test1")},
805 },
806 },
807 },
808 grpcServerOpts: func() ([]grpc.ServerOption, error) {
809 opts := []grpc.ServerOption{}
810 tlsCreds, err := credentials.NewServerTLSFromFile("testdata/server-certs/server.crt", "testdata/server-certs/server.key")
811 if err != nil {
812 return nil, err
813 }
814 opts = append(opts, grpc.Creds(tlsCreds))
815 return opts, nil
816 },
817 },
818 {
819 name: "with TLS config and client key",
820 args: args{
821 ctx: t.Context(),
822 otlpConfig: &OTLP{
823 Protocol: ptr("grpc"),
824 Compression: ptr("gzip"),
825 Timeout: ptr(5000),
826 Certificate: ptr("testdata/server-certs/server.crt"),
827 ClientKey: ptr("testdata/client-certs/client.key"),
828 ClientCertificate: ptr("testdata/client-certs/client.crt"),
829 Headers: []NameStringValuePair{
830 {Name: "test", Value: ptr("test1")},
831 },
832 },
833 },
834 grpcServerOpts: func() ([]grpc.ServerOption, error) {
835 opts := []grpc.ServerOption{}
836 cert, err := tls.LoadX509KeyPair("testdata/server-certs/server.crt", "testdata/server-certs/server.key")
837 if err != nil {
838 return nil, err
839 }
840 caCert, err := os.ReadFile("testdata/ca.crt")
841 if err != nil {
842 return nil, err
843 }
844 caCertPool := x509.NewCertPool()
845 caCertPool.AppendCertsFromPEM(caCert)
846 tlsCreds := credentials.NewTLS(&tls.Config{
847 Certificates: []tls.Certificate{cert},
848 ClientCAs: caCertPool,
849 ClientAuth: tls.RequireAndVerifyClientCert,
850 })
851 opts = append(opts, grpc.Creds(tlsCreds))
852 return opts, nil
853 },
854 },
855 }
856 for _, tt := range tests {
857 t.Run(tt.name, func(t *testing.T) {
858 n, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp4", "localhost:0")
859 require.NoError(t, err)
860
861 // We need to manually construct the endpoint using the port on which the server is listening.
862 //
863 // n.Addr() always returns 127.0.0.1 instead of localhost.
864 // But our certificate is created with CN as 'localhost', not '127.0.0.1'.
865 // So we have to manually form the endpoint as "localhost:<port>".
866 _, port, err := net.SplitHostPort(n.Addr().String())
867 require.NoError(t, err)
868 tt.args.otlpConfig.Endpoint = ptr("localhost:" + port)
869
870 serverOpts, err := tt.grpcServerOpts()
871 require.NoError(t, err)
872
873 startGRPCLogsCollector(t, n, serverOpts)
874
875 exporter, err := otlpGRPCLogExporter(tt.args.ctx, tt.args.otlpConfig)
876 require.NoError(t, err)
877
878 logFactory := sdklogtest.RecordFactory{
879 Body: log.StringValue("test"),
880 }
881
882 assert.EventuallyWithT(t, func(collect *assert.CollectT) {
883 assert.NoError(collect, exporter.Export(context.Background(), []sdklog.Record{ //nolint:usetesting // required to avoid getting a canceled context.
884 logFactory.NewRecord(),
885 }))
886 }, 10*time.Second, 1*time.Second)
887 })
888 }
889 }
890
891 // grpcLogsCollector is an OTLP gRPC server that collects all requests it receives.
892 type grpcLogsCollector struct {
893 collogpb.UnimplementedLogsServiceServer
894 }
895
896 var _ collogpb.LogsServiceServer = (*grpcLogsCollector)(nil)
897
898 // startGRPCLogsCollector returns a *grpcLogsCollector that is listening at the provided
899 // endpoint.
900 //
901 // If endpoint is an empty string, the returned collector will be listening on
902 // the localhost interface at an OS chosen port.
903 func startGRPCLogsCollector(t *testing.T, listener net.Listener, serverOptions []grpc.ServerOption) {
904 srv := grpc.NewServer(serverOptions...)
905 c := &grpcLogsCollector{}
906
907 collogpb.RegisterLogsServiceServer(srv, c)
908
909 errCh := make(chan error, 1)
910 go func() { errCh <- srv.Serve(listener) }()
911
912 t.Cleanup(func() {
913 srv.GracefulStop()
914 if err := <-errCh; err != nil && !errors.Is(err, grpc.ErrServerStopped) {
915 assert.NoError(t, err)
916 }
917 })
918 }
919
920 // Export handles the export req.
921 func (*grpcLogsCollector) Export(
922 _ context.Context,
923 _ *collogpb.ExportLogsServiceRequest,
924 ) (*collogpb.ExportLogsServiceResponse, error) {
925 return &collogpb.ExportLogsServiceResponse{}, nil
926 }