diff --git a/jobs/cache_maintenance/src/cache_maintenance/cache_metrics.py b/jobs/cache_maintenance/src/cache_maintenance/cache_metrics.py index dbe015d4b..37e45f534 100644 --- a/jobs/cache_maintenance/src/cache_maintenance/cache_metrics.py +++ b/jobs/cache_maintenance/src/cache_maintenance/cache_metrics.py @@ -31,9 +31,12 @@ def collect_cache_metrics() -> None: logging.info(f"{kind=} {http_status=} {error_code=} has been deleted") for (kind, http_status, error_code), total in new_metric_by_id.items(): - CacheTotalMetricDocument.objects(kind=kind, http_status=http_status, error_code=error_code).upsert_one( - total=total - ) + #add a validation to force total to be non-negative + if total < 0: + logging.warning(f"Negative metric value for {kind=} {http_status=} {error_code=}: {total=}. Setting to 0.") + total = 0 + + CacheTotalMetricDocument.objects(kind=kind, http_status=http_status, error_code=error_code).upsert_one(total=total) logging.info(f"{kind=} {http_status=} {error_code=}: {total=} has been inserted") logging.info("cache metrics have been updated")