From 8bafe7ea35e990377f96662fc81ccdcc34b4dda6 Mon Sep 17 00:00:00 2001 From: Thibault Cohen <47721+titilambert@users.noreply.github.com> Date: Wed, 26 Feb 2025 01:55:22 -0500 Subject: [PATCH] fix: improve exported prometheus metrics for task execution and status (#684) --- internal/metric/metric.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/metric/metric.go b/internal/metric/metric.go index fa9fe0f3..b1f49942 100644 --- a/internal/metric/metric.go +++ b/internal/metric/metric.go @@ -38,6 +38,10 @@ func initRegistry() *Registry { Name: "backrest_tasks_run_total", Help: "The total number of tasks run", }, append(slices.Clone(commonDims), "task_type", "status")), + lastTaskStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "backrest_last_task_status", + Help: "The status of the last task", + }, append(slices.Clone(commonDims), "task_type", "status")), } registry.reg.MustRegister(registry.backupBytesProcessed) @@ -45,6 +49,7 @@ func initRegistry() *Registry { registry.reg.MustRegister(registry.backupFileWarnings) registry.reg.MustRegister(registry.tasksDuration) registry.reg.MustRegister(registry.tasksRun) + registry.reg.MustRegister(registry.lastTaskStatus) return registry } @@ -60,6 +65,7 @@ type Registry struct { backupFileWarnings *prometheus.GaugeVec tasksDuration *prometheus.GaugeVec tasksRun *prometheus.CounterVec + lastTaskStatus *prometheus.GaugeVec } func (r *Registry) Handler() http.Handler { @@ -73,7 +79,14 @@ func (r *Registry) RecordTaskRun(repoID, planID, taskType string, duration_secs if planID == "" { planID = "_unassociated_" } - r.tasksRun.DeletePartialMatch(prometheus.Labels{"repo_id": repoID, "plan_id": planID, "task_type": taskType}) + r.lastTaskStatus.DeletePartialMatch(prometheus.Labels{"repo_id": repoID, "plan_id": planID, "task_type": taskType}) + if status == "success" { + r.lastTaskStatus.WithLabelValues(repoID, planID, taskType, status).Set(0) + } else if status == "failed" { + r.lastTaskStatus.WithLabelValues(repoID, planID, taskType, status).Set(1) + } else { + r.lastTaskStatus.WithLabelValues(repoID, planID, taskType, status).Set(-1) + } r.tasksRun.WithLabelValues(repoID, planID, taskType, status).Inc() r.tasksDuration.WithLabelValues(repoID, planID, taskType).Set(duration_secs) }