From 613b7c6cb09b7dd7cbc69d5f50fe2a9001dbdbb3 Mon Sep 17 00:00:00 2001 From: Seth Levine Date: Fri, 6 Oct 2023 13:46:20 -0400 Subject: [PATCH] Skip Timestamp For Agent Log --- .../sampleConfig/no_skip_log_timestamp.conf | 37 +++ .../sampleConfig/no_skip_log_timestamp.json | 15 + .../no_skip_log_timestamp_windows.conf | 37 +++ .../no_skip_log_timestamp_windows.json | 16 + .../sampleConfig/skip_log_timestamp.conf | 35 +++ .../sampleConfig/skip_log_timestamp.json | 18 ++ .../skip_log_timestamp_default.conf | 35 +++ .../skip_log_timestamp_default.json | 15 + .../skip_log_timestamp_default_windows.conf | 35 +++ .../skip_log_timestamp_default_windows.json | 15 + .../skip_log_timestamp_windows.conf | 35 +++ .../skip_log_timestamp_windows.json | 18 ++ translator/tocwconfig/tocwconfig_test.go | 281 +++++++++++++++--- translator/translate/agent/ruleLogFile.go | 1 + .../files/collect_list/ruleTimestampFormat.go | 17 +- 15 files changed, 570 insertions(+), 40 deletions(-) create mode 100644 translator/tocwconfig/sampleConfig/no_skip_log_timestamp.conf create mode 100644 translator/tocwconfig/sampleConfig/no_skip_log_timestamp.json create mode 100644 translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.conf create mode 100644 translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.json create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp.conf create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp.json create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_default.conf create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_default.json create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.conf create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.json create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.conf create mode 100644 translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.json diff --git a/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.conf b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.conf new file mode 100644 index 0000000000..5bf0ea1d61 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.conf @@ -0,0 +1,37 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "/opt/aws/amazon-cloudwatch-agent/logs/state" + + [[inputs.logfile.file_config]] + file_path = "/tmp/not-amazon-cloudwatch-agent.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + timestamp_layout = "15:04:05 06 Jan 02" + timestamp_regex = "(d{2}:d{2}:d{2} d{2} w{3} d{2})" + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.json b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.json new file mode 100644 index 0000000000..4e04deb3de --- /dev/null +++ b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp.json @@ -0,0 +1,15 @@ +{ + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "/tmp/not-amazon-cloudwatch-agent.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.conf b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.conf new file mode 100644 index 0000000000..ef0961d523 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.conf @@ -0,0 +1,37 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\state" + + [[inputs.logfile.file_config]] + file_path = "c:\\tmp\\not-amazon-cloudwatch-agent.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + timestamp_layout = "15:04:05 06 Jan 02" + timestamp_regex = "(d{2}:d{2}:d{2} d{2} w{3} d{2})" + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.json b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.json new file mode 100644 index 0000000000..fb7bf89559 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/no_skip_log_timestamp_windows.json @@ -0,0 +1,16 @@ +{ + + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "c:\\tmp\\not-amazon-cloudwatch-agent.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp.conf b/translator/tocwconfig/sampleConfig/skip_log_timestamp.conf new file mode 100644 index 0000000000..527d132104 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp.conf @@ -0,0 +1,35 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "/opt/tmp/a.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "/opt/aws/amazon-cloudwatch-agent/logs/state" + + [[inputs.logfile.file_config]] + file_path = "/opt/tmp/a.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp.json b/translator/tocwconfig/sampleConfig/skip_log_timestamp.json new file mode 100644 index 0000000000..db134e261e --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp.json @@ -0,0 +1,18 @@ +{ + "agent": { + "logfile": "/opt/tmp/a.log" + }, + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "/opt/tmp/a.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.conf b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.conf new file mode 100644 index 0000000000..3cb30744ec --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.conf @@ -0,0 +1,35 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "/opt/aws/amazon-cloudwatch-agent/logs/state" + + [[inputs.logfile.file_config]] + file_path = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.json b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.json new file mode 100644 index 0000000000..734df33a1b --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default.json @@ -0,0 +1,15 @@ +{ + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.conf b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.conf new file mode 100644 index 0000000000..36c2684e47 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.conf @@ -0,0 +1,35 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\state" + + [[inputs.logfile.file_config]] + file_path = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.json b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.json new file mode 100644 index 0000000000..0ba8b224fb --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_default_windows.json @@ -0,0 +1,15 @@ +{ + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\amazon-cloudwatch-agent.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.conf b/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.conf new file mode 100644 index 0000000000..fbd9d27b6a --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.conf @@ -0,0 +1,35 @@ +[agent] + collection_jitter = "0s" + debug = false + flush_interval = "1s" + flush_jitter = "0s" + hostname = "" + interval = "60s" + logfile = "c:\\tmp\\am.log" + logtarget = "lumberjack" + metric_batch_size = 1000 + metric_buffer_limit = 10000 + omit_hostname = false + precision = "" + quiet = false + round_interval = false + +[inputs] + + [[inputs.logfile]] + destination = "cloudwatchlogs" + file_state_folder = "c:\\ProgramData\\Amazon\\AmazonCloudWatchAgent\\Logs\\state" + + [[inputs.logfile.file_config]] + file_path = "c:\\tmp\\am.log" + from_beginning = true + log_group_name = "amazon-cloudwatch-agent.log" + pipe = false + retention_in_days = -1 + +[outputs] + + [[outputs.cloudwatchlogs]] + force_flush_interval = "5s" + log_stream_name = "i-UNKNOWN" + region = "us-west-2" diff --git a/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.json b/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.json new file mode 100644 index 0000000000..66394e40c7 --- /dev/null +++ b/translator/tocwconfig/sampleConfig/skip_log_timestamp_windows.json @@ -0,0 +1,18 @@ +{ + "agent": { + "logfile": "c:\\tmp\\am.log" + }, + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { + "file_path": "c:\\tmp\\am.log", + "log_group_name": "amazon-cloudwatch-agent.log", + "timestamp_format": "%H:%M:%S %y %b %d" + } + ] + } + } + } +} \ No newline at end of file diff --git a/translator/tocwconfig/tocwconfig_test.go b/translator/tocwconfig/tocwconfig_test.go index 1c0e010c27..bf5b0ce323 100644 --- a/translator/tocwconfig/tocwconfig_test.go +++ b/translator/tocwconfig/tocwconfig_test.go @@ -47,6 +47,13 @@ const ( //go:embed sampleConfig/prometheus_config.yaml var prometheusConfig string +type testCase struct { + filename string + targetPlatform string + expectedEnvVars map[string]string + appendString string +} + func TestBaseContainerInsightsConfig(t *testing.T) { resetContext(t) context.CurrentContext().SetRunInContainer(true) @@ -101,11 +108,32 @@ func TestWindowsEventOnlyConfig(t *testing.T) { } func TestStatsDConfig(t *testing.T) { - resetContext(t) - expectedEnvVars := map[string]string{} - checkTranslation(t, "statsd_config", "linux", expectedEnvVars, "_linux") - checkTranslation(t, "statsd_config", "windows", expectedEnvVars, "_windows") - checkTranslation(t, "statsd_config", "darwin", nil, "_linux") + testCases := map[string]testCase{ + "linux": { + filename: "statsd_config", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "_linux", + }, + "windows": { + filename: "statsd_config", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "_windows", + }, + "darwin": { + filename: "statsd_config", + targetPlatform: "darwin", + expectedEnvVars: nil, + appendString: "_linux", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } } // Linux only for CollectD @@ -141,11 +169,32 @@ func TestPrometheusConfig(t *testing.T) { } func TestBasicConfig(t *testing.T) { - resetContext(t) - expectedEnvVars := map[string]string{} - checkTranslation(t, "basic_config_linux", "linux", expectedEnvVars, "") - checkTranslation(t, "basic_config_linux", "darwin", nil, "") - checkTranslation(t, "basic_config_windows", "windows", expectedEnvVars, "") + testCases := map[string]testCase{ + "linux": { + filename: "basic_config_linux", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "darwin": { + filename: "basic_config_linux", + targetPlatform: "darwin", + expectedEnvVars: nil, + appendString: "", + }, + "windows": { + filename: "basic_config_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } } func TestInvalidInputConfig(t *testing.T) { @@ -157,20 +206,62 @@ func TestInvalidInputConfig(t *testing.T) { func TestStandardConfig(t *testing.T) { // the way our config translator works is int(0) leaves an empty in the yaml // this will default to 0 on contrib side since int default is 0 for golang - resetContext(t) - t.Setenv(envconfig.IMDS_NUMBER_RETRY, "0") - expectedEnvVars := map[string]string{} - checkTranslation(t, "standard_config_linux", "linux", expectedEnvVars, "") - checkTranslation(t, "standard_config_linux", "darwin", nil, "") - checkTranslation(t, "standard_config_windows", "windows", nil, "") + testCases := map[string]testCase{ + "linux": { + filename: "standard_config_linux", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "darwin": { + filename: "standard_config_linux", + targetPlatform: "darwin", + expectedEnvVars: nil, + appendString: "", + }, + "windows": { + filename: "standard_config_windows", + targetPlatform: "windows", + expectedEnvVars: nil, + appendString: "", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + t.Setenv(envconfig.IMDS_NUMBER_RETRY, "0") + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } } func TestAdvancedConfig(t *testing.T) { - resetContext(t) - expectedEnvVars := map[string]string{} - checkTranslation(t, "advanced_config_linux", "linux", expectedEnvVars, "") - checkTranslation(t, "advanced_config_darwin", "darwin", nil, "") - checkTranslation(t, "advanced_config_windows", "windows", expectedEnvVars, "") + testCases := map[string]testCase{ + "linux": { + filename: "advanced_config_linux", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "darwin": { + filename: "advanced_config_darwin", + targetPlatform: "darwin", + expectedEnvVars: nil, + appendString: "", + }, + "windows": { + filename: "advanced_config_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } } func TestLogOnlyConfig(t *testing.T) { @@ -179,13 +270,105 @@ func TestLogOnlyConfig(t *testing.T) { checkTranslation(t, "log_only_config_windows", "windows", expectedEnvVars, "") } -func TestTraceConfig(t *testing.T) { +func TestSkipLogTimestampConfig(t *testing.T) { + testCases := map[string]testCase{ + "default_linux": { + filename: "skip_log_timestamp_default", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "default_darwin": { + filename: "skip_log_timestamp_default", + targetPlatform: "darwin", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "default_windows": { + filename: "skip_log_timestamp_default_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "set_linux": { + filename: "skip_log_timestamp", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "set_darwin": { + filename: "skip_log_timestamp", + targetPlatform: "darwin", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "set_windows": { + filename: "skip_log_timestamp_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "no_skip_linux": { + filename: "no_skip_log_timestamp", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "no_skip_darwin": { + filename: "no_skip_log_timestamp", + targetPlatform: "darwin", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + "no_skip_windows": { + filename: "no_skip_log_timestamp_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } +} + +func TestDoNotSkipLogDefaultTimestampConfig(t *testing.T) { resetContext(t) - readCommonConfig(t, "./sampleConfig/commonConfig/withCredentials.toml") expectedEnvVars := map[string]string{} - checkTranslation(t, "trace_config", "linux", expectedEnvVars, "_linux") - checkTranslation(t, "trace_config", "darwin", expectedEnvVars, "_linux") - checkTranslation(t, "trace_config", "windows", expectedEnvVars, "_windows") + checkTranslation(t, "log_only_config_windows", "windows", expectedEnvVars, "") +} + +func TestTraceConfig(t *testing.T) { + testCases := map[string]testCase{ + "linux": { + filename: "trace_config", + targetPlatform: "linux", + expectedEnvVars: map[string]string{}, + appendString: "_linux", + }, + "darwin": { + filename: "trace_config", + targetPlatform: "darwin", + expectedEnvVars: map[string]string{}, + appendString: "_linux", + }, + "windows": { + filename: "trace_config", + targetPlatform: "windows", + expectedEnvVars: map[string]string{}, + appendString: "_windows", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + readCommonConfig(t, "./sampleConfig/commonConfig/withCredentials.toml") + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) + } } func TestConfigWithEnvironmentVariables(t *testing.T) { @@ -195,17 +378,43 @@ func TestConfigWithEnvironmentVariables(t *testing.T) { } func TestStandardConfigWithCommonConfig(t *testing.T) { - resetContext(t) - readCommonConfig(t, "./sampleConfig/commonConfig/withCredentialsProxySsl.toml") - expectedEnvVars := map[string]string{ - "AWS_CA_BUNDLE": "/etc/test/ca_bundle.pem", - "HTTPS_PROXY": "https://127.0.0.1:3280", - "HTTP_PROXY": "http://127.0.0.1:3280", - "NO_PROXY": "254.1.1.1", + testCases := map[string]testCase{ + "linux": { + filename: "standard_config_linux", + targetPlatform: "linux", + expectedEnvVars: map[string]string{ + "AWS_CA_BUNDLE": "/etc/test/ca_bundle.pem", + "HTTPS_PROXY": "https://127.0.0.1:3280", + "HTTP_PROXY": "http://127.0.0.1:3280", + "NO_PROXY": "254.1.1.1", + }, + appendString: "_with_common_config", + }, + "darwin": { + filename: "standard_config_linux", + targetPlatform: "darwin", + expectedEnvVars: nil, + appendString: "_with_common_config", + }, + "windows": { + filename: "standard_config_windows", + targetPlatform: "windows", + expectedEnvVars: map[string]string{ + "AWS_CA_BUNDLE": "/etc/test/ca_bundle.pem", + "HTTPS_PROXY": "https://127.0.0.1:3280", + "HTTP_PROXY": "http://127.0.0.1:3280", + "NO_PROXY": "254.1.1.1", + }, + appendString: "_with_common_config", + }, + } + for name, testCase := range testCases { + t.Run(name, func(t *testing.T) { + resetContext(t) + readCommonConfig(t, "./sampleConfig/commonConfig/withCredentialsProxySsl.toml") + checkTranslation(t, testCase.filename, testCase.targetPlatform, testCase.expectedEnvVars, testCase.appendString) + }) } - checkTranslation(t, "standard_config_linux", "linux", expectedEnvVars, "_with_common_config") - checkTranslation(t, "standard_config_linux", "darwin", nil, "_with_common_config") - checkTranslation(t, "standard_config_windows", "windows", expectedEnvVars, "_with_common_config") } func TestDeltaNetConfigLinux(t *testing.T) { diff --git a/translator/translate/agent/ruleLogFile.go b/translator/translate/agent/ruleLogFile.go index e2988893a7..1eff380c8a 100644 --- a/translator/translate/agent/ruleLogFile.go +++ b/translator/translate/agent/ruleLogFile.go @@ -19,6 +19,7 @@ type Logfile struct { func (l *Logfile) ApplyRule(input interface{}) (returnKey string, returnVal interface{}) { returnKey, returnVal = translator.DefaultCase("logfile", GetDefaultValue(), input) + context.CurrentContext().SetAgentLogFile(returnVal.(string)) return } diff --git a/translator/translate/logs/logs_collected/files/collect_list/ruleTimestampFormat.go b/translator/translate/logs/logs_collected/files/collect_list/ruleTimestampFormat.go index 65af348631..05b8ae318e 100644 --- a/translator/translate/logs/logs_collected/files/collect_list/ruleTimestampFormat.go +++ b/translator/translate/logs/logs_collected/files/collect_list/ruleTimestampFormat.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/aws/amazon-cloudwatch-agent/translator" + "github.com/aws/amazon-cloudwatch-agent/translator/context" ) /* @@ -138,13 +139,17 @@ func checkAndReplace(input string, timestampFormatMap map[string]string) string type TimestampRegax struct { } +// ApplyRule add timestamp regex +// do not add timestamp check when viewing cwa logfile func (t *TimestampRegax) ApplyRule(input interface{}) (returnKey string, returnVal interface{}) { //Convert the input string into []rune and iterate the map and build the output []rune m := input.(map[string]interface{}) //If user not specify the timestamp_format, then no config entry for "timestamp_layout" in TOML if val, ok := m["timestamp_format"]; !ok { - returnKey = "" - returnVal = "" + return "", "" + } else if m["file_path"] == context.CurrentContext().GetAgentLogFile() { + fmt.Printf("timestamp_format set file_path : %s is the same as agent log file %s thus do not use timestamp_regex \n", m["file_path"], context.CurrentContext().GetAgentLogFile()) + return "", "" } else { //If user provide with the specific timestamp_format, use the one that user provide res := checkAndReplace(val.(string), TimeFormatRegexEscapeMap) @@ -170,13 +175,17 @@ func (t *TimestampRegax) ApplyRule(input interface{}) (returnKey string, returnV type TimestampLayout struct { } +// ApplyRule add timestamp layout +// do not add timestamp check when viewing cwa logfile func (t *TimestampLayout) ApplyRule(input interface{}) (returnKey string, returnVal interface{}) { //Convert the input string into []rune and iterate the map and build the output []rune m := input.(map[string]interface{}) //If user not specify the timestamp_format, then no config entry for "timestamp_layout" in TOML if val, ok := m["timestamp_format"]; !ok { - returnKey = "" - returnVal = "" + return "", "" + } else if m["file_path"] == context.CurrentContext().GetAgentLogFile() { + fmt.Printf("timestamp_format set file_path : %s is the same as agent log file %s thus do not use timestamp_layout \n", m["file_path"], context.CurrentContext().GetAgentLogFile()) + return "", "" } else { res := checkAndReplace(val.(string), TimeFormatMap) //If user provide with the specific timestamp_format, use the one that user provide