-
Notifications
You must be signed in to change notification settings - Fork 24
/
metrics_incident.go
153 lines (130 loc) · 4.2 KB
/
metrics_incident.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
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 MetricsCollectorIncident struct {
collector.Processor
prometheus struct {
incident *prometheus.GaugeVec
incidentStatus *prometheus.GaugeVec
}
teamListOpt []string
}
func (m *MetricsCollectorIncident) Setup(collector *collector.Collector) {
m.Processor.Setup(collector)
m.prometheus.incident = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_incident_info",
Help: "PagerDuty incident",
},
[]string{
"incidentID",
"serviceID",
"incidentUrl",
"incidentNumber",
"title",
"status",
"urgency",
"acknowledged",
"assigned",
"type",
"time",
},
)
m.prometheus.incidentStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_incident_status",
Help: "PagerDuty incident status",
},
[]string{
"incidentID",
"userID",
"time",
"type",
},
)
prometheus.MustRegister(m.prometheus.incident)
prometheus.MustRegister(m.prometheus.incidentStatus)
}
func (m *MetricsCollectorIncident) Reset() {
m.prometheus.incident.Reset()
m.prometheus.incidentStatus.Reset()
}
func (m *MetricsCollectorIncident) Collect(callback chan<- func()) {
listOpts := pagerduty.ListIncidentsOptions{}
listOpts.Limit = PagerdutyListLimit
listOpts.Statuses = Opts.PagerDuty.Incident.Statuses
listOpts.Offset = 0
listOpts.SortBy = "created_at:desc"
if len(m.teamListOpt) > 0 {
listOpts.TeamIDs = m.teamListOpt
}
incidentMetricList := prometheusCommon.NewMetricsList()
incidentStatusMetricList := prometheusCommon.NewMetricsList()
for {
m.Logger().Debugf("fetch incidents (offset: %v, limit:%v)", listOpts.Offset, listOpts.Limit)
list, err := PagerDutyClient.ListIncidentsWithContext(m.Context(), listOpts)
PrometheusPagerDutyApiCounter.WithLabelValues("ListIncidents").Inc()
if err != nil {
m.Logger().Panic(err)
}
for _, incident := range list.Incidents {
// info
createdAt, _ := time.Parse(time.RFC3339, incident.CreatedAt)
incidentMetricList.AddTime(prometheus.Labels{
"incidentID": incident.ID,
"serviceID": incident.Service.ID,
"incidentUrl": incident.HTMLURL,
"incidentNumber": uintToString(incident.IncidentNumber),
"title": incident.Title,
"status": incident.Status,
"urgency": incident.Urgency,
"acknowledged": boolToString(len(incident.Acknowledgements) >= 1),
"assigned": boolToString(len(incident.Assignments) >= 1),
"type": incident.Type,
"time": createdAt.Format(Opts.PagerDuty.Incident.TimeFormat),
}, createdAt)
// acknowledgement
for _, acknowledgement := range incident.Acknowledgements {
createdAt, _ := time.Parse(time.RFC3339, acknowledgement.At)
incidentStatusMetricList.AddTime(prometheus.Labels{
"incidentID": incident.ID,
"userID": acknowledgement.Acknowledger.ID,
"time": createdAt.Format(Opts.PagerDuty.Incident.TimeFormat),
"type": "acknowledgement",
}, createdAt)
}
// assignment
for _, assignment := range incident.Assignments {
createdAt, _ := time.Parse(time.RFC3339, assignment.At)
incidentStatusMetricList.AddTime(prometheus.Labels{
"incidentID": incident.ID,
"userID": assignment.Assignee.ID,
"time": createdAt.Format(Opts.PagerDuty.Incident.TimeFormat),
"type": "assignment",
}, createdAt)
}
// lastChange
changedAt, _ := time.Parse(time.RFC3339, incident.LastStatusChangeAt)
incidentStatusMetricList.AddTime(prometheus.Labels{
"incidentID": incident.ID,
"userID": incident.LastStatusChangeBy.ID,
"time": changedAt.Format(Opts.PagerDuty.Incident.TimeFormat),
"type": "lastChange",
}, changedAt)
}
listOpts.Offset += PagerdutyListLimit
if stopPagerdutyPaging(list.APIListObject) || listOpts.Offset >= Opts.PagerDuty.Incident.Limit {
break
}
}
// set metrics
callback <- func() {
incidentMetricList.GaugeSet(m.prometheus.incident)
incidentStatusMetricList.GaugeSet(m.prometheus.incidentStatus)
}
}