-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmetrics_incident.go
More file actions
144 lines (123 loc) · 4.07 KB
/
metrics_incident.go
File metadata and controls
144 lines (123 loc) · 4.07 KB
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
package main
import (
"log/slog"
"time"
"github.com/PagerDuty/go-pagerduty"
"github.com/prometheus/client_golang/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.Collector.RegisterMetricList("pagerduty_incident_info", m.prometheus.incident, true)
m.prometheus.incidentStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "pagerduty_incident_status",
Help: "PagerDuty incident status",
},
[]string{
"incidentID",
"userID",
"time",
"type",
},
)
m.Collector.RegisterMetricList("pagerduty_incident_status", m.prometheus.incidentStatus, true)
}
func (m *MetricsCollectorIncident) 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 := m.Collector.GetMetricList("pagerduty_incident_info")
incidentStatusMetricList := m.Collector.GetMetricList("pagerduty_incident_status")
for {
m.Logger().Debug("fetch incidents", slog.Uint64("offset", uint64(listOpts.Offset)), slog.Uint64("limit", uint64(listOpts.Limit)))
list, err := PagerDutyClient.ListIncidentsWithContext(m.Context(), listOpts)
PrometheusPagerDutyApiCounter.WithLabelValues("ListIncidents").Inc()
if err != nil {
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
}
}
}