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

Run Extension checks conditionally #235

Merged
merged 3 commits into from
Oct 15, 2024
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
15 changes: 9 additions & 6 deletions checks/startup_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,7 +32,10 @@ func RunChecks(ctx context.Context, conf *config.Configuration, reg *api.Registr
util.Logln(errLog)
}

for _, check := range checks {
for checkName, check := range checks {
if conf.IgnoreExtensionChecks[checkName] {
continue
}
runCheck(ctx, conf, reg, runtimeConfig, logSender, check)
}
}
Expand Down
22 changes: 22 additions & 0 deletions checks/startup_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
36 changes: 36 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var EmptyNRWrapper = "Undefined"
type Configuration struct {
TestingOverride bool // ignores envioronment specific details when running unit tests
ExtensionEnabled bool
IgnoreExtensionChecks map[string]bool
LogsEnabled bool
SendFunctionLogs bool
CollectTraceID bool
Expand All @@ -37,9 +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 nil
}

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")
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")
Expand Down Expand Up @@ -93,6 +127,8 @@ func ConfigurationFromEnvironment() *Configuration {
ret.LicenseKeySSMParameterName = licenseKeySSMParameterName
}

ret.IgnoreExtensionChecks = parseIgnoredExtensionChecks(nrIgnoreExtensionChecksOverride, nrIgnoreExtensionChecksStr)

if nrOverride {
ret.NRHandler = nrHandler
} else {
Expand Down
54 changes: 54 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()

Expand Down
Loading