9
9
"time"
10
10
11
11
"github.com/prometheus/client_golang/prometheus"
12
+
13
+ "github.com/drone/autoscaler"
12
14
)
13
15
14
16
var noContext = context .Background ()
@@ -40,6 +42,10 @@ type Collector interface {
40
42
// IncrServerSetupError keeps a count of errors encountered
41
43
// when installing software on servers.
42
44
IncrServerSetupError ()
45
+
46
+ RegisterKnownInstance (instance * autoscaler.Instance )
47
+
48
+ UnregisterKnownInstance (instance * autoscaler.Instance )
43
49
}
44
50
45
51
// Prometheus is a Prometheus metrics collector.
@@ -50,6 +56,7 @@ type Prometheus struct {
50
56
countServerCreateErr prometheus.Counter
51
57
countServerInitErr prometheus.Counter
52
58
countServerSetupErr prometheus.Counter
59
+ knownInstance * prometheus.GaugeVec
53
60
}
54
61
55
62
// New returns a new Prometheus metrics provider.
@@ -82,12 +89,23 @@ func New() *Prometheus {
82
89
Name : "drone_server_install_errors_total" ,
83
90
Help : "Total number of errors installing software on a server." ,
84
91
})
92
+ p .knownInstance = prometheus .NewGaugeVec (prometheus.GaugeOpts {
93
+ Name : "drone_server_known_instance" ,
94
+ Help : "Known server instances." ,
95
+ },
96
+ []string {
97
+ "name" ,
98
+ "provider" ,
99
+ "region" ,
100
+ "size" ,
101
+ })
85
102
prometheus .MustRegister (p .trackServerCreateTime )
86
103
prometheus .MustRegister (p .trackServerInitTime )
87
104
prometheus .MustRegister (p .trackServerSetupTime )
88
105
prometheus .MustRegister (p .countServerCreateErr )
89
106
prometheus .MustRegister (p .countServerInitErr )
90
107
prometheus .MustRegister (p .countServerSetupErr )
108
+ prometheus .MustRegister (p .knownInstance )
91
109
return p
92
110
}
93
111
@@ -135,6 +153,26 @@ func (m *Prometheus) IncrServerSetupError() {
135
153
m .countServerSetupErr .Inc ()
136
154
}
137
155
156
+ // RegisterKnownInstance registers that we know about a server.
157
+ func (m * Prometheus ) RegisterKnownInstance (instance * autoscaler.Instance ) {
158
+ m .knownInstance .With (prometheus.Labels {
159
+ "name" : instance .Name ,
160
+ "provider" : string (instance .Provider ),
161
+ "region" : instance .Region ,
162
+ "size" : instance .Size ,
163
+ }).Set (1 )
164
+ }
165
+
166
+ // UnregisterKnownInstance forgets a server we once knew.
167
+ func (m * Prometheus ) UnregisterKnownInstance (instance * autoscaler.Instance ) {
168
+ m .knownInstance .Delete (prometheus.Labels {
169
+ "name" : instance .Name ,
170
+ "provider" : string (instance .Provider ),
171
+ "region" : instance .Region ,
172
+ "size" : instance .Size ,
173
+ })
174
+ }
175
+
138
176
// NopCollector provides a no-op metrics collector.
139
177
type NopCollector struct {}
140
178
@@ -163,3 +201,9 @@ func (*NopCollector) IncrServerInitError() {}
163
201
// IncrServerSetupError keeps a count of errors encountered
164
202
// when installing software on servers.
165
203
func (* NopCollector ) IncrServerSetupError () {}
204
+
205
+ // RegisterKnownInstance registers that we know about a server.
206
+ func (* NopCollector ) RegisterKnownInstance (instance * autoscaler.Instance ) {}
207
+
208
+ // UnregisterKnownInstance forgets a server we once knew.
209
+ func (* NopCollector ) UnregisterKnownInstance (instance * autoscaler.Instance ) {}
0 commit comments