@@ -8,38 +8,48 @@ import (
8
8
"github.com/prometheus/client_golang/prometheus"
9
9
"github.com/prometheus/client_golang/prometheus/push"
10
10
"github.com/prometheus/common/expfmt"
11
+ "golang.org/x/exp/maps"
11
12
)
12
13
13
- const namespace = "resticprofile"
14
- const backup = "backup"
15
- const groupLabel = "group"
16
- const profileLabel = "profile"
17
- const goVersionLabel = "goversion"
18
- const versionLabel = "version"
14
+ const (
15
+ namespace = "resticprofile"
16
+ backup = "backup"
17
+ groupLabel = "group"
18
+ profileLabel = "profile"
19
+ goVersionLabel = "goversion"
20
+ versionLabel = "version"
21
+ )
19
22
20
23
type Metrics struct {
21
- group string
22
- configLabels map [string ]string
23
- registry * prometheus.Registry
24
- info * prometheus.GaugeVec
25
- backup BackupMetrics
24
+ labels prometheus.Labels
25
+ registry * prometheus.Registry
26
+ info * prometheus.GaugeVec
27
+ backup BackupMetrics
26
28
}
27
29
28
- func NewMetrics (group , version string , configLabels map [string ]string ) * Metrics {
30
+ func NewMetrics (profile , group , version string , configLabels map [string ]string ) * Metrics {
31
+ // default labels for all metrics
32
+ labels := prometheus.Labels {profileLabel : profile }
33
+ if group != "" {
34
+ labels [groupLabel ] = group
35
+ }
36
+ labels = mergeLabels (labels , configLabels )
37
+ keys := maps .Keys (labels )
38
+
29
39
registry := prometheus .NewRegistry ()
30
40
p := & Metrics {
31
- group : group ,
32
- configLabels : configLabels ,
33
- registry : registry ,
41
+ labels : labels ,
42
+ registry : registry ,
34
43
}
35
44
p .info = prometheus .NewGaugeVec (prometheus.GaugeOpts {
36
45
Namespace : namespace ,
37
46
Name : "build_info" ,
38
47
Help : "resticprofile build information." ,
39
- }, mergeKeys ([]string {goVersionLabel , versionLabel }, configLabels ))
40
- p .info .With (mergeLabels (prometheus.Labels {goVersionLabel : runtime .Version (), versionLabel : version }, configLabels )).Set (1 )
48
+ }, append (keys , goVersionLabel , versionLabel ))
49
+ // send the information about the build right away
50
+ p .info .With (mergeLabels (cloneLabels (labels ), map [string ]string {goVersionLabel : runtime .Version (), versionLabel : version })).Set (1 )
41
51
42
- p .backup = newBackupMetrics (group , configLabels )
52
+ p .backup = newBackupMetrics (keys )
43
53
44
54
registry .MustRegister (
45
55
p .info ,
@@ -59,34 +69,29 @@ func NewMetrics(group, version string, configLabels map[string]string) *Metrics
59
69
return p
60
70
}
61
71
62
- func (p * Metrics ) BackupResults (profile string , status Status , summary monitor.Summary ) {
63
- labels := prometheus.Labels {profileLabel : profile }
64
- if p .group != "" {
65
- labels [groupLabel ] = p .group
66
- }
67
- labels = mergeLabels (labels , p .configLabels )
68
- p .backup .duration .With (labels ).Set (summary .Duration .Seconds ())
69
-
70
- p .backup .filesNew .With (labels ).Set (float64 (summary .FilesNew ))
71
- p .backup .filesChanged .With (labels ).Set (float64 (summary .FilesChanged ))
72
- p .backup .filesUnmodified .With (labels ).Set (float64 (summary .FilesUnmodified ))
73
-
74
- p .backup .dirNew .With (labels ).Set (float64 (summary .DirsNew ))
75
- p .backup .dirChanged .With (labels ).Set (float64 (summary .DirsChanged ))
76
- p .backup .dirUnmodified .With (labels ).Set (float64 (summary .DirsUnmodified ))
77
-
78
- p .backup .filesTotal .With (labels ).Set (float64 (summary .FilesTotal ))
79
- p .backup .bytesAdded .With (labels ).Set (float64 (summary .BytesAdded ))
80
- p .backup .bytesTotal .With (labels ).Set (float64 (summary .BytesTotal ))
81
- p .backup .status .With (labels ).Set (float64 (status ))
82
- p .backup .time .With (labels ).Set (float64 (time .Now ().Unix ()))
72
+ func (p * Metrics ) BackupResults (status Status , summary monitor.Summary ) {
73
+ p .backup .duration .With (p .labels ).Set (summary .Duration .Seconds ())
74
+
75
+ p .backup .filesNew .With (p .labels ).Set (float64 (summary .FilesNew ))
76
+ p .backup .filesChanged .With (p .labels ).Set (float64 (summary .FilesChanged ))
77
+ p .backup .filesUnmodified .With (p .labels ).Set (float64 (summary .FilesUnmodified ))
78
+
79
+ p .backup .dirNew .With (p .labels ).Set (float64 (summary .DirsNew ))
80
+ p .backup .dirChanged .With (p .labels ).Set (float64 (summary .DirsChanged ))
81
+ p .backup .dirUnmodified .With (p .labels ).Set (float64 (summary .DirsUnmodified ))
82
+
83
+ p .backup .filesTotal .With (p .labels ).Set (float64 (summary .FilesTotal ))
84
+ p .backup .bytesAdded .With (p .labels ).Set (float64 (summary .BytesAdded ))
85
+ p .backup .bytesTotal .With (p .labels ).Set (float64 (summary .BytesTotal ))
86
+ p .backup .status .With (p .labels ).Set (float64 (status ))
87
+ p .backup .time .With (p .labels ).Set (float64 (time .Now ().Unix ()))
83
88
}
84
89
85
90
func (p * Metrics ) SaveTo (filename string ) error {
86
91
return prometheus .WriteToTextfile (filename , p .registry )
87
92
}
88
93
89
- func (p * Metrics ) Push (url , format string , jobName string ) error {
94
+ func (p * Metrics ) Push (url , format , jobName string ) error {
90
95
var expFmt expfmt.Format
91
96
92
97
if format == "protobuf" {
@@ -101,16 +106,17 @@ func (p *Metrics) Push(url, format string, jobName string) error {
101
106
Add ()
102
107
}
103
108
104
- func mergeKeys (keys []string , add map [string ]string ) []string {
105
- for key := range add {
106
- keys = append (keys , key )
107
- }
108
- return keys
109
- }
110
-
111
109
func mergeLabels (labels prometheus.Labels , add map [string ]string ) prometheus.Labels {
112
110
for key , value := range add {
113
111
labels [key ] = value
114
112
}
115
113
return labels
116
114
}
115
+
116
+ func cloneLabels (labels prometheus.Labels ) prometheus.Labels {
117
+ clone := make (prometheus.Labels , len (labels ))
118
+ for key , value := range labels {
119
+ clone [key ] = value
120
+ }
121
+ return clone
122
+ }
0 commit comments