Skip to content

Commit a03f7cf

Browse files
authored
Set OTel Collector Log Level To Match Agent (#1186)
1 parent 9d6131a commit a03f7cf

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

internal/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,14 @@ func normalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
789789
}
790790

791791
func resolveLog() *Log {
792+
logLevel := strings.ToLower(viperInstance.GetString(LogLevelKey))
793+
validLevels := []string{"debug", "info", "warn", "error"}
794+
795+
if !slices.Contains(validLevels, logLevel) {
796+
slog.Warn("Invalid log level set, defaulting to 'info'", "log_level", logLevel)
797+
viperInstance.Set(LogLevelKey, "info")
798+
}
799+
792800
return &Log{
793801
Level: viperInstance.GetString(LogLevelKey),
794802
Path: viperInstance.GetString(LogPathKey),
@@ -1168,6 +1176,10 @@ func isHealthExtensionSet() bool {
11681176
}
11691177

11701178
func resolveCollectorLog() *Log {
1179+
if !viperInstance.IsSet(CollectorLogLevelKey) {
1180+
viperInstance.Set(CollectorLogLevelKey, strings.ToUpper(viperInstance.GetString(LogLevelKey)))
1181+
}
1182+
11711183
return &Log{
11721184
Level: viperInstance.GetString(CollectorLogLevelKey),
11731185
Path: viperInstance.GetString(CollectorLogPathKey),

internal/config/config_test.go

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,54 @@ func TestResolveAllowedDirectories(t *testing.T) {
243243

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

249-
result := resolveLog()
250-
assert.Equal(t, "error", result.Level)
251-
assert.Equal(t, "/var/log/test/test.log", result.Path)
247+
tests := []struct {
248+
name string
249+
logLevel string
250+
logPath string
251+
expectedLogPath string
252+
expectedLogLevel string
253+
}{
254+
{
255+
name: "Test 1: Log level set to info",
256+
logLevel: "info",
257+
logPath: "/var/log/test/test.log",
258+
expectedLogPath: "/var/log/test/test.log",
259+
expectedLogLevel: "info",
260+
},
261+
{
262+
name: "Test 2: Invalid log level set",
263+
logLevel: "trace",
264+
logPath: "/var/log/test/test.log",
265+
expectedLogPath: "/var/log/test/test.log",
266+
expectedLogLevel: "info",
267+
},
268+
{
269+
name: "Test 3: Log level set to debug",
270+
logLevel: "debug",
271+
logPath: "/var/log/test/test.log",
272+
expectedLogPath: "/var/log/test/test.log",
273+
expectedLogLevel: "debug",
274+
},
275+
{
276+
name: "Test 4: Log level set with capitalization",
277+
logLevel: "DEBUG",
278+
logPath: "./logs/nginx.log",
279+
expectedLogPath: "./logs/nginx.log",
280+
expectedLogLevel: "DEBUG",
281+
},
282+
}
283+
284+
for _, test := range tests {
285+
t.Run(test.name, func(t *testing.T) {
286+
viperInstance.Set(LogLevelKey, test.logLevel)
287+
viperInstance.Set(LogPathKey, test.logPath)
288+
289+
result := resolveLog()
290+
assert.Equal(t, test.expectedLogLevel, result.Level)
291+
assert.Equal(t, test.expectedLogPath, result.Path)
292+
})
293+
}
252294
}
253295

254296
func TestResolveClient(t *testing.T) {
@@ -298,6 +340,59 @@ func TestResolveCollector(t *testing.T) {
298340
})
299341
}
300342

343+
func TestResolveCollectorLog(t *testing.T) {
344+
tests := []struct {
345+
name string
346+
logLevel string
347+
logPath string
348+
agentLogLevel string
349+
expectedLogPath string
350+
expectedLogLevel string
351+
}{
352+
{
353+
name: "Test 1: OTel Log Level Set In Config",
354+
logLevel: "",
355+
logPath: "/tmp/collector.log",
356+
agentLogLevel: "debug",
357+
expectedLogPath: "/tmp/collector.log",
358+
expectedLogLevel: "DEBUG",
359+
},
360+
{
361+
name: "Test 2: Agent Log Level is Warn",
362+
logLevel: "",
363+
logPath: "/tmp/collector.log",
364+
agentLogLevel: "warn",
365+
expectedLogPath: "/tmp/collector.log",
366+
expectedLogLevel: "WARN",
367+
},
368+
{
369+
name: "Test 3: OTel Log Level Set In Config",
370+
logLevel: "INFO",
371+
logPath: "/tmp/collector.log",
372+
agentLogLevel: "debug",
373+
expectedLogPath: "/tmp/collector.log",
374+
expectedLogLevel: "INFO",
375+
},
376+
}
377+
378+
for _, test := range tests {
379+
t.Run(test.name, func(t *testing.T) {
380+
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
381+
viperInstance.Set(CollectorLogPathKey, test.logPath)
382+
viperInstance.Set(LogLevelKey, test.agentLogLevel)
383+
384+
if test.logLevel != "" {
385+
viperInstance.Set(CollectorLogLevelKey, test.logLevel)
386+
}
387+
388+
log := resolveCollectorLog()
389+
390+
assert.Equal(t, test.expectedLogLevel, log.Level)
391+
assert.Equal(t, test.expectedLogPath, log.Path)
392+
})
393+
}
394+
}
395+
301396
func TestCommand(t *testing.T) {
302397
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
303398
expected := agentConfig().Command

0 commit comments

Comments
 (0)