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

agent: add config option to enable file and line log detail. #769

Merged
merged 2 commits into from
Nov 10, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ IMPROVEMENTS:
* agent: Update Nomad API dependency to v1.6.0 [[GH-671](https://github.com/hashicorp/nomad-autoscaler/pull/671)]
* agent: Add specific metadata to drained nodes to allow for later identification [[GH-636](https://github.com/hashicorp/nomad-autoscaler/issues/627)]
* agent: Logged 404s from jobs/policies going away lowered to debug [[GH-723](https://github.com/hashicorp/nomad-autoscaler/pull/723/)]
* agent: Added config option to enable file and line log detail [[GH-769](https://github.com/hashicorp/nomad-autoscaler/pull/769/files)]

BUG FIXES:
* plugin/apm/datadog: Fixed a panic when Datadog queries return `null` values [[GH-606](https://github.com/hashicorp/nomad-autoscaler/pull/606)]
Expand Down
8 changes: 8 additions & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type Agent struct {
// LogJson enables log output in JSON format.
LogJson bool `hcl:"log_json,optional"`

// LogIncludeLocation dictates whether the logger includes file and line
// information on each log line. This is useful for development and
// debugging.
LogIncludeLocation bool `hcl:"log_include_location,optional"`

// EnableDebug is used to enable debugging HTTP endpoints.
EnableDebug bool `hcl:"enable_debug,optional"`

Expand Down Expand Up @@ -481,6 +486,9 @@ func (a *Agent) Merge(b *Agent) *Agent {
if b.LogJson {
result.LogJson = true
}
if b.LogIncludeLocation {
result.LogIncludeLocation = true
}
if b.PluginDir != "" {
result.PluginDir = b.PluginDir
}
Expand Down
19 changes: 11 additions & 8 deletions agent/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Test_Default(t *testing.T) {
assert.NotNil(t, def)
assert.False(t, def.LogJson)
assert.Equal(t, def.LogLevel, "info")
assert.False(t, def.LogIncludeLocation)
assert.True(t, strings.HasSuffix(def.PluginDir, "/plugins"))
assert.Equal(t, def.Policy.DefaultEvaluationInterval, 10*time.Second)
assert.Equal(t, "127.0.0.1", def.HTTP.BindAddress)
Expand Down Expand Up @@ -85,10 +86,11 @@ func TestAgent_Merge(t *testing.T) {
}

cfg2 := &Agent{
EnableDebug: true,
LogLevel: "trace",
LogJson: true,
PluginDir: "/var/lib/nomad-autoscaler/plugins",
EnableDebug: true,
LogLevel: "trace",
LogJson: true,
LogIncludeLocation: true,
PluginDir: "/var/lib/nomad-autoscaler/plugins",
DynamicApplicationSizing: &DynamicApplicationSizing{
MetricsPreloadThreshold: 12 * time.Hour,
EvaluateAfter: 2 * time.Hour,
Expand Down Expand Up @@ -187,10 +189,11 @@ func TestAgent_Merge(t *testing.T) {
}

expectedResult := &Agent{
EnableDebug: true,
LogLevel: "trace",
LogJson: true,
PluginDir: "/var/lib/nomad-autoscaler/plugins",
EnableDebug: true,
LogLevel: "trace",
LogJson: true,
LogIncludeLocation: true,
PluginDir: "/var/lib/nomad-autoscaler/plugins",
DynamicApplicationSizing: &DynamicApplicationSizing{
MetricsPreloadThreshold: 12 * time.Hour,
EvaluateAfter: 2 * time.Hour,
Expand Down
11 changes: 8 additions & 3 deletions command/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Options:
-log-json
Output logs in a JSON format. The default is false.

-log-include-location
Include file and line information in each log line. The default is false.

-enable-debug
Enable the agent debugging HTTP endpoints. The default is false.

Expand Down Expand Up @@ -310,9 +313,10 @@ func (c *AgentCommand) Run(args []string) int {

// Create the agent logger.
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
Name: "agent",
Level: hclog.LevelFromString(parsedConfig.LogLevel),
JSONFormat: parsedConfig.LogJson,
Name: "agent",
Level: hclog.LevelFromString(parsedConfig.LogLevel),
JSONFormat: parsedConfig.LogJson,
IncludeLocation: parsedConfig.LogIncludeLocation,
})

logger.Info("starting Nomad Autoscaler agent")
Expand Down Expand Up @@ -451,6 +455,7 @@ func (c *AgentCommand) readConfig() (*config.Agent, []string) {
flags.Var((*flaghelper.StringFlag)(&configPath), "config", "")
flags.StringVar(&cmdConfig.LogLevel, "log-level", "", "")
flags.BoolVar(&cmdConfig.LogJson, "log-json", false, "")
flags.BoolVar(&cmdConfig.LogIncludeLocation, "log-include-location", false, "")
flags.BoolVar(&cmdConfig.EnableDebug, "enable-debug", false, "")
flags.StringVar(&cmdConfig.PluginDir, "plugin-dir", "", "")

Expand Down
32 changes: 18 additions & 14 deletions command/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ func TestCommandAgent_readConfig(t *testing.T) {
args: []string{
"-log-level", "WARN",
"-log-json",
"-log-include-location",
"-enable-debug",
"-plugin-dir", "./plugins",
},
want: defaultConfig.Merge(&config.Agent{
LogLevel: "WARN",
LogJson: true,
EnableDebug: true,
PluginDir: "./plugins",
LogLevel: "WARN",
LogJson: true,
LogIncludeLocation: true,
EnableDebug: true,
PluginDir: "./plugins",
}),
},
{
Expand Down Expand Up @@ -210,10 +212,11 @@ func TestCommandAgent_readConfig(t *testing.T) {
"-config", "./test-fixtures/agent_config_full.hcl",
},
want: defaultConfig.Merge(&config.Agent{
LogLevel: "TRACE",
LogJson: true,
EnableDebug: true,
PluginDir: "./plugin_dir_from_file",
LogLevel: "TRACE",
LogJson: true,
LogIncludeLocation: true,
EnableDebug: true,
PluginDir: "./plugin_dir_from_file",
HTTP: &config.HTTP{
BindAddress: "10.0.0.2",
BindPort: 8888,
Expand Down Expand Up @@ -250,7 +253,7 @@ func TestCommandAgent_readConfig(t *testing.T) {
},
},
HighAvailability: &config.HighAvailability{
Enabled: ptr.BoolToPtr(true),
Enabled: ptr.Of(true),
LockPath: "my/custom/path",
LockTTL: 30 * time.Second,
LockDelay: 15 * time.Second,
Expand Down Expand Up @@ -295,10 +298,11 @@ func TestCommandAgent_readConfig(t *testing.T) {
"-config", "./test-fixtures",
},
want: defaultConfig.Merge(&config.Agent{
LogLevel: "ERROR",
LogJson: true,
EnableDebug: true,
PluginDir: "./plugin_dir_from_file",
LogLevel: "ERROR",
LogJson: true,
LogIncludeLocation: true,
EnableDebug: true,
PluginDir: "./plugin_dir_from_file",
HTTP: &config.HTTP{
BindAddress: "10.0.0.2",
BindPort: 8888,
Expand Down Expand Up @@ -335,7 +339,7 @@ func TestCommandAgent_readConfig(t *testing.T) {
},
},
HighAvailability: &config.HighAvailability{
Enabled: ptr.BoolToPtr(true),
Enabled: ptr.Of(true),
LockPath: "my/custom/path",
LockTTL: 30 * time.Second,
LockDelay: 15 * time.Second,
Expand Down
9 changes: 5 additions & 4 deletions command/test-fixtures/agent_config_full.hcl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

log_level = "TRACE"
log_json = true
enable_debug = true
plugin_dir = "./plugin_dir_from_file"
log_level = "TRACE"
log_json = true
log_include_location = true
enable_debug = true
plugin_dir = "./plugin_dir_from_file"

http {
bind_address = "10.0.0.2"
Expand Down