From 04cfbad47f467bf7e0f67864063659e6513fad4c Mon Sep 17 00:00:00 2001 From: Saket Chaudhary Date: Thu, 10 Oct 2024 01:24:57 +0530 Subject: [PATCH 1/3] Extension checks --- checks/startup_check.go | 19 ++++++++++++------- config/config.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/checks/startup_check.go b/checks/startup_check.go index 0e94e66..ccd96a1 100644 --- a/checks/startup_check.go +++ b/checks/startup_check.go @@ -18,11 +18,11 @@ type LogSender interface { } /// Register checks here -var checks = []checkFn{ - agentVersionCheck, - handlerCheck, - sanityCheck, - vendorCheck, +var checks = map[string]checkFn{ + "agent": agentVersionCheck, + "handler": handlerCheck, + "sanity": sanityCheck, + "vendor": vendorCheck, } func RunChecks(ctx context.Context, conf *config.Configuration, reg *api.RegistrationResponse, logSender LogSender) { @@ -32,8 +32,13 @@ func RunChecks(ctx context.Context, conf *config.Configuration, reg *api.Registr util.Logln(errLog) } - for _, check := range checks { - runCheck(ctx, conf, reg, runtimeConfig, logSender, check) + runAllChecks := len(conf.ExtensionChecks) == 0 + + for checkName, check := range checks { + if runAllChecks || conf.ExtensionChecks[checkName] { + util.Debugf("Running Extension check: %s", checkName) + runCheck(ctx, conf, reg, runtimeConfig, logSender, check) + } } } diff --git a/config/config.go b/config/config.go index b02eec0..f7f1f66 100644 --- a/config/config.go +++ b/config/config.go @@ -21,6 +21,7 @@ var EmptyNRWrapper = "Undefined" type Configuration struct { TestingOverride bool // ignores envioronment specific details when running unit tests ExtensionEnabled bool + ExtensionChecks map[string]bool LogsEnabled bool SendFunctionLogs bool CollectTraceID bool @@ -40,6 +41,7 @@ type Configuration struct { func ConfigurationFromEnvironment() *Configuration { nrEnabledStr, nrEnabledOverride := os.LookupEnv("NEW_RELIC_ENABLED") nrEnabledRubyStr, nrEnabledRubyOverride := os.LookupEnv("NEW_RELIC_AGENT_ENABLED") + nrExtensionChecksStr, nrExtensionChecksOverride := os.LookupEnv("NEW_RELIC_EXTENSION_CHECKS") enabledStr, extensionEnabledOverride := os.LookupEnv("NEW_RELIC_LAMBDA_EXTENSION_ENABLED") licenseKey, lkOverride := os.LookupEnv("NEW_RELIC_LICENSE_KEY") licenseKeySecretId, lkSecretOverride := os.LookupEnv("NEW_RELIC_LICENSE_KEY_SECRET") @@ -93,6 +95,28 @@ func ConfigurationFromEnvironment() *Configuration { ret.LicenseKeySSMParameterName = licenseKeySSMParameterName } + if nrExtensionChecksOverride && nrExtensionChecksStr != "" { + validChecks := map[string]bool{ + "agent": true, + "handler": true, + "sanity": true, + "vendor": true, + } + selectedChecks := make(map[string]bool) + checks := strings.Split(nrExtensionChecksStr, ",") + + for _, check := range checks { + trimmedCheck := strings.TrimSpace(check) + if trimmedCheck != "" && validChecks[trimmedCheck] { + selectedChecks[trimmedCheck] = true + } + } + + if len(selectedChecks) > 0 { + ret.ExtensionChecks = selectedChecks + } + } + if nrOverride { ret.NRHandler = nrHandler } else { From 9e0c2738a5392485ce9320ff72208e4fcdcafd77 Mon Sep 17 00:00:00 2001 From: Saket Chaudhary Date: Thu, 10 Oct 2024 22:20:23 +0530 Subject: [PATCH 2/3] modify env var to ignore extension checks --- checks/startup_check.go | 8 ++--- checks/startup_check_test.go | 22 ++++++++++++++ config/config.go | 58 ++++++++++++++++++++++-------------- config/config_test.go | 54 +++++++++++++++++++++++++++++++++ main.go | 4 +++ 5 files changed, 118 insertions(+), 28 deletions(-) diff --git a/checks/startup_check.go b/checks/startup_check.go index ccd96a1..6c08dec 100644 --- a/checks/startup_check.go +++ b/checks/startup_check.go @@ -32,13 +32,11 @@ func RunChecks(ctx context.Context, conf *config.Configuration, reg *api.Registr util.Logln(errLog) } - runAllChecks := len(conf.ExtensionChecks) == 0 - for checkName, check := range checks { - if runAllChecks || conf.ExtensionChecks[checkName] { - util.Debugf("Running Extension check: %s", checkName) - runCheck(ctx, conf, reg, runtimeConfig, logSender, check) + if conf.IgnoreExtensionChecks[checkName] { + continue } + runCheck(ctx, conf, reg, runtimeConfig, logSender, check) } } diff --git a/checks/startup_check_test.go b/checks/startup_check_test.go index 8336e86..cfeb7c9 100644 --- a/checks/startup_check_test.go +++ b/checks/startup_check_test.go @@ -78,3 +78,25 @@ func TestRunChecks(t *testing.T) { ctx := context.Background() RunChecks(ctx, c, r, l) } + +func TestRunChecksIgnoreExtensionChecks(t *testing.T) { + c := &config.Configuration{IgnoreExtensionChecks: map[string]bool{"agent": true}} + r := &api.RegistrationResponse{} + l := &TestLogSender{} + + client = &mockClientError{} + + ctx := context.Background() + RunChecks(ctx, c, r, l) +} + +func TestRunChecksIgnoreExtensionChecksAll(t *testing.T) { + c := &config.Configuration{IgnoreExtensionChecks: map[string]bool{"all": true}} + r := &api.RegistrationResponse{} + l := &TestLogSender{} + + client = &mockClientError{} + + ctx := context.Background() + RunChecks(ctx, c, r, l) +} diff --git a/config/config.go b/config/config.go index f7f1f66..a0b4f9e 100644 --- a/config/config.go +++ b/config/config.go @@ -21,7 +21,7 @@ var EmptyNRWrapper = "Undefined" type Configuration struct { TestingOverride bool // ignores envioronment specific details when running unit tests ExtensionEnabled bool - ExtensionChecks map[string]bool + IgnoreExtensionChecks map[string]bool LogsEnabled bool SendFunctionLogs bool CollectTraceID bool @@ -38,10 +38,42 @@ type Configuration struct { ClientTimeout time.Duration } +func parseIgnoredExtensionChecks(nrIgnoreExtensionChecksOverride bool, nrIgnoreExtensionChecksStr string) map[string]bool { + ignoredChecks := make(map[string]bool) + + if !nrIgnoreExtensionChecksOverride || nrIgnoreExtensionChecksStr == "" { + return ignoredChecks + } + + validChecks := map[string]bool{ + "agent": true, + "handler": true, + "sanity": true, + "vendor": true, + } + + ignoredChecksStr := strings.ToLower(nrIgnoreExtensionChecksStr) + + if ignoredChecksStr == "all" { + ignoredChecks["all"] = true + return ignoredChecks + } + + checks := strings.Split(ignoredChecksStr, ",") + for _, check := range checks { + trimmedCheck := strings.TrimSpace(check) + if trimmedCheck != "" && validChecks[trimmedCheck] { + ignoredChecks[trimmedCheck] = true + } + } + + return ignoredChecks +} + func ConfigurationFromEnvironment() *Configuration { nrEnabledStr, nrEnabledOverride := os.LookupEnv("NEW_RELIC_ENABLED") nrEnabledRubyStr, nrEnabledRubyOverride := os.LookupEnv("NEW_RELIC_AGENT_ENABLED") - nrExtensionChecksStr, nrExtensionChecksOverride := os.LookupEnv("NEW_RELIC_EXTENSION_CHECKS") + nrIgnoreExtensionChecksStr, nrIgnoreExtensionChecksOverride := os.LookupEnv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") enabledStr, extensionEnabledOverride := os.LookupEnv("NEW_RELIC_LAMBDA_EXTENSION_ENABLED") licenseKey, lkOverride := os.LookupEnv("NEW_RELIC_LICENSE_KEY") licenseKeySecretId, lkSecretOverride := os.LookupEnv("NEW_RELIC_LICENSE_KEY_SECRET") @@ -95,27 +127,7 @@ func ConfigurationFromEnvironment() *Configuration { ret.LicenseKeySSMParameterName = licenseKeySSMParameterName } - if nrExtensionChecksOverride && nrExtensionChecksStr != "" { - validChecks := map[string]bool{ - "agent": true, - "handler": true, - "sanity": true, - "vendor": true, - } - selectedChecks := make(map[string]bool) - checks := strings.Split(nrExtensionChecksStr, ",") - - for _, check := range checks { - trimmedCheck := strings.TrimSpace(check) - if trimmedCheck != "" && validChecks[trimmedCheck] { - selectedChecks[trimmedCheck] = true - } - } - - if len(selectedChecks) > 0 { - ret.ExtensionChecks = selectedChecks - } - } + ret.IgnoreExtensionChecks = parseIgnoredExtensionChecks(nrIgnoreExtensionChecksOverride, nrIgnoreExtensionChecksStr) if nrOverride { ret.NRHandler = nrHandler diff --git a/config/config_test.go b/config/config_test.go index 5b8ea33..baba6ac 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -99,6 +99,60 @@ func TestConfigurationFromEnvironmentNRAgentEnabled(t *testing.T) { assert.Equal(t, conf.ExtensionEnabled, false) } +func TestConfigurationFromEnvironmentExtensionChecks(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "agent,handler,dummy") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, conf.IgnoreExtensionChecks["agent"], true) + assert.Equal(t, conf.IgnoreExtensionChecks["handler"], true) + assert.Equal(t, len(conf.IgnoreExtensionChecks), 2) +} + +func TestConfigurationFromEnvironmentExtensionChecksAll(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "ALL") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, conf.IgnoreExtensionChecks["agent"], false) + assert.Equal(t, conf.IgnoreExtensionChecks["handler"], false) + assert.Equal(t, conf.IgnoreExtensionChecks["sanity"], false) + assert.Equal(t, conf.IgnoreExtensionChecks["vendor"], false) + assert.Equal(t, len(conf.IgnoreExtensionChecks), 1) +} + +func TestConfigurationFromEnvironmentExtensionChecksIncorrectString(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "incorrect,valuess,...,,") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, len(conf.IgnoreExtensionChecks), 0) +} + +func TestConfigurationFromEnvironmentExtensionChecksIncorrectStringWithAll(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "incorrect,valuess,...,ALL,") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, len(conf.IgnoreExtensionChecks), 0) +} + +func TestConfigurationFromEnvironmentExtensionChecksIncorrectStringAll(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "All,ALL,...,ALL,") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, len(conf.IgnoreExtensionChecks), 0) +} + +func TestConfigurationFromEnvironmentExtensionChecksEmptyString(t *testing.T) { + os.Setenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS", "") + defer os.Unsetenv("NEW_RELIC_IGNORE_EXTENSION_CHECKS") + + conf := ConfigurationFromEnvironment() + assert.Equal(t, len(conf.IgnoreExtensionChecks), 0) +} + func TestConfigurationFromEnvironmentSecretId(t *testing.T) { os.Setenv("NEW_RELIC_LICENSE_KEY_SECRET", "secretId") defer os.Unsetenv("NEW_RELIC_LICENSE_KEY_SECRET") diff --git a/main.go b/main.go index cedfb23..e57797a 100644 --- a/main.go +++ b/main.go @@ -137,6 +137,10 @@ func main() { // Run startup checks go func() { + if conf.IgnoreExtensionChecks["all"] { + util.Debugf("Ignoring all extension checks") + return + } checks.RunChecks(ctx, conf, registrationResponse, telemetryClient) }() From 78d8d03a9273f6df6d16265fc0e24c7948110545 Mon Sep 17 00:00:00 2001 From: Saket Chaudhary Date: Thu, 10 Oct 2024 22:25:51 +0530 Subject: [PATCH 3/3] modify default val to nil --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index a0b4f9e..146d9ec 100644 --- a/config/config.go +++ b/config/config.go @@ -42,7 +42,7 @@ func parseIgnoredExtensionChecks(nrIgnoreExtensionChecksOverride bool, nrIgnoreE ignoredChecks := make(map[string]bool) if !nrIgnoreExtensionChecksOverride || nrIgnoreExtensionChecksStr == "" { - return ignoredChecks + return nil } validChecks := map[string]bool{