-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
221 lines (181 loc) · 5.19 KB
/
exporter.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"log"
"sync"
"time"
nomad "github.com/hashicorp/nomad/api"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
)
const (
CheckSuccess = "success"
CheckFailure = "failure"
CheckPending = "pending"
)
var (
metricServicesTotal = metricInfo{
prometheus.NewDesc(
"nomad_services",
"Number of services registers to Nomad native service discovery",
[]string{"namespace"},
nil),
prometheus.GaugeValue,
}
metricServicesHealth = metricInfo{
prometheus.NewDesc(
"nomad_services_health",
"Health status of a service registered to Nomad Service Discovery",
[]string{"namespace", "job_id", "task_name", "service_name", "check_name", "check_id", "status"},
nil),
prometheus.GaugeValue,
}
)
type metricInfo struct {
Desc *prometheus.Desc
Type prometheus.ValueType
}
type Exporter struct {
Config *ExporterConfig
client *nomad.Client
queryOptions *nomad.QueryOptions
mutex sync.RWMutex
cache sync.Map
limit chan struct{}
totalErrs prometheus.Counter
}
type ExporterConfig struct {
Address string
Region string
Namespace string
SecretID string
Duration time.Duration
Parallelism int
AllowStale bool
}
func New(config *ExporterConfig) (*Exporter, error) {
client, err := nomad.NewClient(&nomad.Config{
Address: config.Address,
Region: config.Region,
SecretID: config.SecretID,
Namespace: config.Namespace,
})
if err != nil {
return nil, err
}
exporter := Exporter{
client: client,
queryOptions: &nomad.QueryOptions{AllowStale: config.AllowStale},
limit: make(chan struct{}, config.Parallelism),
Config: config,
totalErrs: prometheus.NewCounter(prometheus.CounterOpts{
Name: "nomad_services_api_errors_total",
Help: "Number of scrapes that resulted with one or more API errors from Nomad",
}),
}
return &exporter, nil
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- metricServicesTotal.Desc
ch <- metricServicesHealth.Desc
ch <- e.totalErrs.Desc()
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()
e.cache = sync.Map{}
// common context for all the requests - cancel everything when deadline reached
ctx, cancel := context.WithTimeout(context.Background(), e.Config.Duration)
defer cancel()
e.queryOptions = e.queryOptions.WithContext(ctx)
err := e.collectServices(ch)
if err != nil {
log.Println("scrape error ", err)
e.totalErrs.Inc()
}
ch <- e.totalErrs
}
func (e *Exporter) collectServices(ch chan<- prometheus.Metric) error {
e.limit <- struct{}{}
listStub, _, err := e.client.Services().List(e.queryOptions)
<-e.limit
if err != nil {
return err
}
errg := new(errgroup.Group)
for _, stub := range listStub {
stub := stub
ch <- prometheus.MustNewConstMetric(
metricServicesTotal.Desc, metricServicesTotal.Type, float64(len(stub.Services)), stub.Namespace,
)
errg.Go(func() error {
return e.collectNamespace(stub.Services, ch)
})
}
return errg.Wait()
}
func (e *Exporter) collectNamespace(services []*nomad.ServiceRegistrationStub, ch chan<- prometheus.Metric) error {
errg := new(errgroup.Group)
for _, svc := range services {
svc := svc
errg.Go(func() error {
return e.collectService(svc.ServiceName, ch)
})
}
return errg.Wait()
}
func (e *Exporter) collectService(svc string, ch chan<- prometheus.Metric) error {
errg := new(errgroup.Group)
e.limit <- struct{}{}
registrations, _, err := e.client.Services().Get(svc, e.queryOptions)
<-e.limit
if err != nil {
return err
}
for _, r := range registrations {
// same allocID can exists for multiple registrations
// avoid scanning and reporting metrics for same allocs (prometheus will complain)
r := r
if _, exists := e.cache.LoadOrStore(r.AllocID, struct{}{}); exists {
continue
}
errg.Go(func() error {
return e.collectAllocation(r, ch)
})
}
return errg.Wait()
}
func (e *Exporter) collectAllocation(reg *nomad.ServiceRegistration, ch chan<- prometheus.Metric) error {
e.limit <- struct{}{}
statuses, err := e.client.Allocations().Checks(reg.AllocID, e.queryOptions)
<-e.limit
if err != nil {
log.Printf("unable to scrape allocation %v: %v\n", reg.AllocID, err)
return err
}
for _, status := range statuses {
// two types of checks exists: readiness and healthiness, we only care about the later
if status.Mode != "healthiness" {
continue
}
var healthy, failure, pending float64
switch status.Status {
case CheckSuccess:
healthy = 1
case CheckFailure:
failure = 1
case CheckPending:
pending = 1
}
ch <- prometheus.MustNewConstMetric(
metricServicesHealth.Desc, metricServicesHealth.Type, healthy, reg.Namespace, reg.JobID, status.Task, status.Service, status.Check, status.ID, CheckSuccess,
)
ch <- prometheus.MustNewConstMetric(
metricServicesHealth.Desc, metricServicesHealth.Type, failure, reg.Namespace, reg.JobID, status.Task, status.Service, status.Check, status.ID, CheckFailure,
)
ch <- prometheus.MustNewConstMetric(
metricServicesHealth.Desc, metricServicesHealth.Type, pending, reg.Namespace, reg.JobID, status.Task, status.Service, status.Check, status.ID, CheckPending,
)
}
return nil
}