Skip to content

Commit

Permalink
Avoid streaming of ProviderTagSelector evaluation errors (#17)
Browse files Browse the repository at this point in the history
* Fixed typing issue

* Avoid streaming of ProviderTagSelector evaluation errors

* Version 1.0.4
  • Loading branch information
marc-perreaut authored Jun 8, 2023
1 parent 2c11924 commit 54c03ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
34 changes: 28 additions & 6 deletions cloud_cost_allocation/cost_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,12 @@ def compute_provider_tag_selector_amounts(self, amount_to_allocation_key_indexes
for item in self.cost_items:
item.nb_matching_provider_tag_selectors = 0

# Build evaluation error dict, used to avoid error streaming in case of incorrect provider tag
# selector expression
# Key = date, provider service, provider instance, provider tag selector, expression
# Value = count of errors
evaluation_error_dict = {}

# Compute raw costs of non-default provider tag selectors, by checking matching items
total_provider_tag_selector_raw_amounts = [0.0] * nb_amounts
total_provider_tag_selector_raw_product_amounts = [0.0] * nb_amounts
Expand All @@ -584,12 +590,14 @@ def compute_provider_tag_selector_amounts(self, amount_to_allocation_key_indexes
eval_true = eval(provider_tag_selector_amount.selector, eval_globals_dict, {})
except:
exception = sys.exc_info()[0]
error("Caught exception '" + str(exception) +
"' while evaluating provider tag selector '" +
provider_tag_selector_amount.selector +
"' for provider service '" +
self.service +
"'")
error_key =\
cost_item.date_str + chr(10) + self.service + chr(10) + self.instance + chr(10) +\
provider_tag_selector_amount.selector + chr(10) + str(exception)
if error_key in evaluation_error_dict:
error_count = evaluation_error_dict[error_key]
else:
error_count = 0
evaluation_error_dict[error_key] = error_count + 1

# If item matches provider tag selector, update raw amounts of the provider tag selector
if eval_true:
Expand All @@ -600,6 +608,20 @@ def compute_provider_tag_selector_amounts(self, amount_to_allocation_key_indexes
total_provider_tag_selector_raw_product_amounts,
amount_to_allocation_key_indexes)

# Log evaluation errors
for error_key, error_count in evaluation_error_dict.items():
error_fields = error_key.split(chr(10))
date_str = error_fields[0]
provider_service = error_fields[1]
provider_instance = error_fields[2]
provider_tag_selector = error_fields[3]
exception = error_fields[4]
error("Caught exception '" + exception + "' " + str(error_count) + " times " +
"when evaluating ProviderTagSelector '" + provider_tag_selector +
"' of ProviderService '" + provider_service +
"' and of ProviderInstance '" + provider_instance + "'" +
", for date " + date_str)

# Compute the raw amounts of the default provider tag selector
if '' in self.provider_tag_selector_amounts:
default_provider_tag_selector = self.provider_tag_selector_amounts['']
Expand Down
4 changes: 2 additions & 2 deletions cloud_cost_allocation/writer/base_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class GenericWriter(ABC):

__slots__ = (
'config', # type: Config
'service_instances', # type: ServiceInstance
'service_instances', # type: dict[ServiceInstance]
'exporters', # type: dict[type -> method]
)

def __init__(self, service_instances: list[ServiceInstance], config: Config):
def __init__(self, service_instances: dict[ServiceInstance], config: Config):
'''
Constructor
'''
Expand Down
2 changes: 1 addition & 1 deletion cloud_cost_allocation/writer/csv_allocated_cost_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CSV_AllocatedCostWriter(GenericWriter):
classdocs
'''

def __init__(self, service_instances: list[ServiceInstance], config: Config):
def __init__(self, service_instances: dict[ServiceInstance], config: Config):
super().__init__(service_instances, config)

def get_headers(self) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Setup
setup(
name='cloud-cost-allocation',
version='1.0.3',
version='1.0.4',
description='Python library for shared, hierarchical cost allocation based on user-defined usage metrics.',
long_description=readme,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 54c03ab

Please sign in to comment.