diff --git a/swh/web/common/origin_save.py b/swh/web/common/origin_save.py --- a/swh/web/common/origin_save.py +++ b/swh/web/common/origin_save.py @@ -622,9 +622,25 @@ ) +# Cumulative metric on the duration of save code now request per status and visit_type +ACCEPTED_SAVE_REQUESTS_DURATION_METRIC = "swh_web_save_requests_duration_seconds" + +_accepted_save_requests_gauge = Gauge( + name=ACCEPTED_SAVE_REQUESTS_DURATION_METRIC, + documentation="Save Requests Duration", + labelnames=["load_task_status", "visit_type"], + registry=SWH_WEB_METRICS_REGISTRY, +) + + def compute_save_requests_metrics() -> None: - """Compute a couple of Prometheus metrics related to - origin save requests""" + """Compute Prometheus metrics related to origin save requests: + + - Number of submitted origin save requests + - Number of accepted origin save requests + - Save Code Now requests duration + + """ request_statuses = ( SAVE_REQUEST_ACCEPTED, @@ -653,6 +669,14 @@ for labels in labels_set: _accepted_save_requests_gauge.labels(*labels).set(0) + duration_load_task_statuses = ( + SAVE_TASK_FAILED, + SAVE_TASK_SUCCEEDED, + ) + + for labels in product(duration_load_task_statuses, visit_types): + _accepted_save_requests_gauge.labels(*labels).set(0) + for sor in SaveOriginRequest.objects.all(): if sor.status == SAVE_REQUEST_ACCEPTED: _accepted_save_requests_gauge.labels( @@ -662,3 +686,11 @@ _submitted_save_requests_gauge.labels( status=sor.status, visit_type=sor.visit_type ).inc() + + if sor.loading_task_status in (SAVE_TASK_SUCCEEDED, SAVE_TASK_FAILED): + assert sor.visit_date is not None + assert sor.request_date is not None + duration = sor.visit_date.timestamp() - sor.request_date.timestamp() + _accepted_save_requests_gauge.labels( + load_task_status=sor.loading_task_status, visit_type=sor.visit_type + ).inc(duration)