restic-scheduler/metrics.go

85 lines
2.0 KiB
Go
Raw Permalink Normal View History

2022-04-13 20:44:48 +00:00
package main
import (
"fmt"
2022-04-13 20:44:48 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
2022-04-13 20:44:48 +00:00
)
type ResticMetrics struct {
JobStartTime *prometheus.GaugeVec
JobFailureCount *prometheus.GaugeVec
SnapshotCurrentCount *prometheus.GaugeVec
SnapshotLatestTime *prometheus.GaugeVec
Registry *prometheus.Registry
}
func (m ResticMetrics) PushToGateway(url string) error {
err := push.New(url, "batch").
Gatherer(m.Registry).
Add()
if err != nil {
return fmt.Errorf("error pushing to registry %s: %w", url, err)
}
return nil
2022-04-13 20:44:48 +00:00
}
func InitMetrics() *ResticMetrics {
labelNames := []string{"job"}
metrics := &ResticMetrics{
Registry: prometheus.NewRegistry(),
2022-04-13 20:44:48 +00:00
JobStartTime: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "restic_job_start_time",
Help: "time that a job was run",
Namespace: "",
Subsystem: "",
ConstLabels: nil,
},
labelNames,
),
JobFailureCount: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "restic_job_failure_count",
Help: "number of consecutive failures for jobs",
Namespace: "",
Subsystem: "",
ConstLabels: nil,
},
labelNames,
),
SnapshotCurrentCount: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "restic_snapshot_current_total",
Help: "number of current snapshots",
2022-04-13 20:44:48 +00:00
Namespace: "",
Subsystem: "",
ConstLabels: nil,
},
labelNames,
),
SnapshotLatestTime: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "restic_snapshot_latest_time",
Help: "time of the most recent snapshot",
Namespace: "",
Subsystem: "",
ConstLabels: nil,
},
labelNames,
),
}
metrics.Registry.MustRegister(metrics.JobStartTime)
metrics.Registry.MustRegister(metrics.JobFailureCount)
metrics.Registry.MustRegister(metrics.SnapshotCurrentCount)
metrics.Registry.MustRegister(metrics.SnapshotLatestTime)
2022-04-13 20:44:48 +00:00
return metrics
}
var Metrics = InitMetrics()