2022-04-13 20:44:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-14 23:33:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-04-13 20:44:48 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-11-14 23:33:26 +00:00
|
|
|
"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
|
2022-11-14 23:33:26 +00:00
|
|
|
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{
|
2022-11-14 23:33:26 +00:00
|
|
|
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",
|
2022-11-08 05:01:07 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:33:26 +00:00
|
|
|
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()
|