Skip to content

Commit 28d2929

Browse files
Exclude integration error logs by default. (#1816)
* Exclude integration error logs by default (Except the one causing to exit with status > 1) but allow including them using filters. * Add Error and Fatal Logs inclussion on log_configuration.md
1 parent 9d72cec commit 28d2929

File tree

8 files changed

+49
-17
lines changed

8 files changed

+49
-17
lines changed

docs/log_configuration.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ strongly recommend using the new configuration.
4141
The new log configuration has added two new options to filter logs based on key-values (fields). They can be used in
4242
order to remove logging noise in a troubleshooting scenario.
4343

44-
By default, all entries will be included* in the logs (`include_filters`). To exclude some entries, we must define the
44+
By default, all entries will be included* in the logs (`include_filters`) except the integration execution errors. To exclude some entries, we must define the
4545
key-values to remove using the `exclude_filters` option. The following text is a usual agent's log line:
4646

4747
`time="2022-06-10T15:46:38Z" level=debug msg="Integration instances finished their execution. Waiting until next interval." component=integrations.runner.Runner integration_name=nri-flex runner_uid=c03734e49d`
@@ -79,6 +79,16 @@ log:
7979
- nri-nginx
8080
```
8181

82+
* Only general information from an integration execution is logged, Error and Fatal logs are only visible under debug level. If we want to always show them under any log level for a specific integration we'll need to add it under the include_filters:
83+
84+
```yaml
85+
log:
86+
include_filters:
87+
integration_name:
88+
- nri-flex
89+
- nri-nginx
90+
```
91+
8292
* The integration supervisor trace logs are excluded by default due the big amount of produced log entries, the
8393
following configuration would enable them:
8494

internal/integrations/v4/runner/runner.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/newrelic/infrastructure-agent/internal/agent/instrumentation"
1414
"github.com/newrelic/infrastructure-agent/internal/integrations/v4/constants"
15+
"github.com/newrelic/infrastructure-agent/pkg/config" //nolint:depguard
1516
"github.com/newrelic/infrastructure-agent/pkg/entity/host"
1617

1718
"github.com/newrelic/infrastructure-agent/internal/integrations/v4/cache"
@@ -310,20 +311,29 @@ func (r *runner) handleStderr(stderr <-chan []byte) {
310311
logLine := r.log.WithFields(logrus.Fields(fields))
311312
logMessage := "integration log"
312313

314+
lineWasParsed = true
315+
316+
if r.log.IsDebugEnabled() {
317+
// If Debug is enabled the level is ignored.
318+
logLine.Debug(logMessage)
319+
320+
break
321+
}
322+
313323
lvl, ok := lvl.(string)
314324
if ok {
315325
switch strings.ToLower(lvl) {
316-
// We will set all this levels as debug to not be shown by default
326+
// We only want to respect Fatal and Error levels.
317327
case "info", "debug", "warn", "warning":
318328
logLine.Debug(logMessage)
319329
case "trace":
320330
logLine.Trace(logMessage)
321331
default:
322-
logLine.Error(logMessage)
332+
// This error log line has the filtered-by-default "integration-errors" component.
333+
// Unless included on the config, this log lines won't be logged by the agent.
334+
logLine.WithComponent(config.IntegrationsErrorsValue).Error(logMessage)
323335
}
324336

325-
lineWasParsed = true
326-
327337
break
328338
}
329339
}

internal/integrations/v4/runner/runner_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ func Test_runner_Run_Integration_Log(t *testing.T) {
200200
hook := new(test.Hook)
201201
log.AddHook(hook)
202202

203-
log.SetLevel(logrus.TraceLevel)
204-
205203
testCases := []struct {
206204
name string
207205
logLine string
@@ -224,7 +222,7 @@ func Test_runner_Run_Integration_Log(t *testing.T) {
224222
name: "SDK_Trace_log",
225223
logLine: "[TRACE] This is a trace message",
226224
expectedLogMsg: "This is a trace message",
227-
expectedLevel: logrus.TraceLevel,
225+
expectedLevel: logrus.DebugLevel,
228226
},
229227
{
230228
name: "SDK_Warning_log",
@@ -260,7 +258,7 @@ func Test_runner_Run_Integration_Log(t *testing.T) {
260258
name: "Logrus_Trace_log",
261259
logLine: "level=trace msg=\"This is a trace message\"",
262260
expectedLogMsg: "This is a trace message",
263-
expectedLevel: logrus.TraceLevel,
261+
expectedLevel: logrus.DebugLevel,
264262
},
265263
{
266264
name: "Logrus_Warning_log",
@@ -291,7 +289,8 @@ func Test_runner_Run_Integration_Log(t *testing.T) {
291289
for _, tt := range testCases {
292290
testCase := tt
293291
t.Run(testCase.name, func(t *testing.T) {
294-
t.Parallel()
292+
log.SetLevel(testCase.expectedLevel)
293+
295294
// GIVEN a runner that receives a cfg request without a handle function.
296295
def, err := integration.NewDefinition(config.ConfigEntry{
297296
InstanceName: testCase.name,

pkg/config/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343
FeatureTrace = "feature"
4444
ProcessTrace = "process"
4545

46+
IntegrationsErrorsField = "component"
47+
IntegrationsErrorsValue = "integration-errors"
48+
4649
// LogFilterWildcard will match everything.
4750
LogFilterWildcard = "*"
4851

@@ -1332,6 +1335,9 @@ func (lc *LogConfig) AttachDefaultFilters() {
13321335

13331336
// Exclude by default supervisor and feature traces.
13341337
lc.ExcludeFilters[TracesFieldName] = append(lc.ExcludeFilters[TracesFieldName], SupervisorTrace, FeatureTrace, ProcessTrace)
1338+
1339+
// Exclude all integration error logs by default
1340+
lc.ExcludeFilters[IntegrationsErrorsField] = append(lc.ExcludeFilters[IntegrationsErrorsField], IntegrationsErrorsValue)
13351341
}
13361342

13371343
// HasIncludeFilter returns true if key-value pair are included in the filtering configuration.

pkg/config/config_darwin_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ func Test_ParseLogConfigRule_EnvVar(t *testing.T) {
115115
Forward: nil,
116116
SmartLevelEntryLimit: &expectedSmartLevelEntryLimit,
117117
IncludeFilters: map[string][]interface{}{"component": {"ProcessSample", "StorageSample"}},
118-
ExcludeFilters: map[string][]interface{}{TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}},
118+
ExcludeFilters: map[string][]interface{}{
119+
TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}, IntegrationsErrorsField: {IntegrationsErrorsValue},
120+
},
119121
Rotate: LogRotateConfig{
120122
MaxSizeMb: intPtr(0),
121123
MaxFiles: 0,

pkg/config/config_linux_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ func Test_ParseLogConfigRule_EnvVar(t *testing.T) {
196196
Forward: nil,
197197
SmartLevelEntryLimit: &expectedSmartLevelEntryLimit,
198198
IncludeFilters: map[string][]interface{}{"component": {"ProcessSample", "StorageSample"}},
199-
ExcludeFilters: map[string][]interface{}{TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}},
199+
ExcludeFilters: map[string][]interface{}{
200+
TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}, IntegrationsErrorsField: {IntegrationsErrorsValue},
201+
},
200202
Rotate: LogRotateConfig{
201203
MaxSizeMb: intPtr(0),
202204
MaxFiles: 0,

pkg/config/config_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ func TestLoadLogConfig_BackwardsCompatability(t *testing.T) {
783783
}
784784
}
785785

786+
//nolint:exhaustruct,lll
786787
func TestLoadLogConfig_Populate(t *testing.T) {
787788
// TODO: migrate to generic function with go1.18
788789
boolPtr := func(a bool) *bool {
@@ -796,10 +797,10 @@ func TestLoadLogConfig_Populate(t *testing.T) {
796797
c Config
797798
expectedLogConfig LogConfig
798799
}{
799-
{"Verbose disabled (info level) and custom log file", Config{Verbose: 0, LogFile: "agent.log"}, LogConfig{Level: LogLevelInfo, File: "agent.log", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}}, SmartLevelEntryLimit: intPtr(0)}},
800-
{"Smart Verbose enabled with defined limit", Config{Verbose: 2, SmartVerboseModeEntryLimit: 200}, LogConfig{Level: LogLevelSmart, File: "", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}}, SmartLevelEntryLimit: intPtr(200)}},
801-
{"Forward Verbose enabled and stdout", Config{Verbose: 3, LogToStdout: true}, LogConfig{Level: LogLevelDebug, File: "", ToStdout: boolPtr(true), Forward: boolPtr(true), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}}, SmartLevelEntryLimit: intPtr(0)}},
802-
{"Trace Verbose enabled and file", Config{Verbose: 4, LogFile: "agent.log"}, LogConfig{Level: LogLevelTrace, File: "agent.log", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}}, SmartLevelEntryLimit: intPtr(0)}},
800+
{"Verbose disabled (info level) and custom log file", Config{Verbose: 0, LogFile: "agent.log"}, LogConfig{Level: LogLevelInfo, File: "agent.log", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}, "component": []interface{}{"integration-errors"}}, SmartLevelEntryLimit: intPtr(0)}},
801+
{"Smart Verbose enabled with defined limit", Config{Verbose: 2, SmartVerboseModeEntryLimit: 200}, LogConfig{Level: LogLevelSmart, File: "", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}, "component": []interface{}{"integration-errors"}}, SmartLevelEntryLimit: intPtr(200)}},
802+
{"Forward Verbose enabled and stdout", Config{Verbose: 3, LogToStdout: true}, LogConfig{Level: LogLevelDebug, File: "", ToStdout: boolPtr(true), Forward: boolPtr(true), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}, "component": []interface{}{"integration-errors"}}, SmartLevelEntryLimit: intPtr(0)}},
803+
{"Trace Verbose enabled and file", Config{Verbose: 4, LogFile: "agent.log"}, LogConfig{Level: LogLevelTrace, File: "agent.log", ToStdout: boolPtr(false), Forward: boolPtr(false), ExcludeFilters: LogFilters{"traces": []interface{}{"supervisor", "feature", "process"}, "component": []interface{}{"integration-errors"}}, SmartLevelEntryLimit: intPtr(0)}},
803804
}
804805

805806
for _, tt := range configs {

pkg/config/config_windows_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func Test_ParseLogConfigRule_EnvVar(t *testing.T) {
113113
Forward: nil,
114114
SmartLevelEntryLimit: &expectedSmartLevelEntryLimit,
115115
IncludeFilters: map[string][]interface{}{"component": {"ProcessSample", "StorageSample"}},
116-
ExcludeFilters: map[string][]interface{}{TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}},
116+
ExcludeFilters: map[string][]interface{}{
117+
TracesFieldName: {SupervisorTrace, FeatureTrace, ProcessTrace}, IntegrationsErrorsField: {IntegrationsErrorsValue},
118+
},
117119
Rotate: LogRotateConfig{
118120
MaxSizeMb: intPtr(100),
119121
MaxFiles: 5,

0 commit comments

Comments
 (0)