Skip to content

Commit

Permalink
default values
Browse files Browse the repository at this point in the history
  • Loading branch information
scrungus committed Aug 19, 2024
1 parent b6630fe commit e8f207d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions coral_credits/prom_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

LOG = logging.getLogger(__name__)

def provide_default_values(pid, rc, prov, hours):
"""Provide default values for metrics that might be None or empty."""
default_pid = pid if pid else "UnknownProjectID"
default_rc = rc if rc else "UnknownResourceClass"
default_prov = prov if prov else "UnknownProvider"
default_hours = hours if hours is not None else 0
return default_pid, default_rc, default_prov, default_hours

def get_credit_allocation_date(date_type):
try:
Expand Down Expand Up @@ -118,6 +125,7 @@ def collect(self):
labels=["project_id", "resource_class", "provider"],
)
for pid, rc, prov, hours in get_total_hours():
pid, rc, prov, hours = provide_default_values(pid, rc, prov, hours)
coral_credits_allocation_hours_per_project.add_metric(
[pid, rc, prov], hours
)
Expand All @@ -130,6 +138,7 @@ def collect(self):
labels=["project_id", "resource_class", "provider"],
)
for pid, rc, prov, hours in get_free_hours():
pid, rc, prov, hours = provide_default_values(pid, rc, prov, hours)
coral_credits_allocation_hours_free_per_project.add_metric(
[pid, rc, prov], hours
)
Expand All @@ -142,6 +151,7 @@ def collect(self):
labels=["project_id", "resource_class", "provider"],
)
for pid, rc, prov, hours in get_reserved_hours():
pid, rc, prov, hours = provide_default_values(pid, rc, prov, hours)
coral_credits_allocation_hours_reserved_per_project.add_metric(
[pid, rc, prov], hours
)
Expand Down Expand Up @@ -170,6 +180,7 @@ def collect(self):
labels=["project_id", "resource_class", "provider"],
)
for pid, rc, prov, hours in get_credit_allocation_date(date_type="end"):
pid, rc, prov, hours = provide_default_values(pid, rc, prov, hours)
coral_credits_allocation_hours_expires_in_days_per_project.add_metric(
[pid, rc, prov], hours
)
Expand All @@ -182,6 +193,7 @@ def collect(self):
labels=["project_id", "resource_class", "provider"],
)
for pid, rc, prov, hours in get_credit_allocation_date(date_type="start"):
pid, rc, prov, hours = provide_default_values(pid, rc, prov, hours)
coral_credits_allocation_hours_valid_since_days_per_project.add_metric(
[pid, rc, prov], hours
)
Expand Down

0 comments on commit e8f207d

Please sign in to comment.