forked from openshift/osd-alert-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.py
310 lines (262 loc) · 9.44 KB
/
questions.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
Questions models
"""
import re
from sqlalchemy import desc
from sqlalchemy.sql import func
from sqlalchemy.sql.expression import text
from models import Alert, Incident, PDAgent
STANDARD_COLUMNS = ["name", "namespace", "urgency", "silenced", "occurrences"]
class Answer:
"""
Container class for the results of Questions
"""
def __init__(self, question_id, column_names, raw_data) -> None:
"""
Sets answer column and data values
"""
self._question_id = question_id
self._column_names = column_names
self._raw_data = raw_data
safe_column_names = [
re.sub(r"[^\d\w]+", "_", cn.strip()) for cn in self._column_names
]
self._column_ids = [f"{scn}_{question_id}" for scn in safe_column_names]
@property
def columns(self):
"""
Returns a Dash DataTable-friendly representation of the columns for this answer
"""
return list(
{"name": cn, "id": cid}
for cn, cid in zip(self._column_names, self._column_ids)
)
@property
def data(self):
"""
Returns a Dash DataTable-friendly representation of the data for this answer
"""
return list(dict(zip(self._column_ids, row)) for row in self._raw_data)
class Question:
"""
Base class for questions
"""
def __init__(self, db_session, since, until):
"""
Constructor for Questions. Mainly sets up DB connection
:param db_session: an SQLAlchemy session
:param since: a datetime.datetime containing the start of the time window over
which the question query will be evaluated
:param until: a datetime.datetime containing the end of the time window over
which the question query will be evaluated
"""
self._since = since
self._until = until
self._db_session = db_session
self._column_names = STANDARD_COLUMNS.copy()
# The following should be defined in child class constructors
self._id = ""
self._description = ""
@classmethod
def _aggregate_by_alert(cls, alert_analyzer_query):
"""
Takes the raw query returned by an SQLAlchemy query and does a value
count/aggregation, returning a table with columns [name, namespace, urgency,
silenced, occurrences]
:returns: an SQLAlchemy result object
"""
alert_count = func.count("*").label("occurrences")
return (
alert_analyzer_query.join(Incident)
.group_by(Alert.name, Alert.namespace, Incident.urgency, Incident.silenced)
.with_entities(
Alert.name,
Alert.namespace,
Incident.urgency,
Incident.silenced,
alert_count,
)
.having(alert_count > 1)
.order_by(desc(alert_count))
.all()
)
def _query(self):
"""
Returns the SQLAlchemy query used to answer this Question
"""
raise NotImplementedError
def get_answer(self):
"""
Returns an Answer to the question
"""
raw_data = self._aggregate_by_alert(self._query())
return Answer(self._id, self._column_names, raw_data)
# pylint: disable=invalid-name
@property
def id(self) -> str:
"""
Get the machine-readable ID of the question
"""
return self._id
@property
def description(self) -> str:
"""
Get the human-readable description of this Question
"""
return self._description
def __str__(self) -> str:
"""
Human-readable string representation
"""
return self._description
def __repr__(self) -> str:
"""
Unambiguous machine-oriented representation
"""
return self._id
class QNeverAcknowledged(Question):
"""
Which alerts have yet to be acknowledged by SRE?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "nack"
self._description = "Which alerts have yet to be acknowledged by SRE?"
def _query(self):
# The ~ operator negates the condition
return (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.filter(Alert.incident.has(~Incident.acknowledged_by.any()))
)
class QNeverAcknowledgedSelfResolved(Question):
"""
Which alerts self-resolve without acknowledgement?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "nacksres"
self._description = "Which alerts self-resolve without acknowledgement?"
def _query(self):
# The ~ operator negates the condition
return (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.filter(Alert.incident.has(~Incident.acknowledged_by.any()))
.filter(
Alert.incident.has(
Incident.resolved_by.has(PDAgent.name.contains("Alertmanager"))
)
)
)
class QAcknowledgedUnresolved(Question):
"""
Which alerts are acknowledged but never resolved?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "ackures"
self._description = "Which alerts are acknowledged but never resolved?"
def _query(self):
# pylint: disable=singleton-comparison
return (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.filter(Alert.incident.has(Incident.acknowledged_by.any()))
.filter(Alert.incident.has(Incident.resolved_at == None))
)
class QSelfResolvedImmediately(Question):
"""
Which alerts self-resolve w/in 15 minutes?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "sres15"
self._description = "Which alerts self-resolve within 15 minutes?"
def _query(self):
# pylint: disable=singleton-comparison
return (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.filter(~Alert.incident.has(Incident.resolved_at == None))
.filter(
Alert.incident.has(
Incident.resolved_by.has(PDAgent.name.contains("Alertmanager"))
)
)
.filter(
Alert.incident.has(
Incident.resolved_at
< func.date_add(Incident.created_at, text("INTERVAL 15 MINUTE"))
)
)
)
class QSREResolvedImmediately(Question):
"""
Which alerts are resolved by SRE within 15 minutes of firing?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "eres15"
self._description = "Which alerts are resolved within 15 minutes by SRE?"
def _query(self):
# pylint: disable=singleton-comparison
return (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.filter(~Alert.incident.has(Incident.resolved_at == None))
.filter(
~Alert.incident.has(
Incident.resolved_by.has(PDAgent.name.contains("Alertmanager"))
)
)
.filter(
Alert.incident.has(
Incident.resolved_at
< func.date_add(Incident.created_at, text("INTERVAL 15 MINUTE"))
)
)
)
class QFlappingShift(Question):
"""
Which alerts fire more than once per on-call shift (in the same cluster)?
"""
def __init__(self, db_session, since, until):
super().__init__(db_session, since, until)
self._id = "sflap"
self._description = (
"Which alerts fire more than once per on-call shift (in the same cluster)?"
)
self._column_names = ["cluster", "name", "namespace", "urgency", "flaps"]
def _query(self):
# First create a subquery that counts flaps per-shift-date
flap_count = func.count("*").label("flap_count")
subq = (
self._db_session.query(Alert)
.filter(Alert.created_at.between(self._since, self._until))
.join(Incident)
.group_by(
Alert.cluster_id,
Alert.name,
Alert.namespace,
Alert.shift,
Incident.urgency,
)
.with_entities(
Alert.cluster_id,
Alert.name,
Alert.namespace,
Incident.urgency,
flap_count,
)
.having(flap_count > 1)
).subquery()
# Then add up the flaps for alerts that flapped on multiple shift-days
flap_sum = func.sum(subq.c.flap_count).label("flap_sum")
return (
self._db_session.query(subq, flap_sum)
.group_by(subq.c.cluster_id, subq.c.name, subq.c.namespace, subq.c.urgency)
.order_by(desc(flap_sum))
)
def get_answer(self):
return Answer(self._id, self._column_names, self._query().all())