Skip to content

Commit

Permalink
feat: add EVENT_TYPE to limits with default and event_type option to …
Browse files Browse the repository at this point in the history
…customize
  • Loading branch information
sdewitt-newrelic committed May 15, 2024
1 parent 2b3af04 commit 390e7e4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/newrelic_logging/limits/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_limit_names(config: Config, limits: dict) -> list[str]:
return list(limits)


def build_attributes(limits: dict, key) -> dict:
def build_attributes(config: Config, limits: dict, key) -> dict:
attributes = { 'name': key }

if 'Max' in limits[key]:
Expand All @@ -32,6 +32,8 @@ def build_attributes(limits: dict, key) -> dict:
if 'Remaining' in limits[key]:
attributes['Remaining'] = int(limits[key]['Remaining'])

attributes['EVENT_TYPE'] = config.get('event_type', 'SalesforceOrgLimit')

return attributes


Expand All @@ -47,7 +49,7 @@ def transform_limits(

yield {
'message': f'Salesforce Org Limit: {limit_name}',
'attributes': build_attributes(limits, limit_name),
'attributes': build_attributes(config, limits, limit_name),
'timestamp': get_timestamp(),
}

Expand Down
84 changes: 76 additions & 8 deletions src/tests/test_limits_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ def test_get_limit_names_returns_limit_names_when_names_not_in_limits_options(se
def test_build_attributes_returns_name_only_given_no_max_or_remaining(self):
'''
build_attributes() returns a dict containing only a name attribute when there are no Max or Remaining attributes in the given limit dict
given: the result of export_limits()
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the limit for the given limit name does not have Max or Remaining attributes
then: return a dict with the limit name only
'''

# setup
config = mod_config.Config({})
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
'Bar': { 'Max': 300, 'Remaining': 10 },
Expand All @@ -146,7 +148,7 @@ def test_build_attributes_returns_name_only_given_no_max_or_remaining(self):
}

# execute
attrs = receiver.build_attributes(limits, 'Boop')
attrs = receiver.build_attributes(config, limits, 'Boop')

# verify
self.assertTrue('name' in attrs)
Expand All @@ -157,7 +159,8 @@ def test_build_attributes_returns_name_only_given_no_max_or_remaining(self):
def test_build_attributes_returns_name_and_max_given_no_remaining(self):
'''
build_attributes() returns a dict containing only name and Max attributes when there is no Remaining attribute in the given limit dict
given: the result of export_limits()
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the limit for the given limit name has a Max attribute
Expand All @@ -166,6 +169,7 @@ def test_build_attributes_returns_name_and_max_given_no_remaining(self):
'''

# setup
config = mod_config.Config({})
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
'Bar': { 'Max': 300, 'Remaining': 10 },
Expand All @@ -174,7 +178,7 @@ def test_build_attributes_returns_name_and_max_given_no_remaining(self):
}

# execute
attrs = receiver.build_attributes(limits, 'Boop')
attrs = receiver.build_attributes(config, limits, 'Boop')

# verify
self.assertTrue('name' in attrs)
Expand All @@ -186,7 +190,8 @@ def test_build_attributes_returns_name_and_max_given_no_remaining(self):
def test_build_attributes_returns_name_and_remaining_given_no_max(self):
'''
build_attributes() returns a dict containing only name and Remaining attributes when there is no Max attribute in the given limit dict
given: the result of export_limits()
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the limit for the given limit name has a Remaining attribute
Expand All @@ -195,6 +200,7 @@ def test_build_attributes_returns_name_and_remaining_given_no_max(self):
'''

# setup
config = mod_config.Config({})
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
'Bar': { 'Max': 300, 'Remaining': 10 },
Expand All @@ -203,7 +209,7 @@ def test_build_attributes_returns_name_and_remaining_given_no_max(self):
}

# execute
attrs = receiver.build_attributes(limits, 'Boop')
attrs = receiver.build_attributes(config, limits, 'Boop')

# verify
self.assertTrue('name' in attrs)
Expand All @@ -215,7 +221,8 @@ def test_build_attributes_returns_name_and_remaining_given_no_max(self):
def test_build_attributes_returns_name_and_max_and_remaining(self):
'''
build_attributes() returns a dict containing the name, Max, and Remaining attributes when both Max and Remaining exist in the given limit dict
given: the result of export_limits()
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the limit for the given limit name has a Remaining attribute
Expand All @@ -224,6 +231,7 @@ def test_build_attributes_returns_name_and_max_and_remaining(self):
'''

# setup
config = mod_config.Config({})
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
'Bar': { 'Max': 300, 'Remaining': 10 },
Expand All @@ -232,7 +240,65 @@ def test_build_attributes_returns_name_and_max_and_remaining(self):
}

# execute
attrs = receiver.build_attributes(limits, 'Foo')
attrs = receiver.build_attributes(config, limits, 'Foo')

# verify
self.assertTrue('name' in attrs)
self.assertEqual(attrs['name'], 'Foo')
self.assertTrue('Max' in attrs)
self.assertEqual(attrs['Max'], 50)
self.assertTrue('Remaining' in attrs)
self.assertEqual(attrs['Remaining'], 3)

def test_build_attributes_returns_default_event_type_given_no_event_type_option(self):
'''
build_attributes() returns a dict containing a limit with the default event type given no event type option is specified in the config
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the event_type option is not set in the limits options
then: return a dict with the default EVENT_TYPE
'''

# setup
config = mod_config.Config({})
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
}

# execute
attrs = receiver.build_attributes(config, limits, 'Foo')

# verify
self.assertTrue('name' in attrs)
self.assertEqual(attrs['name'], 'Foo')
self.assertTrue('Max' in attrs)
self.assertEqual(attrs['Max'], 50)
self.assertTrue('Remaining' in attrs)
self.assertEqual(attrs['Remaining'], 3)
self.assertTrue('EVENT_TYPE' in attrs)
self.assertEqual(attrs['EVENT_TYPE'], 'SalesforceOrgLimit')

def test_build_attributes_returns_custom_event_type_given_event_type_option(self):
'''
build_attributes() returns a dict containing a limit with a custom event type given an event type option is specified in the config
given: a set of limits options from the instance configuration
and given: the result of export_limits()
and given: a limit name
when: build_attributes() is called
and when: the event_type option is set in the limits options
then: return a dict with the custom EVENT_TYPE
'''

# setup
config = mod_config.Config({ 'event_type': 'CustomSFOrgLimit' })
limits = {
'Foo': { 'Max': 50, 'Remaining': 3 },
}

# execute
attrs = receiver.build_attributes(config, limits, 'Foo')

# verify
self.assertTrue('name' in attrs)
Expand All @@ -241,6 +307,8 @@ def test_build_attributes_returns_name_and_max_and_remaining(self):
self.assertEqual(attrs['Max'], 50)
self.assertTrue('Remaining' in attrs)
self.assertEqual(attrs['Remaining'], 3)
self.assertTrue('EVENT_TYPE' in attrs)
self.assertEqual(attrs['EVENT_TYPE'], 'CustomSFOrgLimit')

def test_transform_limits_yields_no_results_given_no_matching_limit_names(self):
'''
Expand Down

0 comments on commit 390e7e4

Please sign in to comment.