-
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.
- Loading branch information
1 parent
0b60ef8
commit 0add644
Showing
1 changed file
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"go.opentelemetry.io/collector/config/configtelemetry" | ||
) | ||
|
||
func TestTelemetryConfiguration(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg *Config | ||
success bool | ||
}{ | ||
{ | ||
name: "Valid config", | ||
cfg: &Config{ | ||
Logs: LogsConfig{ | ||
Level: zapcore.DebugLevel, | ||
Encoding: "console", | ||
}, | ||
Metrics: MetricsConfig{ | ||
Level: configtelemetry.LevelBasic, | ||
Address: "127.0.0.1:3333", | ||
}, | ||
}, | ||
success: true, | ||
}, | ||
{ | ||
name: "Invalid config", | ||
cfg: &Config{ | ||
Logs: LogsConfig{ | ||
Level: zapcore.DebugLevel, | ||
}, | ||
Metrics: MetricsConfig{ | ||
Level: configtelemetry.LevelBasic, | ||
Address: "127.0.0.1:3333", | ||
}, | ||
}, | ||
success: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
telemetry, err := New(context.Background(), Settings{ZapOptions: []zap.Option{}}, *tt.cfg) | ||
if tt.success { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, telemetry) | ||
} else { | ||
assert.Error(t, err) | ||
assert.Nil(t, telemetry) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSampledLoggerCreateFirstTime(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg *Config | ||
}{ | ||
{ | ||
name: "Default sampling", | ||
cfg: &Config{ | ||
Logs: LogsConfig{ | ||
Level: zapcore.DebugLevel, | ||
Encoding: "console", | ||
}, | ||
Metrics: MetricsConfig{ | ||
Level: configtelemetry.LevelBasic, | ||
Address: "127.0.0.1:3333", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Custom sampling", | ||
cfg: &Config{ | ||
Logs: LogsConfig{ | ||
Level: zapcore.DebugLevel, | ||
Encoding: "console", | ||
Sampling: &LogsSamplingConfig{ | ||
Initial: 50, | ||
Tick: 2 * time.Second, | ||
Thereafter: 40, | ||
}, | ||
}, | ||
Metrics: MetricsConfig{ | ||
Level: configtelemetry.LevelBasic, | ||
Address: "127.0.0.1:3333", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
telemetry, err := New(context.Background(), Settings{ZapOptions: []zap.Option{}}, *tt.cfg) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, telemetry) | ||
assert.Nil(t, telemetry.sampledLogger) | ||
getSampledLogger := telemetry.SampledLogger() | ||
assert.NotNil(t, getSampledLogger()) | ||
assert.Equal(t, getSampledLogger(), telemetry.sampledLogger) | ||
}) | ||
} | ||
} |