diff --git a/src/newrelic_logging/limits/receiver.py b/src/newrelic_logging/limits/receiver.py index 7c40f1e..9bdb382 100644 --- a/src/newrelic_logging/limits/receiver.py +++ b/src/newrelic_logging/limits/receiver.py @@ -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]: @@ -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 @@ -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(), } diff --git a/src/tests/test_limits_receiver.py b/src/tests/test_limits_receiver.py index 19d2ed5..f3fef0a 100644 --- a/src/tests/test_limits_receiver.py +++ b/src/tests/test_limits_receiver.py @@ -130,7 +130,8 @@ 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 @@ -138,6 +139,7 @@ def test_build_attributes_returns_name_only_given_no_max_or_remaining(self): ''' # setup + config = mod_config.Config({}) limits = { 'Foo': { 'Max': 50, 'Remaining': 3 }, 'Bar': { 'Max': 300, 'Remaining': 10 }, @@ -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) @@ -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 @@ -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 }, @@ -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) @@ -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 @@ -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 }, @@ -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) @@ -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 @@ -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 }, @@ -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) @@ -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): '''