File size: 27,572 Bytes
44084af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#!/bin/bash
# oracle: rewrites changed files to the oracle state
set -euo pipefail
cd /workspace/monitoring
mkdir -p 'alertmanager'
cat > 'alertmanager/alertmanager.yml' <<'AF_EOF'
# Shared Alertmanager config (this repo, platform and data Prometheus servers).
# Receivers belong to the teams; ask before touching someone else's.
global:
  resolve_timeout: 5m
  slack_api_url_file: /etc/alertmanager/secrets/slack-url
templates:
- /etc/alertmanager/templates/*.tmpl
route:
  receiver: slack-alerts-default
  group_by: [alertname, service]
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  routes:
  # rule evaluation problems. Nobody pages on these (yet).
  - matchers:
    - alertname=~"PrometheusRuleFailures|PrometheusRuleEvaluationSlow"
    receiver: slack-prometheus-meta
  # SRE sees every page as FYI; owners get paged by their own subtree below.
  - matchers:
    - severity=~"page|critical"
    receiver: slack-sre-fyi
    continue: true
  # ingest: still on the old match syntax
  - match:
      team: ingest
    receiver: slack-ingest
    routes:
    - match_re:
        severity: page|critical
      receiver: pagerduty-ingest
      continue: true
    - match:
        severity: ticket
      receiver: jira-ingest
      continue: true
    - receiver: slack-ingest
  - matchers:
    - team="subscriptions"
    receiver: slack-subscriptions
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-subscriptions
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-subscriptions
      continue: true
    # tickets and warnings per pod in Slack, so the channel shows which pod; pages group per service above
    - receiver: slack-subscriptions
      group_by: [alertname, pod, service]
  - matchers:
    - team="discovery"
    receiver: slack-discovery
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-discovery
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-discovery
      continue: true
    - receiver: slack-discovery
  - matchers:
    - team="playback"
    receiver: slack-playback
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-playback
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-playback
      continue: true
    - receiver: slack-playback
  - matchers:
    - team="content-pipeline"
    receiver: slack-content-pipeline
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-content-pipeline
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-content-pipeline
      continue: true
    - receiver: slack-content-pipeline
  # discovery services from before team labels were everywhere
  - matchers:
    - service=~"search-indexer|catalog-svc|recs-svc"
    receiver: slack-discovery
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-discovery
      continue: true
    - receiver: slack-discovery
  - matchers:
    - team="rights"
    receiver: slack-rights
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-rights
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-rights
      continue: true
    - receiver: slack-rights
  # teams below route alerts from the platform and data Prometheus servers
  - matchers:
    - team="platform"
    receiver: slack-platform
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-platform
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-platform
      continue: true
    - receiver: slack-platform
  - matchers:
    - team="security-ops"
    receiver: slack-security-ops
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-security-ops
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-security-ops
      continue: true
    - receiver: slack-security-ops
  # network: fewer updates per group, too chatty at 5m
  - matchers:
    - team="network"
    receiver: slack-network
    group_interval: 10m
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-network
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-network
      continue: true
    - receiver: slack-network
  - matchers:
    - team="data-platform"
    receiver: slack-data-platform
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-data-platform
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-data-platform
      continue: true
    - receiver: slack-data-platform
  - matchers:
    - team="database-reliability"
    receiver: slack-database-reliability
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-database-reliability
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-database-reliability
      continue: true
    - receiver: slack-database-reliability
  # messaging asked for hourly repeats while something is still firing
  - matchers:
    - team="messaging"
    receiver: slack-messaging
    repeat_interval: 1h
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-messaging
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-messaging
      continue: true
    - receiver: slack-messaging
  - matchers:
    - team="ml-platform"
    receiver: slack-ml-platform
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-ml-platform
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-ml-platform
      continue: true
    - receiver: slack-ml-platform
  # edge wanted tickets in Slack as well as Jira (INFRA-1201)
  - matchers:
    - team="edge"
    receiver: slack-edge
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-edge
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-edge
      continue: true
    - receiver: slack-edge
  - matchers:
    - team="storage"
    receiver: slack-storage
    routes:
    - matchers:
      - severity=~"page|critical"
      receiver: pagerduty-storage
      continue: true
    - matchers:
      - severity="ticket"
      receiver: jira-storage
      continue: true
    - receiver: slack-storage
receivers:
- name: slack-alerts-default
  slack_configs:
  - channel: "#alerts-default"
    send_resolved: true
- name: slack-sre-fyi
  slack_configs:
  - channel: "#sre-fyi"
    send_resolved: false
- name: slack-prometheus-meta
  slack_configs:
  - channel: "#prometheus-meta"
- name: pagerduty-sre
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/sre
- name: pagerduty-content-pipeline
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/content-pipeline
- name: slack-content-pipeline
  slack_configs:
  - channel: "#content-pipeline-alerts"
    send_resolved: true
- name: jira-content-pipeline
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/content_pipeline
- name: pagerduty-data-platform
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/data-platform
- name: slack-data-platform
  slack_configs:
  - channel: "#data-platform-alerts"
    send_resolved: true
- name: jira-data-platform
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/data_platform
- name: pagerduty-database-reliability
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/database-reliability
- name: slack-database-reliability
  slack_configs:
  - channel: "#database-reliability-alerts"
    send_resolved: true
- name: jira-database-reliability
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/database_reliability
- name: pagerduty-discovery
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/discovery
- name: slack-discovery
  slack_configs:
  - channel: "#discovery-alerts"
    send_resolved: true
- name: jira-discovery
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/discovery
- name: pagerduty-edge
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/edge
- name: slack-edge
  slack_configs:
  - channel: "#edge-alerts"
    send_resolved: true
- name: jira-edge
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/edge
- name: pagerduty-ingest
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/ingest
- name: slack-ingest
  slack_configs:
  - channel: "#ingest-alerts"
    send_resolved: true
- name: jira-ingest
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/ingest
- name: pagerduty-messaging
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/messaging
- name: slack-messaging
  slack_configs:
  - channel: "#messaging-alerts"
    send_resolved: true
- name: jira-messaging
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/messaging
- name: pagerduty-ml-platform
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/ml-platform
- name: slack-ml-platform
  slack_configs:
  - channel: "#ml-platform-alerts"
    send_resolved: true
- name: jira-ml-platform
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/ml_platform
- name: pagerduty-network
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/network
- name: slack-network
  slack_configs:
  - channel: "#network-alerts"
    send_resolved: true
- name: jira-network
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/network
- name: pagerduty-platform
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/platform
- name: slack-platform
  slack_configs:
  - channel: "#platform-alerts"
    send_resolved: true
- name: jira-platform
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/platform
- name: pagerduty-playback
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/playback
- name: slack-playback
  slack_configs:
  - channel: "#playback-alerts"
    send_resolved: true
- name: jira-playback
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/playback
- name: pagerduty-rights
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/rights
- name: slack-rights
  slack_configs:
  - channel: "#rights-alerts"
    send_resolved: true
- name: jira-rights
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/rights
- name: pagerduty-security-ops
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/security-ops
- name: slack-security-ops
  slack_configs:
  - channel: "#security-ops-alerts"
    send_resolved: true
- name: jira-security-ops
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/security_ops
- name: pagerduty-storage
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/storage
- name: slack-storage
  slack_configs:
  - channel: "#storage-alerts"
    send_resolved: true
- name: jira-storage
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/storage
- name: pagerduty-subscriptions
  pagerduty_configs:
  - routing_key_file: /etc/alertmanager/secrets/pagerduty/subscriptions
- name: slack-subscriptions
  slack_configs:
  - channel: "#subscriptions-alerts"
    send_resolved: true
- name: jira-subscriptions
  webhook_configs:
  - url: https://jira-bridge.int.ondaplay.example/hooks/subscriptions
# kept for the platform Prometheus until its config is cleaned up
- name: slack-ops-legacy
  slack_configs:
  - channel: "#ops-alerts-legacy"
    send_resolved: true
inhibit_rules:
# a critical copy holds back the warning copy of the same alert
- source_matchers:
  - severity="critical"
  target_matchers:
  - severity="warning"
  equal: [alertname, service]
AF_EOF
mkdir -p 'rules'
cat > 'rules/legacy-content-pipeline.yml' <<'AF_EOF'
# content-pipeline alerts from before the conventions in README.md.
# Don't reformat in passing; move things out when you touch them for a reason.
groups:
# old dashboard records; the svc-overview board still reads these
- name: content-pipeline-records
  rules:
  - record: job:http_requests:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service=~"subtitle-svc|transcode-worker"}[5m]))
  - record: job:http_errors:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service=~"subtitle-svc|transcode-worker",code=~"5.."}[5m]))
  # running total for the 'errors since deploy' stat on svc-overview
  - record: service:http_errors:sum
    expr: sum by (service) (http_requests_total{code=~"5.."})
- name: subtitle-svc
  rules:
  - alert: SubtitleSvcHighErrorRatio
    expr: sum by (service) (rate(http_requests_total{service="subtitle-svc",code=~"5.."}[5m])) / sum by (service) (rate(http_requests_total{service="subtitle-svc"}[5m])) > 0.1
    for: "10m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcHighErrorRatio
  - alert: SubtitleSvcLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="subtitle-svc"}[5m]))) > 0.5
    for: "10m"
    labels: {severity: warning, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} p99 above 500ms"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcLatencyHigh
  - alert: SubtitleSvcLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="subtitle-svc"}[5m]))) > 1
    for: "10m"
    labels: {severity: critical, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} p99 above 1s"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcLatencyHigh
  - alert: SubtitleSvcDbConnectionsHigh
    expr: max by (service) (db_pool_connections_active{service="subtitle-svc"} / db_pool_connections_max{service="subtitle-svc"}) > 0.8
    for: "5m"
    labels: {severity: warning, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} is using over 80% of its DB pool"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcDbConnectionsHigh
  - alert: SubtitleSvcDbConnectionsHigh
    expr: max by (service) (db_pool_connections_active{service="subtitle-svc"} / db_pool_connections_max{service="subtitle-svc"}) > 0.95
    for: "5m"
    labels: {severity: critical, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} DB pool nearly exhausted"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcDbConnectionsHigh
  - alert: DbClientConnectionErrors
    expr: rate(db_client_connection_errors_total{service="subtitle-svc"}[2m]) > 0.5
    for: "2m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.pod }} can't get database connections"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/DbClientConnectionErrors
  - alert: SubtitleSvcErrorBurst
    expr: sum by (service) (rate(http_requests_total{service="subtitle-svc",code=~"5.."}[5m])) > 4
    for: "10m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} is returning more than 4 errors/s"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcErrorBurst
- name: transcode-worker
  rules:
  - alert: TranscodeWorkerHighErrorRatio
    expr: sum by (service) (rate(http_requests_total{service="transcode-worker",code=~"5.."}[5m])) / sum by (service) (rate(http_requests_total{service="transcode-worker"}[5m])) > 0.1
    for: "10m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/TranscodeWorkerHighErrorRatio
  - alert: DbClientConnectionErrors
    expr: rate(db_client_connection_errors_total{service="transcode-worker"}[2m]) > 0.5
    for: "2m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.pod }} can't get database connections"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/DbClientConnectionErrors
  - alert: TranscodeWorkerQueueBacklogHigh
    expr: max by (service) (work_queue_depth{service="transcode-worker"}) > 5000
    for: "15m"
    labels: {severity: page, team: content-pipeline}
    annotations:
      summary: "{{ $labels.service }} has more than 5000 jobs waiting"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/TranscodeWorkerQueueBacklogHigh
AF_EOF
mkdir -p 'rules'
cat > 'rules/legacy-discovery.yml' <<'AF_EOF'
# discovery alerts from before the conventions in README.md.
# Don't reformat in passing; move things out when you touch them for a reason.
groups:
# old dashboard records; the svc-overview board still reads these
- name: discovery-records
  rules:
  - record: job:grpc_calls:rate5m
    expr: sum by (job, service) (rate(grpc_server_handled_total{service=~"catalog-svc|recs-svc"}[5m]))
  # keep: finance reads this one for the monthly availability report
  - record: job:grpc_errors:rate5m
    expr: sum by (job, service) (rate(grpc_server_handled_total{service=~"catalog-svc|recs-svc",grpc_code=~"Unavailable|Internal|DeadlineExceeded|Unknown|DataLoss"}[5m]))
  - record: job:http_requests:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service="license-gw"}[5m]))
  - record: job:http_errors:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service="license-gw",code=~"5.."}[5m]))
- name: catalog-svc
  rules:
  # Cancelled isn't in the list on purpose: clients hang up on long polls
  - alert: CatalogSvcHighErrorRatio
    expr: sum by (service) (rate(grpc_server_handled_total{service="catalog-svc",grpc_code=~"Unavailable|Internal|DeadlineExceeded|Unknown|DataLoss"}[5m])) / sum by (service) (rate(grpc_server_handled_total{service="catalog-svc"}[5m])) > 0.02
    for: "10m"
    labels: {severity: page, team: discovery}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/CatalogSvcHighErrorRatio
- name: recs-svc
  rules:
  - alert: RecsSvcHighErrorRatio
    expr: sum by (service) (rate(grpc_server_handled_total{service="recs-svc",grpc_code=~"Unavailable|Internal|DeadlineExceeded|Unknown|DataLoss"}[5m])) / sum by (service) (rate(grpc_server_handled_total{service="recs-svc"}[5m])) > 0.05
    for: "10m"
    labels: {severity: page, team: discovery}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/RecsSvcHighErrorRatio
- name: license-gw
  rules:
  - alert: LicenseGwHighErrorRatio
    expr: sum by (service) (rate(http_requests_total{service="license-gw",code=~"5.."}[5m])) / sum by (service) (rate(http_requests_total{service="license-gw"}[5m])) > 0.05
    for: "10m"
    labels: {severity: page, team: rights}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwHighErrorRatio
  - alert: LicenseGwLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="license-gw"}[5m]))) > 0.5
    for: "10m"
    labels: {severity: warning, team: rights}
    annotations:
      summary: "{{ $labels.service }} p99 above 500ms"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwLatencyHigh
  - alert: LicenseGwLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="license-gw"}[5m]))) > 1
    for: "10m"
    labels: {severity: critical, team: rights}
    annotations:
      summary: "{{ $labels.service }} p99 above 1s"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwLatencyHigh
AF_EOF
mkdir -p 'rules'
cat > 'rules/legacy-playback.yml' <<'AF_EOF'
# playback alerts from before the conventions in README.md.
# Don't reformat in passing; move things out when you touch them for a reason.
groups:
# old dashboard records; the svc-overview board still reads these
- name: playback-records
  rules:
  - record: job:http_requests:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service="playback-api"}[5m]))
  - record: job:http_errors:rate5m
    expr: sum by (job, service) (rate(http_requests_total{service="playback-api",code=~"5.."}[5m]))
- name: playback-api
  rules:
  # threshold agreed with playback in INFRA-1871
  - alert: PlaybackApiHighErrorRatio
    expr: sum by (service) (rate(http_requests_total{service="playback-api",code=~"5.."}[5m])) / sum by (service) (rate(http_requests_total{service="playback-api"}[5m])) > 0.05
    for: "10m"
    labels: {severity: page, team: playback}
    annotations:
      summary: "{{ $labels.service }} is failing {{ $value | humanizePercentage }} of requests"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiHighErrorRatio
  - alert: PlaybackApiLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="playback-api"}[5m]))) > 0.5
    for: "10m"
    labels: {severity: warning, team: playback}
    annotations:
      summary: "{{ $labels.service }} p99 above 500ms"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiLatencyHigh
  - alert: PlaybackApiLatencyHigh
    expr: histogram_quantile(0.99, sum by (service, le) (rate(http_request_duration_seconds_bucket{service="playback-api"}[5m]))) > 1
    for: "10m"
    labels: {severity: critical, team: playback}
    annotations:
      summary: "{{ $labels.service }} p99 above 1s"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiLatencyHigh
  - alert: PlaybackApiDbConnectionsHigh
    expr: max by (service) (db_pool_connections_active{service="playback-api"} / db_pool_connections_max{service="playback-api"}) > 0.8
    for: "5m"
    labels: {severity: warning, team: playback}
    annotations:
      summary: "{{ $labels.service }} is using over 80% of its DB pool"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiDbConnectionsHigh
  - alert: PlaybackApiDbConnectionsHigh
    expr: max by (service) (db_pool_connections_active{service="playback-api"} / db_pool_connections_max{service="playback-api"}) > 0.95
    for: "5m"
    labels: {severity: critical, team: playback}
    annotations:
      summary: "{{ $labels.service }} DB pool nearly exhausted"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiDbConnectionsHigh
  - alert: DbClientConnectionErrors
    expr: rate(db_client_connection_errors_total{service="playback-api"}[2m]) > 0.5
    for: "2m"
    labels: {severity: page, team: playback}
    annotations:
      summary: "{{ $labels.pod }} can't get database connections"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/DbClientConnectionErrors
AF_EOF
mkdir -p 'rules'
cat > 'rules/service-health.yml' <<'AF_EOF'
# One Down alert per service. See README, Service health.
groups:
- name: service-health
  rules:
  - alert: PlaybackApiDown
    expr: |
      sum by (service) (up{service="playback-api"}) == 0
      or absent(up{service="playback-api"})
    for: 3m
    labels:
      severity: page
      team: playback
    annotations:
      summary: playback-api has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/PlaybackApiDown
  # for: 3m so a rolling restart doesn't page; subtitle-svc cycles one pod at a time
  - alert: SubtitleSvcDown
    expr: |
      sum by (service) (up{service="subtitle-svc"}) == 0
      or absent(up{service="subtitle-svc"})
    for: 3m
    labels:
      severity: page
      team: content-pipeline
    annotations:
      summary: subtitle-svc has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SubtitleSvcDown
  - alert: TranscodeWorkerDown
    expr: |
      sum by (service) (up{service="transcode-worker"}) == 0
      or absent(up{service="transcode-worker"})
    for: 3m
    labels:
      severity: page
      team: content-pipeline
    annotations:
      summary: transcode-worker has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/TranscodeWorkerDown
  - alert: SearchIndexerDown
    expr: |
      sum by (service) (up{service="search-indexer"}) == 0
      or absent(up{service="search-indexer"})
    for: 3m
    labels:
      severity: page
      team: discovery
    annotations:
      summary: search-indexer has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/SearchIndexerDown
  - alert: CatalogSvcDown
    expr: |
      sum by (service) (up{service="catalog-svc"}) == 0
      or absent(up{service="catalog-svc"})
    for: 3m
    labels:
      severity: page
      team: discovery
    annotations:
      summary: catalog-svc has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/CatalogSvcDown
  - alert: RecsSvcDown
    expr: |
      sum by (service) (up{service="recs-svc"}) == 0
      or absent(up{service="recs-svc"})
    for: 3m
    labels:
      severity: page
      team: discovery
    annotations:
      summary: recs-svc has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/RecsSvcDown
  - alert: LicenseGwDown
    expr: |
      sum by (service) (up{service="license-gw"}) == 0
      or absent(up{service="license-gw"})
    for: 3m
    labels:
      severity: page
      team: rights
    annotations:
      summary: license-gw has no healthy scrape targets
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwDown
AF_EOF
mkdir -p 'rules'
cat > 'rules/slo-license-gw.yml' <<'AF_EOF'
groups:
- name: slo-license-gw
  rules:
  - alert: LicenseGwErrorBudgetBurnFast
    expr: |
      slo:sli_error:ratio_rate1h{service="license-gw"} > (14.4 * 0.005)
      and slo:sli_error:ratio_rate5m{service="license-gw"} > (14.4 * 0.005)
    for: 2m
    labels:
      severity: page
      team: rights
      slo: license-gw-availability
    annotations:
      summary: "{{ $labels.service }} is spending its error budget fast"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwErrorBudgetBurnFast
  - alert: LicenseGwErrorBudgetBurnSlow
    expr: |
      slo:sli_error:ratio_rate6h{service="license-gw"} > (6 * 0.005)
      and slo:sli_error:ratio_rate30m{service="license-gw"} > (6 * 0.005)
    for: 15m
    labels:
      severity: ticket
      team: rights
      slo: license-gw-availability
    annotations:
      summary: "{{ $labels.service }} is spending its error budget steadily"
      runbook_url: https://runbooks.int.ondaplay.example/alerts/LicenseGwErrorBudgetBurnSlow
AF_EOF