-
Notifications
You must be signed in to change notification settings - Fork 24
/
metrics_schedule.go
288 lines (238 loc) · 9.13 KB
/
metrics_schedule.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"time"
"github.com/PagerDuty/go-pagerduty"
"github.com/prometheus/client_golang/prometheus"
prometheusCommon "github.com/webdevops/go-common/prometheus"
"github.com/webdevops/go-common/prometheus/collector"
)
type MetricsCollectorSchedule struct {
collector.Processor
prometheus struct {
schedule *prometheus.GaugeVec
scheduleLayer *prometheus.GaugeVec
scheduleLayerEntry *prometheus.GaugeVec
scheduleLayerCoverage *prometheus.GaugeVec
scheduleFinalEntry *prometheus.GaugeVec
scheduleFinalCoverage *prometheus.GaugeVec
scheduleOnCall *prometheus.GaugeVec
scheduleOverwrite *prometheus.GaugeVec
}
}
func (m *MetricsCollectorSchedule) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.schedule = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_info",
Help: "PagerDuty schedule",
},
[]string{"scheduleID", "scheduleName", "scheduleTimeZone"},
)
m.prometheus.scheduleLayer = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_layer_info",
Help: "PagerDuty schedule layer informations",
},
[]string{"scheduleID", "scheduleLayerID", "scheduleLayerName"},
)
m.prometheus.scheduleLayerEntry = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_layer_entry",
Help: "PagerDuty schedule layer entries",
},
[]string{"scheduleLayerID", "scheduleID", "userID", "time", "type"},
)
m.prometheus.scheduleLayerCoverage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_layer_coverage",
Help: "PagerDuty schedule layer entry coverage",
},
[]string{"scheduleLayerID", "scheduleID"},
)
m.prometheus.scheduleFinalEntry = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_final_entry",
Help: "PagerDuty schedule final entries",
},
[]string{"scheduleID", "userID", "time", "type"},
)
m.prometheus.scheduleFinalCoverage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_final_coverage",
Help: "PagerDuty schedule final entry coverage",
},
[]string{"scheduleID"},
)
m.prometheus.scheduleOverwrite = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_schedule_override",
Help: "PagerDuty schedule override",
},
[]string{"overrideID", "scheduleID", "userID", "type"},
)
prometheus.MustRegister(m.prometheus.schedule)
prometheus.MustRegister(m.prometheus.scheduleLayer)
prometheus.MustRegister(m.prometheus.scheduleLayerEntry)
prometheus.MustRegister(m.prometheus.scheduleLayerCoverage)
prometheus.MustRegister(m.prometheus.scheduleFinalEntry)
prometheus.MustRegister(m.prometheus.scheduleFinalCoverage)
prometheus.MustRegister(m.prometheus.scheduleOverwrite)
}
func (m *MetricsCollectorSchedule) Reset() {
m.prometheus.schedule.Reset()
m.prometheus.scheduleLayer.Reset()
m.prometheus.scheduleLayerEntry.Reset()
m.prometheus.scheduleLayerCoverage.Reset()
m.prometheus.scheduleFinalEntry.Reset()
m.prometheus.scheduleFinalCoverage.Reset()
m.prometheus.scheduleOverwrite.Reset()
}
func (m *MetricsCollectorSchedule) Collect(callback chan<- func()) {
listOpts := pagerduty.ListSchedulesOptions{}
listOpts.Limit = PagerdutyListLimit
listOpts.Offset = 0
scheduleMetricList := prometheusCommon.NewMetricsList()
for {
m.Logger().Debugf("fetch schedules (offset: %v, limit:%v)", listOpts.Offset, listOpts.Limit)
list, err := PagerDutyClient.ListSchedulesWithContext(m.Context(), listOpts)
PrometheusPagerDutyApiCounter.WithLabelValues("ListSchedules").Inc()
if err != nil {
m.Logger().Panic(err)
}
for _, schedule := range list.Schedules {
scheduleMetricList.AddInfo(prometheus.Labels{
"scheduleID": schedule.ID,
"scheduleName": schedule.Name,
"scheduleTimeZone": schedule.TimeZone,
})
// get detail information about schedule
m.collectScheduleInformation(schedule.ID, callback)
m.collectScheduleOverrides(schedule.ID, callback)
}
listOpts.Offset += list.Limit
if stopPagerdutyPaging(list.APIListObject) {
break
}
}
// set metrics
callback <- func() {
scheduleMetricList.GaugeSet(m.prometheus.schedule)
}
}
func (m *MetricsCollectorSchedule) collectScheduleInformation(scheduleID string, callback chan<- func()) {
filterSince := time.Now().Add(-Opts.ScrapeTime.General)
filterUntil := time.Now().Add(Opts.PagerDuty.Schedule.EntryTimeframe)
listOpts := pagerduty.GetScheduleOptions{}
listOpts.Since = filterSince.Format(time.RFC3339)
listOpts.Until = filterUntil.Format(time.RFC3339)
m.Logger().Debugf("fetch schedule information (schedule: %v)", scheduleID)
schedule, err := PagerDutyClient.GetScheduleWithContext(m.Context(), scheduleID, listOpts)
PrometheusPagerDutyApiCounter.WithLabelValues("GetSchedule").Inc()
if err != nil {
m.Logger().Panic(err)
}
scheduleLayerMetricList := prometheusCommon.NewMetricsList()
scheduleLayerEntryMetricList := prometheusCommon.NewMetricsList()
scheduleLayerCoverageMetricList := prometheusCommon.NewMetricsList()
scheduleFinalEntryMetricList := prometheusCommon.NewMetricsList()
scheduleFinalCoverageMetricList := prometheusCommon.NewMetricsList()
for _, scheduleLayer := range schedule.ScheduleLayers {
// schedule layer informations
scheduleLayerMetricList.AddInfo(prometheus.Labels{
"scheduleID": scheduleID,
"scheduleLayerID": scheduleLayer.ID,
"scheduleLayerName": scheduleLayer.Name,
})
// schedule layer entries
for _, scheduleEntry := range scheduleLayer.RenderedScheduleEntries {
startTime, _ := time.Parse(time.RFC3339, scheduleEntry.Start)
endTime, _ := time.Parse(time.RFC3339, scheduleEntry.End)
// schedule item start
scheduleLayerEntryMetricList.AddTime(prometheus.Labels{
"scheduleID": scheduleID,
"scheduleLayerID": scheduleLayer.ID,
"userID": scheduleEntry.User.ID,
"time": startTime.Format(Opts.PagerDuty.Schedule.EntryTimeFormat),
"type": "startTime",
}, startTime)
// schedule item end
scheduleLayerEntryMetricList.AddTime(prometheus.Labels{
"scheduleID": scheduleID,
"scheduleLayerID": scheduleLayer.ID,
"userID": scheduleEntry.User.ID,
"time": endTime.Format(Opts.PagerDuty.Schedule.EntryTimeFormat),
"type": "endTime",
}, endTime)
}
// layer coverage
scheduleLayerCoverageMetricList.Add(prometheus.Labels{
"scheduleID": scheduleID,
"scheduleLayerID": scheduleLayer.ID,
}, scheduleLayer.RenderedCoveragePercentage)
}
// final schedule entries
for _, scheduleEntry := range schedule.FinalSchedule.RenderedScheduleEntries {
startTime, _ := time.Parse(time.RFC3339, scheduleEntry.Start)
endTime, _ := time.Parse(time.RFC3339, scheduleEntry.End)
// schedule item start
scheduleFinalEntryMetricList.AddTime(prometheus.Labels{
"scheduleID": scheduleID,
"userID": scheduleEntry.User.ID,
"time": startTime.Format(Opts.PagerDuty.Schedule.EntryTimeFormat),
"type": "startTime",
}, startTime)
// schedule item end
scheduleFinalEntryMetricList.AddTime(prometheus.Labels{
"scheduleID": scheduleID,
"userID": scheduleEntry.User.ID,
"time": endTime.Format(Opts.PagerDuty.Schedule.EntryTimeFormat),
"type": "endTime",
}, endTime)
}
// final schedule coverage
scheduleFinalCoverageMetricList.Add(prometheus.Labels{
"scheduleID": scheduleID,
}, schedule.FinalSchedule.RenderedCoveragePercentage)
// set metrics
callback <- func() {
scheduleLayerMetricList.GaugeSet(m.prometheus.scheduleLayer)
scheduleLayerCoverageMetricList.GaugeSet(m.prometheus.scheduleLayerCoverage)
scheduleLayerEntryMetricList.GaugeSet(m.prometheus.scheduleLayerEntry)
scheduleFinalEntryMetricList.GaugeSet(m.prometheus.scheduleFinalEntry)
scheduleFinalCoverageMetricList.GaugeSet(m.prometheus.scheduleFinalCoverage)
}
}
func (m *MetricsCollectorSchedule) collectScheduleOverrides(scheduleID string, callback chan<- func()) {
filterSince := time.Now().Add(-Opts.ScrapeTime.General)
filterUntil := time.Now().Add(Opts.PagerDuty.Schedule.OverrideTimeframe)
listOpts := pagerduty.ListOverridesOptions{}
listOpts.Since = filterSince.Format(time.RFC3339)
listOpts.Until = filterUntil.Format(time.RFC3339)
overrideMetricList := prometheusCommon.NewMetricsList()
m.Logger().Debugf("fetch schedule overrides (schedule: %v)", scheduleID)
list, err := PagerDutyClient.ListOverridesWithContext(m.Context(), scheduleID, listOpts)
PrometheusPagerDutyApiCounter.WithLabelValues("ListOverrides").Inc()
if err != nil {
m.Logger().Panic(err)
}
for _, override := range list.Overrides {
startTime, _ := time.Parse(time.RFC3339, override.Start)
endTime, _ := time.Parse(time.RFC3339, override.End)
overrideMetricList.AddTime(prometheus.Labels{
"overrideID": override.ID,
"scheduleID": scheduleID,
"userID": override.User.ID,
"type": "startTime",
}, startTime)
overrideMetricList.AddTime(prometheus.Labels{
"overrideID": override.ID,
"scheduleID": scheduleID,
"userID": override.User.ID,
"type": "endTime",
}, endTime)
}
// set metrics
callback <- func() {
overrideMetricList.GaugeSet(m.prometheus.scheduleOverwrite)
}
}