Skip to content

Commit

Permalink
2 supportability metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahramadan committed Oct 24, 2023
1 parent 944d421 commit 5546674
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
22 changes: 19 additions & 3 deletions lib/new_relic/control/security_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class SecurityInterface

attr_accessor :wait

SUPPORTABILITY_METRIC = 'Supportability/Ruby/SecurityAgent/Enabled/'
SUPPORTABILITY_PREFIX_SECURITY = 'Supportability/Ruby/SecurityAgent/Enabled/'
SUPPORTABILITY_PREFIX_SECURITY_AGENT = 'Supportability/Ruby/SecurityAgent/Agent/Enabled/'
ENABLED = 'enabled'
DISABLED = 'disabled'

def agent_started?
(@agent_started ||= false) == true
Expand All @@ -24,21 +27,34 @@ def waiting?
def init_agent
return if agent_started? || waiting?

record_supportability_metrics

if Agent.config[:'security.agent.enabled'] && Agent.config[:'security.enabled'] && !Agent.config[:high_security]
Agent.logger.info('Invoking New Relic security module')
NewRelic::Agent.record_metric_once(SUPPORTABILITY_METRIC + 'enabled')
require 'newrelic_security'

@agent_started = true
else
Agent.logger.info('New Relic Security is completely disabled by one of the user provided config `security.agent.enabled`, `security.enabled`, or `high_security`. Not loading security capabilities.')
NewRelic::Agent.record_metric_once(SUPPORTABILITY_METRIC + 'disabled')
end
rescue LoadError
Agent.logger.info('New Relic security agent not found - skipping')
rescue StandardError => exception
Agent.logger.error("Exception in New Relic security module loading: #{exception} #{exception.backtrace}")
end

def record_supportability_metrics
Agent.config[:'security.enabled'] ? security_metric(ENABLED) : security_metric(DISABLED)
Agent.config[:'security.agent.enabled'] ? security_agent_metric(ENABLED) : security_agent_metric(DISABLED)
end

def security_metric(setting)
NewRelic::Agent.record_metric_once(SUPPORTABILITY_PREFIX_SECURITY + setting)
end

def security_agent_metric(setting)
NewRelic::Agent.record_metric_once(SUPPORTABILITY_PREFIX_SECURITY_AGENT + setting)
end
end
end
end
16 changes: 12 additions & 4 deletions test/new_relic/control/security_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_initialization_short_circuits_when_the_security_agent_is_disabled
end

refute_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/disabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/disabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end
logger.verify
end
Expand All @@ -38,6 +39,7 @@ def test_initialization_short_circuits_when_the_security_is_disabled
end

refute_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/disabled'
end
logger.verify
Expand All @@ -53,7 +55,8 @@ def test_initialization_short_circuits_when_high_security_mode_is_enabled
end

refute_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/disabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end
logger.verify
end
Expand All @@ -68,7 +71,8 @@ def test_initialization_short_circuits_if_the_agent_has_already_been_started
end

refute reached, 'Expected init_agent to short circuit but it reached code within the method instead!'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/disabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end

def test_initialization_short_circuits_if_the_agent_has_been_told_to_wait
Expand All @@ -81,7 +85,8 @@ def test_initialization_short_circuits_if_the_agent_has_been_told_to_wait
end

refute reached, 'Expected init_agent to short circuit but it reached code within the method instead!'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/disabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end

def test_initialization_requires_the_security_agent
Expand All @@ -102,6 +107,7 @@ def test_initialization_requires_the_security_agent

assert required, 'Expected init_agent to perform a require statement'
assert_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end

Expand All @@ -122,6 +128,7 @@ def test_initialization_anticipates_a_load_error
logger.verify

refute_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end
end
Expand All @@ -144,6 +151,7 @@ def test_initialization_handles_errors
logger.verify

refute_predicate NewRelic::Control::SecurityInterface.instance, :agent_started?
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Agent/Enabled/enabled'
assert_metrics_recorded 'Supportability/Ruby/SecurityAgent/Enabled/enabled'
end
end

0 comments on commit 5546674

Please sign in to comment.