Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[otel-agent] log level: case insensitivity to set the log level #32619

Merged
merged 5 commits into from
Jan 6, 2025
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
5 changes: 3 additions & 2 deletions cmd/otel-agent/config/agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,22 @@ func NewConfigComponent(ctx context.Context, ddCfg string, uris []string) (confi
return nil, err
}
var ok bool
activeLogLevel, ok = logLevelMap[pkgconfig.GetString("log_level")]
activeLogLevel, ok = logLevelMap[strings.ToLower(pkgconfig.GetString("log_level"))]
if !ok {
return nil, fmt.Errorf("invalid log level (%v) set in the Datadog Agent configuration", pkgconfig.GetString("log_level"))
}
}

// Set the right log level. The most verbose setting takes precedence.
telemetryLogLevel := sc.Telemetry.Logs.Level
telemetryLogMapping, ok := logLevelMap[telemetryLogLevel.String()]
telemetryLogMapping, ok := logLevelMap[strings.ToLower(telemetryLogLevel.String())]
if !ok {
return nil, fmt.Errorf("invalid log level (%v) set in the OTel Telemetry configuration", telemetryLogLevel.String())
}
if telemetryLogMapping < activeLogLevel {
activeLogLevel = telemetryLogMapping
}
fmt.Printf("setting log level to: %v\n", logLevelReverseMap[activeLogLevel])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is useful to keep, previously it took us some time to figure out the log level wasn't respected

pkgconfig.Set("log_level", logLevelReverseMap[activeLogLevel], pkgconfigmodel.SourceFile)

// Override config read (if any) with Default values
Expand Down
24 changes: 23 additions & 1 deletion cmd/otel-agent/config/agent_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,29 @@ func (suite *ConfigTestSuite) TestEnvBadLogLevel() {
fileName := "testdata/config_default.yaml"
ddFileName := "testdata/datadog_low_log_level.yaml"
_, err := NewConfigComponent(context.Background(), ddFileName, []string{fileName})
assert.Error(t, err)
assert.EqualError(t, err, "invalid log level (yabadabadooo) set in the Datadog Agent configuration")
}

func (suite *ConfigTestSuite) TestEnvUpperCaseLogLevel() {
t := suite.T()
oldval, exists := os.LookupEnv("DD_LOG_LEVEL")
os.Unsetenv("DD_LOG_LEVEL")
defer func() {
if !exists {
os.Unsetenv("DD_LOG_LEVEL")
} else {
os.Setenv("DD_LOG_LEVEL", oldval)
}
}()
fileName := "testdata/config_default.yaml"
ddFileName := "testdata/datadog_uppercase_log_level.yaml"
c, err := NewConfigComponent(context.Background(), ddFileName, []string{fileName})
if err != nil {
t.Errorf("Failed to load agent config: %v", err)
}

// log_level will be mapped to lowercase by code and set accordingly
assert.Equal(t, "info", c.Get("log_level"))
}

func (suite *ConfigTestSuite) TestBadDDConfigFile() {
Expand Down
10 changes: 10 additions & 0 deletions cmd/otel-agent/config/testdata/datadog_uppercase_log_level.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
log_level: INFO

otelcollector:
enabled: true
extension_url: "https://localhost:7777"

agent_ipc:
port: 5009
config_refresh_interval: 60

Loading