-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmetrics.go
190 lines (178 loc) · 6.15 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"net/http"
"os"
"runtime/debug"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)
const (
// backupStatusIdle indicates there is no backup in progress
backupStatusIdle = 0
// backupStatusFailed indicates no running backup and the last one failed
backupStatusFailed = -1
// backupStatusRunning indicates the backup is currently in progress
backupStatusRunning = 1
)
var (
// bucketTime is the bucket configuration for time-based histograms (in ms)
bucketTime = []float64{
float64((5 * time.Minute).Milliseconds()),
float64((15 * time.Minute).Milliseconds()),
float64((30 * time.Minute).Milliseconds()),
float64((1 * time.Hour).Milliseconds()),
float64((2 * time.Hour).Milliseconds()),
float64((4 * time.Hour).Milliseconds()),
float64((8 * time.Hour).Milliseconds()),
float64((12 * time.Hour).Milliseconds()),
}
// bucketFileCount is the bucket configuration for file count
bucketFileCount = []float64{5, 50, 500, 5000, 50000, 500000, 5000000}
// bucketFileSize is the bucket configuration for file size
// ranging from 1 MB to 10 TB
bucketFileSize = []float64{1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13}
)
// metrics is used to hold all the Prometheus metrics used
type metrics struct {
backupDuration prometheus.Histogram
backupInfo prometheus.Gauge
backupStatus prometheus.Gauge
backupsFailed prometheus.Counter
backupsSuccessful prometheus.Counter
backupsSuccessfulTimestamp prometheus.Gauge
backupsTotal prometheus.Counter
bytesAdded prometheus.Histogram
bytesProcessed prometheus.Histogram
filesChanged prometheus.Histogram
filesNew prometheus.Histogram
filesProcessed prometheus.Histogram
filesUnmodified prometheus.Histogram
}
// initializeMetrics configures and registers the Prometheus metrics
func (b *backup) initializeMetrics() {
b.backupsTotal = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "backup",
Name: "backups_all_total",
Help: "The total number of backups attempted, including failures.",
})
b.backupsSuccessful = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "backup",
Name: "backups_successful_total",
Help: "The total number of backups that succeeded.",
})
b.backupsFailed = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "backup",
Name: "backups_failed_total",
Help: "The total number of backups that failed.",
})
b.backupDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_duration_milliseconds",
Help: "The duration of backups in milliseconds.",
Buckets: append([]float64{}, bucketTime...),
})
b.filesNew = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_files_new",
Help: "Amount of new files.",
Buckets: append([]float64{}, bucketFileCount...),
})
b.filesChanged = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_files_changed",
Help: "Amount of files with changes.",
Buckets: append([]float64{}, bucketFileCount...),
})
b.filesUnmodified = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_files_unmodified",
Help: "Amount of files unmodified since last backup.",
Buckets: append([]float64{}, bucketFileCount...),
})
b.filesProcessed = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_files_processed",
Help: "Total number of files scanned by the backup for changes.",
Buckets: append([]float64{}, bucketFileCount...),
})
b.bytesAdded = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_added_bytes",
Help: "Total number of bytes added to the repository.",
Buckets: append([]float64{}, bucketFileSize...),
})
b.bytesProcessed = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "backup",
Name: "backup_processed_bytes",
Help: "Total number of bytes scanned by the backup for changes",
Buckets: append([]float64{}, bucketFileSize...),
})
b.backupsSuccessfulTimestamp = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "backup",
Name: "backup_successful_timestamp",
Help: "Timestamp of last successful backup",
})
b.backupStatus = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "backup",
Name: "backup_status",
Help: "Backup status (1 = backing up, 0 = idle, -1 = idle after failed backup)",
})
b.backupInfo = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "backup",
Name: "backup_info",
Help: "Information about the backup process",
ConstLabels: prometheus.Labels(getVersionInfo()),
})
b.backupInfo.Set(1)
prometheus.MustRegister(
b.backupDuration,
b.backupInfo,
b.backupStatus,
b.backupsFailed,
b.backupsSuccessful,
b.backupsSuccessfulTimestamp,
b.backupsTotal,
b.bytesAdded,
b.bytesProcessed,
b.filesChanged,
b.filesNew,
b.filesProcessed,
b.filesUnmodified,
)
}
// getBuildInfo returns a value from Go's build-in debug information
func getBuildInfo(key string) string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == key {
return setting.Value
}
}
}
// not found
return ""
}
// getVersionInfo returns a list of key value pairs to become part of the info metric
func getVersionInfo() map[string]string {
res := make(map[string]string)
// add the hostname
host, err := os.Hostname()
if err != nil {
panic(err)
}
res["hostname"] = host
// add the short git hash of the binary's latest commit
revision := getBuildInfo("vcs.revision")
if revision != "" {
res["commit"] = revision[:7]
}
return res
}
func (b *backup) startMetricsServer() {
http.Handle(b.PrometheusEndpoint, promhttp.Handler())
logger.Info("metrics server listening at " + b.PrometheusAddress)
err := http.ListenAndServe(b.PrometheusAddress, nil)
logger.Fatal("metrics server closed", zap.Error(err))
}