Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,14 @@ func normalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
}

func resolveLog() *Log {
logLevel := strings.ToLower(viperInstance.GetString(LogLevelKey))
validLevels := []string{"debug", "info", "warn", "error"}

if !slices.Contains(validLevels, logLevel) {
slog.Warn("Invalid log level set, defaulting to 'info'", "log_level", logLevel)
viperInstance.Set(LogLevelKey, "info")
}

return &Log{
Level: viperInstance.GetString(LogLevelKey),
Path: viperInstance.GetString(LogPathKey),
Expand Down Expand Up @@ -1168,6 +1176,10 @@ func isHealthExtensionSet() bool {
}

func resolveCollectorLog() *Log {
if !viperInstance.IsSet(CollectorLogLevelKey) {
viperInstance.Set(CollectorLogLevelKey, strings.ToUpper(viperInstance.GetString(LogLevelKey)))
}

return &Log{
Level: viperInstance.GetString(CollectorLogLevelKey),
Path: viperInstance.GetString(CollectorLogPathKey),
Expand Down
105 changes: 100 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,54 @@ func TestResolveAllowedDirectories(t *testing.T) {

func TestResolveLog(t *testing.T) {
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
viperInstance.Set(LogLevelKey, "error")
viperInstance.Set(LogPathKey, "/var/log/test/test.log")

result := resolveLog()
assert.Equal(t, "error", result.Level)
assert.Equal(t, "/var/log/test/test.log", result.Path)
tests := []struct {
name string
logLevel string
logPath string
expectedLogPath string
expectedLogLevel string
}{
{
name: "Test 1: Log level set to info",
logLevel: "info",
logPath: "/var/log/test/test.log",
expectedLogPath: "/var/log/test/test.log",
expectedLogLevel: "info",
},
{
name: "Test 2: Invalid log level set",
logLevel: "trace",
logPath: "/var/log/test/test.log",
expectedLogPath: "/var/log/test/test.log",
expectedLogLevel: "info",
},
{
name: "Test 3: Log level set to debug",
logLevel: "debug",
logPath: "/var/log/test/test.log",
expectedLogPath: "/var/log/test/test.log",
expectedLogLevel: "debug",
},
{
name: "Test 4: Log level set with capitalization",
logLevel: "DEBUG",
logPath: "./logs/nginx.log",
expectedLogPath: "./logs/nginx.log",
expectedLogLevel: "DEBUG",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
viperInstance.Set(LogLevelKey, test.logLevel)
viperInstance.Set(LogPathKey, test.logPath)

result := resolveLog()
assert.Equal(t, test.expectedLogLevel, result.Level)
assert.Equal(t, test.expectedLogPath, result.Path)
})
}
}

func TestResolveClient(t *testing.T) {
Expand Down Expand Up @@ -298,6 +340,59 @@ func TestResolveCollector(t *testing.T) {
})
}

func TestResolveCollectorLog(t *testing.T) {
tests := []struct {
name string
logLevel string
logPath string
agentLogLevel string
expectedLogPath string
expectedLogLevel string
}{
{
name: "Test 1: OTel Log Level Set In Config",
logLevel: "",
logPath: "/tmp/collector.log",
agentLogLevel: "debug",
expectedLogPath: "/tmp/collector.log",
expectedLogLevel: "DEBUG",
},
{
name: "Test 2: Agent Log Level is Warn",
logLevel: "",
logPath: "/tmp/collector.log",
agentLogLevel: "warn",
expectedLogPath: "/tmp/collector.log",
expectedLogLevel: "WARN",
},
{
name: "Test 3: OTel Log Level Set In Config",
logLevel: "INFO",
logPath: "/tmp/collector.log",
agentLogLevel: "debug",
expectedLogPath: "/tmp/collector.log",
expectedLogLevel: "INFO",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
viperInstance.Set(CollectorLogPathKey, test.logPath)
viperInstance.Set(LogLevelKey, test.agentLogLevel)

if test.logLevel != "" {
viperInstance.Set(CollectorLogLevelKey, test.logLevel)
}

log := resolveCollectorLog()

assert.Equal(t, test.expectedLogLevel, log.Level)
assert.Equal(t, test.expectedLogPath, log.Path)
})
}
}

func TestCommand(t *testing.T) {
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
expected := agentConfig().Command
Expand Down