backup_metrics_wiring.go RawHistoryFolder
1package app
2
3import (
4 "context"
5 "time"
6
7 "github.com/bnema/zerowrap"
8
9 "github.com/bnema/gordon/internal/adapters/out/telemetry"
10 "github.com/bnema/gordon/internal/domain"
11 cronSvc "github.com/bnema/gordon/internal/usecase/cron"
12)
13
14// nextRunObserver exports the next planned run of one backup kind.
15type nextRunObserver interface {
16 ObserveBackupNextRun(kind domain.BackupKind, next func() time.Time)
17}
18
19// nextRunObserverPort converts a possibly nil metrics adapter into an
20// interface that is nil when metrics are disabled. A typed nil must not
21// leak into the interface.
22func nextRunObserverPort(metrics *telemetry.Metrics) nextRunObserver {
23 if metrics == nil {
24 return nil
25 }
26 return metrics
27}
28
29// wireBackupMetrics injects backup metrics into the backup services. It
30// must run before any backup can start, so no run reads the field while
31// it is written.
32func wireBackupMetrics(svc *services) {
33 if svc == nil || svc.metrics == nil {
34 return
35 }
36 if svc.backupSvc != nil {
37 svc.backupSvc.WithMetrics(svc.metrics)
38 }
39 if svc.volumeBackupSvc != nil {
40 svc.volumeBackupSvc.WithMetrics(svc.metrics)
41 }
42}
43
44// seedBackupMetrics reports the last success of every target from
45// storage, so the metric is correct right after a restart. It only feeds
46// the metrics state (mutex-guarded), so it may run alongside backups.
47// Failures are logged, never fatal.
48func seedBackupMetrics(ctx context.Context, svc *services, log zerowrap.Logger) {
49 if svc == nil || svc.metrics == nil {
50 return
51 }
52 if svc.backupSvc != nil {
53 if err := svc.backupSvc.SeedMetrics(ctx); err != nil {
54 log.Warn().Err(err).Msg("failed to seed database backup metrics")
55 }
56 }
57 if svc.volumeBackupSvc != nil {
58 if err := svc.volumeBackupSvc.SeedMetrics(ctx); err != nil {
59 log.Warn().Err(err).Msg("failed to seed volume backup metrics")
60 }
61 }
62}
63
64// observeSchedulerNextRun exports the scheduler entry's next run, read at
65// each metrics collection.
66func observeSchedulerNextRun(obs nextRunObserver, kind domain.BackupKind, scheduler *cronSvc.Scheduler, id string) {
67 if obs == nil || scheduler == nil {
68 return
69 }
70 obs.ObserveBackupNextRun(kind, func() time.Time { return nextRunOf(scheduler, id) })
71}