-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test, return logger provider in factory
Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
157 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 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 and invalid config", | ||
cfg: Config{ | ||
Logs: LogsConfig{ | ||
Encoding: "console", | ||
Processors: []config.LogRecordProcessor{ | ||
{ | ||
Batch: &config.BatchLogRecordProcessor{ | ||
Exporter: config.LogRecordExporter{ | ||
OTLP: &config.OTLP{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: errors.New("unsupported protocol \"\""), | ||
}, | ||
{ | ||
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.multiCore", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l, lp, err := newLogger(context.Background(), Settings{}, 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())) | ||
} | ||
} | ||
}) | ||
} | ||
} |