forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
91 lines (86 loc) · 2.3 KB
/
logger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package telemetry // import "go.opentelemetry.io/collector/service/telemetry"
import (
"context"
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/contrib/config"
"go.uber.org/zap/zapcore"
)
func TestNewLogger(t *testing.T) {
tests := []struct {
name string
wantCoreType any
wantErr error
cfg Config
}{
{
name: "no log config",
cfg: Config{},
wantErr: errors.New("no encoder name specified"),
wantCoreType: nil,
},
{
name: "log config with no processors",
cfg: Config{
Logs: LogsConfig{
Level: zapcore.DebugLevel,
Development: true,
Encoding: "console",
DisableCaller: true,
DisableStacktrace: true,
InitialFields: map[string]any{"fieldKey": "filed-value"},
},
},
wantCoreType: "*zapcore.ioCore",
},
{
name: "log config with processors",
cfg: Config{
Logs: LogsConfig{
Level: zapcore.DebugLevel,
Development: true,
Encoding: "console",
DisableCaller: true,
DisableStacktrace: true,
InitialFields: map[string]any{"fieldKey": "filed-value"},
Processors: []config.LogRecordProcessor{
{
Batch: &config.BatchLogRecordProcessor{
Exporter: config.LogRecordExporter{
Console: config.Console{},
},
},
},
},
},
},
wantCoreType: "*zapcore.levelFilterCore",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sdk, _ := config.NewSDK(config.WithOpenTelemetryConfiguration(config.OpenTelemetryConfiguration{LoggerProvider: &config.LoggerProvider{
Processors: tt.cfg.Logs.Processors,
}}))
l, lp, err := newLogger(Settings{SDK: &sdk}, tt.cfg)
if tt.wantErr != nil {
require.ErrorContains(t, err, tt.wantErr.Error())
require.Nil(t, tt.wantCoreType)
} else {
require.NoError(t, err)
gotType := reflect.TypeOf(l.Core()).String()
require.Equal(t, tt.wantCoreType, gotType)
type shutdownable interface {
Shutdown(context.Context) error
}
if prov, ok := lp.(shutdownable); ok {
require.NoError(t, prov.Shutdown(context.Background()))
}
}
})
}
}