-
Notifications
You must be signed in to change notification settings - Fork 17
/
service_discovery_collector.go
211 lines (176 loc) · 5.81 KB
/
service_discovery_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
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
package collectors
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/cloudfoundry-community/bosh_exporter/deployments"
"github.com/cloudfoundry-community/bosh_exporter/filters"
)
const (
boshJobProcessNameLabel = model.MetaLabelPrefix + "bosh_job_process_name"
)
type ProcessesDetails map[string][]ProcessDetails
type ProcessDetails struct {
Name string
DeploymentName string
JobName string
JobID string
JobIndex string
JobAZ string
JobIP string
}
type TargetGroups []TargetGroup
type TargetGroup struct {
Targets []string `json:"targets"`
Labels model.LabelSet `json:"labels,omitempty"`
}
type ServiceDiscoveryCollector struct {
serviceDiscoveryFilename string
azsFilter *filters.AZsFilter
processesFilter *filters.RegexpFilter
lastServiceDiscoveryScrapeTimestampMetric prometheus.Gauge
lastServiceDiscoveryScrapeDurationSecondsMetric prometheus.Gauge
mu *sync.Mutex
}
func NewServiceDiscoveryCollector(
namespace string,
environment string,
boshName string,
boshUUID string,
serviceDiscoveryFilename string,
azsFilter *filters.AZsFilter,
processesFilter *filters.RegexpFilter,
) *ServiceDiscoveryCollector {
lastServiceDiscoveryScrapeTimestampMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_service_discovery_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape of Service Discovery from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)
lastServiceDiscoveryScrapeDurationSecondsMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_service_discovery_scrape_duration_seconds",
Help: "Duration of the last scrape of Service Discovery from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)
collector := &ServiceDiscoveryCollector{
serviceDiscoveryFilename: serviceDiscoveryFilename,
azsFilter: azsFilter,
processesFilter: processesFilter,
lastServiceDiscoveryScrapeTimestampMetric: lastServiceDiscoveryScrapeTimestampMetric,
lastServiceDiscoveryScrapeDurationSecondsMetric: lastServiceDiscoveryScrapeDurationSecondsMetric,
mu: &sync.Mutex{},
}
return collector
}
func (c *ServiceDiscoveryCollector) Collect(deployments []deployments.DeploymentInfo, ch chan<- prometheus.Metric) error {
var begun = time.Now()
processesDetails := make(ProcessesDetails)
for _, deployment := range deployments {
processes := c.getDeploymentProcesses(deployment)
for _, process := range processes {
processesDetails[process.Name] = append(processesDetails[process.Name], process)
}
}
targetGroups := c.createTargetGroups(processesDetails)
err := c.writeTargetGroupsToFile(targetGroups)
c.lastServiceDiscoveryScrapeTimestampMetric.Set(float64(time.Now().Unix()))
c.lastServiceDiscoveryScrapeTimestampMetric.Collect(ch)
c.lastServiceDiscoveryScrapeDurationSecondsMetric.Set(time.Since(begun).Seconds())
c.lastServiceDiscoveryScrapeDurationSecondsMetric.Collect(ch)
return err
}
func (c *ServiceDiscoveryCollector) Describe(ch chan<- *prometheus.Desc) {
c.lastServiceDiscoveryScrapeTimestampMetric.Describe(ch)
c.lastServiceDiscoveryScrapeDurationSecondsMetric.Describe(ch)
}
func (c *ServiceDiscoveryCollector) getDeploymentProcesses(deployment deployments.DeploymentInfo) []ProcessDetails {
processesDetails := []ProcessDetails{}
for _, instance := range deployment.Instances {
if len(instance.IPs) == 0 || !c.azsFilter.Enabled(instance.AZ) {
continue
}
for _, process := range instance.Processes {
if !c.processesFilter.Enabled(process.Name) {
continue
}
processDetails := ProcessDetails{
Name: process.Name,
DeploymentName: deployment.Name,
JobName: instance.Name,
JobID: instance.ID,
JobIndex: instance.Index,
JobAZ: instance.AZ,
JobIP: instance.IPs[0],
}
processesDetails = append(processesDetails, processDetails)
}
}
return processesDetails
}
func (c *ServiceDiscoveryCollector) createTargetGroups(processesDetails ProcessesDetails) TargetGroups {
targetGroups := TargetGroups{}
for name, details := range processesDetails {
targets := []string{}
for _, processDetails := range details {
targets = append(targets, processDetails.JobIP)
}
targetGroup := TargetGroup{
Targets: targets,
Labels: model.LabelSet{
model.LabelName(boshJobProcessNameLabel): model.LabelValue(name),
},
}
targetGroups = append(targetGroups, targetGroup)
}
return targetGroups
}
func (c *ServiceDiscoveryCollector) writeTargetGroupsToFile(targetGroups TargetGroups) error {
targetGroupsJSON, err := json.Marshal(targetGroups)
if err != nil {
return errors.New(fmt.Sprintf("Error while marshalling TargetGroups: %v", err))
}
dir, name := path.Split(c.serviceDiscoveryFilename)
f, err := ioutil.TempFile(dir, name)
if err != nil {
return errors.New(fmt.Sprintf("Error creating temp file: %v", err))
}
_, err = f.Write(targetGroupsJSON)
if err == nil {
err = f.Sync()
}
if closeErr := f.Close(); err == nil {
err = closeErr
}
if permErr := os.Chmod(f.Name(), 0644); err == nil {
err = permErr
}
if err == nil {
err = os.Rename(f.Name(), c.serviceDiscoveryFilename)
}
if err != nil {
os.Remove(f.Name())
}
return err
}