|
| 1 | +package metric |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + globalRegistry = initRegistry() |
| 13 | +) |
| 14 | + |
| 15 | +func initRegistry() *Registry { |
| 16 | + |
| 17 | + commonDims := []string{"repo_id", "plan_id"} |
| 18 | + |
| 19 | + registry := &Registry{ |
| 20 | + reg: prometheus.NewRegistry(), |
| 21 | + backupBytesProcessed: prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 22 | + Name: "backrest_backup_bytes_processed", |
| 23 | + Help: "The total number of bytes processed during a backup", |
| 24 | + }, commonDims), |
| 25 | + backupBytesAdded: prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 26 | + Name: "backrest_backup_bytes_added", |
| 27 | + Help: "The total number of bytes added during a backup", |
| 28 | + }, commonDims), |
| 29 | + backupFileWarnings: prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 30 | + Name: "backrest_backup_file_warnings", |
| 31 | + Help: "The total number of file warnings during a backup", |
| 32 | + }, commonDims), |
| 33 | + tasksDuration: prometheus.NewSummaryVec(prometheus.SummaryOpts{ |
| 34 | + Name: "backrest_tasks_duration_secs", |
| 35 | + Help: "The duration of a task in seconds", |
| 36 | + }, append(slices.Clone(commonDims), "task_type")), |
| 37 | + tasksRun: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 38 | + Name: "backrest_tasks_run_total", |
| 39 | + Help: "The total number of tasks run", |
| 40 | + }, append(slices.Clone(commonDims), "task_type", "status")), |
| 41 | + } |
| 42 | + |
| 43 | + registry.reg.MustRegister(registry.backupBytesProcessed) |
| 44 | + registry.reg.MustRegister(registry.backupBytesAdded) |
| 45 | + registry.reg.MustRegister(registry.backupFileWarnings) |
| 46 | + registry.reg.MustRegister(registry.tasksDuration) |
| 47 | + registry.reg.MustRegister(registry.tasksRun) |
| 48 | + |
| 49 | + return registry |
| 50 | +} |
| 51 | + |
| 52 | +func GetRegistry() *Registry { |
| 53 | + return globalRegistry |
| 54 | +} |
| 55 | + |
| 56 | +type Registry struct { |
| 57 | + reg *prometheus.Registry |
| 58 | + backupBytesProcessed *prometheus.SummaryVec |
| 59 | + backupBytesAdded *prometheus.SummaryVec |
| 60 | + backupFileWarnings *prometheus.SummaryVec |
| 61 | + tasksDuration *prometheus.SummaryVec |
| 62 | + tasksRun *prometheus.CounterVec |
| 63 | +} |
| 64 | + |
| 65 | +func (r *Registry) Handler() http.Handler { |
| 66 | + return promhttp.HandlerFor(r.reg, promhttp.HandlerOpts{}) |
| 67 | +} |
| 68 | + |
| 69 | +func (r *Registry) RecordTaskRun(repoID, planID, taskType string, duration_secs float64, status string) { |
| 70 | + if repoID == "" { |
| 71 | + repoID = "_unassociated_" |
| 72 | + } |
| 73 | + if planID == "" { |
| 74 | + planID = "_unassociated_" |
| 75 | + } |
| 76 | + r.tasksRun.WithLabelValues(repoID, planID, taskType, status).Inc() |
| 77 | + r.tasksDuration.WithLabelValues(repoID, planID, taskType).Observe(duration_secs) |
| 78 | +} |
| 79 | + |
| 80 | +func (r *Registry) RecordBackupSummary(repoID, planID string, bytesProcessed, bytesAdded int64, fileWarnings int64) { |
| 81 | + r.backupBytesProcessed.WithLabelValues(repoID, planID).Observe(float64(bytesProcessed)) |
| 82 | + r.backupBytesAdded.WithLabelValues(repoID, planID).Observe(float64(bytesAdded)) |
| 83 | + r.backupFileWarnings.WithLabelValues(repoID, planID).Observe(float64(fileWarnings)) |
| 84 | +} |
0 commit comments