diff --git a/changelogs/unreleased/1448-fabito b/changelogs/unreleased/1448-fabito new file mode 100644 index 000000000..95fc36729 --- /dev/null +++ b/changelogs/unreleased/1448-fabito @@ -0,0 +1 @@ +Expose the timestamp of the last successful backup in a gauge \ No newline at end of file diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 330783947..542080d85 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -28,26 +28,27 @@ type ServerMetrics struct { } const ( - metricNamespace = "velero" - backupTarballSizeBytesGauge = "backup_tarball_size_bytes" - backupTotal = "backup_total" - backupAttemptTotal = "backup_attempt_total" - backupSuccessTotal = "backup_success_total" - backupPartialFailureTotal = "backup_partial_failure_total" - backupFailureTotal = "backup_failure_total" - backupDurationSeconds = "backup_duration_seconds" - backupDeletionAttemptTotal = "backup_deletion_attempt_total" - backupDeletionSuccessTotal = "backup_deletion_success_total" - backupDeletionFailureTotal = "backup_deletion_failure_total" - restoreTotal = "restore_total" - restoreAttemptTotal = "restore_attempt_total" - restoreValidationFailedTotal = "restore_validation_failed_total" - restoreSuccessTotal = "restore_success_total" - restorePartialFailureTotal = "restore_partial_failure_total" - restoreFailedTotal = "restore_failed_total" - volumeSnapshotAttemptTotal = "volume_snapshot_attempt_total" - volumeSnapshotSuccessTotal = "volume_snapshot_success_total" - volumeSnapshotFailureTotal = "volume_snapshot_failure_total" + metricNamespace = "velero" + backupTarballSizeBytesGauge = "backup_tarball_size_bytes" + backupTotal = "backup_total" + backupAttemptTotal = "backup_attempt_total" + backupSuccessTotal = "backup_success_total" + backupPartialFailureTotal = "backup_partial_failure_total" + backupFailureTotal = "backup_failure_total" + backupDurationSeconds = "backup_duration_seconds" + backupDeletionAttemptTotal = "backup_deletion_attempt_total" + backupDeletionSuccessTotal = "backup_deletion_success_total" + backupDeletionFailureTotal = "backup_deletion_failure_total" + backupLastSuccessfulTimestamp = "backup_last_successful_timestamp" + restoreTotal = "restore_total" + restoreAttemptTotal = "restore_attempt_total" + restoreValidationFailedTotal = "restore_validation_failed_total" + restoreSuccessTotal = "restore_success_total" + restorePartialFailureTotal = "restore_partial_failure_total" + restoreFailedTotal = "restore_failed_total" + volumeSnapshotAttemptTotal = "volume_snapshot_attempt_total" + volumeSnapshotSuccessTotal = "volume_snapshot_success_total" + volumeSnapshotFailureTotal = "volume_snapshot_failure_total" scheduleLabel = "schedule" backupNameLabel = "backupName" @@ -67,6 +68,14 @@ func NewServerMetrics() *ServerMetrics { }, []string{scheduleLabel}, ), + backupLastSuccessfulTimestamp: prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: metricNamespace, + Name: backupLastSuccessfulTimestamp, + Help: "Last time a backup ran successfully, Unix timestamp in seconds", + }, + []string{scheduleLabel}, + ), backupTotal: prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: metricNamespace, @@ -287,6 +296,13 @@ func (m *ServerMetrics) SetBackupTarballSizeBytesGauge(backupSchedule string, si } } +// SetBackupLastSuccessfulTimestamp records the last time a backup ran successfully, Unix timestamp in seconds +func (m *ServerMetrics) SetBackupLastSuccessfulTimestamp(backupSchedule string) { + if g, ok := m.metrics[backupLastSuccessfulTimestamp].(*prometheus.GaugeVec); ok { + g.WithLabelValues(backupSchedule).Set(float64(time.Now().Unix())) + } +} + // SetBackupTotal records the current number of existent backups. func (m *ServerMetrics) SetBackupTotal(numberOfBackups int64) { if g, ok := m.metrics[backupTotal].(prometheus.Gauge); ok { @@ -306,6 +322,7 @@ func (m *ServerMetrics) RegisterBackupSuccess(backupSchedule string) { if c, ok := m.metrics[backupSuccessTotal].(*prometheus.CounterVec); ok { c.WithLabelValues(backupSchedule).Inc() } + m.SetBackupLastSuccessfulTimestamp(backupSchedule) } // RegisterBackupPartialFailure records a partially failed backup.