-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
62 lines (52 loc) · 1.23 KB
/
collector.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
package main
import (
"log"
"time"
"github.com/jpillora/backoff"
"github.com/prometheus/client_golang/prometheus"
)
var (
migrationEvent = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "gce_instance_migration_event_active",
Help: "Whether a GCE live migration event is pending; 1 if true, otherwise 0.",
})
)
type GCEMetadataCollector struct {
client *GCEMetadataClient
}
func NewGCEMetadataCollector() *GCEMetadataCollector {
return &GCEMetadataCollector{
client: NewGCEMetadataClient(),
}
}
func (c *GCEMetadataCollector) Start() {
go func() {
boff := &backoff.Backoff{}
var etag string
for {
var value string
result, err := c.client.Get("instance/maintenance-event", &value, GetOptions{
WaitForChange: true,
LastETag: etag,
})
if err != nil {
log.Printf("Error getting metadata: %s", err)
time.Sleep(boff.Duration())
continue
}
switch value {
case "MIGRATE_ON_HOST_MAINTENANCE":
migrationEvent.Set(1)
case "NONE":
migrationEvent.Set(0)
default:
log.Printf("Warning: Received unexpected value %q from instance/maintenance-event endpoint", value)
}
etag = result.ETag
boff.Reset()
}
}()
}
func init() {
prometheus.MustRegister(migrationEvent)
}