forked from TsinghuaDatabaseGroup/DB-GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgsql_rules.yml
1551 lines (1301 loc) · 66.5 KB
/
pgsql_rules.yml
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#This file contains code that utilizes the Vonng pigsty project.
#
#Original source: https://github.com/Vonng/pigsty
#
#We sincerely thank the developers of the Vonng pigsty for their hard work and contribution to the open source community.
#Their code has been instrumental in enhancing our project.
#
#Pigsty is licensed under AGPL-3.0 license. Please refer to its license file for more details.
#==============================================================#
# File : pgsql.yml
# Ctime : 2020-04-22
# Mtime : 2023-08-19
# Desc : Rules for postgres & pgbouncer
# Path : /etc/prometheus/rules/pgsql.yml
# Author : Ruohang Feng (rh@vonng.com)
# License : AGPLv3
#==============================================================#
################################################################
# PGSQL Rules #
################################################################
# Naming Pattern:
#
# pg : ins : xact_commit _ 15m
# ^ ^ ^ ^
# <namespace>:<level>:<metrics_name>_<modifier>
#--------------------------------------------------------------#
# Metrics Hierarchy
# -------------------------------------------------------------
# env --> environment {job}
# ^--> cls --> cluster {job, cls}
# -------|-----------------------------------------------------
# ^--> ins --> {job, cls, ins} + {instance,ip,etc...}
# ------------|------------------------------------------------
# ^-- datname --> {datname}
# ^---- pool --> {datname, user}
# ^---- table --> {datname, relname}
# ^---- index --> {datname, relname, idxname}
# ^---- query --> {datname, query}
# ^---- ..... --> {datname, ...}
#--------------------------------------------------------------#
# Alert Rule Format:
#
# Level: three alert levels
# 0 : CRIT System failure needs immediate intervene call (e.g down) 1m
# 1 : WARN Anomalies could lead to failure soon sms (e.g high) 1m
# 2 : INFO Warning events that need attention mail 5m
# - alert: PostgresDown <------- CamelCase Alert Name
# expr: pg_up < 1 <------- Expression
# for: 1m <------- Duration Threshold
# ^------- (omit) : Trigger immediately
# labels:
# level: 0 <------- numeric expression of severity 0,1,2
# severity: CRIT <------- alert severity: fatal,error,event
# category: pgsql <------- category: infra, node, pgsql, redis, ...
# annotations: <------- short & detailed information about context
# summary: "FATAL InfraDown {{ $labels.type }}@{{ $labels.instance }}"
# description: |
# pg_up[ins={{ $labels.ins }}, instance={{ $labels.instance }}] = {{ $value }} < 1
################################################################
groups:
################################################################
# Postgres Rules #
################################################################
- name: pgsql-rules
rules:
#==============================================================#
# Reference #
#==============================================================#
# these metrics are scrapped from node_exporter and join on ip
# cpu count for pg instance / cluster
- record: pg:ins:cpu_count
expr: node:ins:cpu_count + on(ip) group_right() pg_up * 0
- record: pg:cls:cpu_count
expr: sum by (job, cls) (pg:ins:cpu_count)
- record: pg:env:cpu_count
expr: sum by (job) (pg:cls:cpu_count)
# cpu usage for node instance
- record: pg:ins:cpu_usage
expr: node:ins:cpu_usage_1m + on(ip) group_right() pg_up * 0
- record: pg:ins:cpu_usage_1m
expr: node:ins:cpu_usage_1m + on(ip) group_right() pg_up * 0
- record: pg:ins:cpu_usage_5m
expr: node:ins:cpu_usage_5m + on(ip) group_right() pg_up * 0
- record: pg:ins:cpu_usage_15m
expr: node:ins:cpu_usage_15m + on(ip) group_right() pg_up * 0
# cpu usage for node cluster
- record: pg:cls:cpu_usage
expr: sum by (job, cls) (pg:ins:cpu_usage * pg:ins:cpu_count) / pg:cls:cpu_count
- record: pg:cls:cpu_usage_1m
expr: sum by (job, cls) (pg:ins:cpu_usage_1m * pg:ins:cpu_count) / pg:cls:cpu_count
- record: pg:cls:cpu_usage_5m
expr: sum by (job, cls) (pg:ins:cpu_usage_5m * pg:ins:cpu_count) / pg:cls:cpu_count
- record: pg:cls:cpu_usage_15m
expr: sum by (job, cls) (pg:ins:cpu_usage_15m * pg:ins:cpu_count) / pg:cls:cpu_count
# cpu usage for entire cpu
- record: pg:env:cpu_usage
expr: sum by (job) (pg:cls:cpu_usage * pg:cls:cpu_count) / pg:env:cpu_count
- record: pg:env:cpu_usage_1m
expr: sum by (job) (pg:cls:cpu_usage_1m * pg:cls:cpu_count) / pg:env:cpu_count
- record: pg:env:cpu_usage_5m
expr: sum by (job) (pg:cls:cpu_usage_5m * pg:cls:cpu_count) / pg:env:cpu_count
- record: pg:env:cpu_usage_15m
expr: sum by (job) (pg:cls:cpu_usage_15m * pg:cls:cpu_count) / pg:env:cpu_count
- record: pg:ins:mem_size
expr: node_memory_MemTotal_bytes + on(ip) group_right() pg_up * 0
- record: pg:ins:fs_size
expr: sum by (ip)(node_filesystem_size_bytes{fstype!~"(n|root|tmp)fs.*"}) + on(ip) group_right() pg_up * 0
- record: pg:ins:space_usage
expr: max by (ip)(node:fs:space_usage)+ on(ip) group_right() pg_up * 0
#==============================================================#
# PGSQL : Identity #
#==============================================================#
# Encoded leadership status: 0:follower[DOWN] 1:leader[DOWN] 2:follower 3:leader
- record: pg:ins:status
expr: pg_up * 2 + (1 - pg_in_recovery)
# True(1) if this instance is leader
- record: pg:ins:is_leader
expr: 1 - pg_in_recovery
# Leader among cluster, multiple record means brain split
- record: pg:cls:leader
expr: (pg_in_recovery == 0) * 0 + 1
#--------------------------------#
# Membership #
#--------------------------------#
# number of scraped targets for one instance, job with pgsql as prefix are counted
- record: pg:ins:target_count
expr: count by (job, cls, ins) (up{cls!="", ins!="", job='pgsql'})
# number of current cluster member
- record: pg:cls:size
expr: sum by (job, cls) (group by (job, cls, ins, ip) (up{cls!="", ins!="", job='pgsql'}))
# number of current cluster member
- record: pg:cls:members
expr: group by (job, cls, ins, ip) (up{cls!="", ins!="", job='pgsql'})
# Bridge instances among cluster, which are non-primary instances with descendant
- record: pg:cls:bridges
expr: count without(appname,usename,address,pid) (pg_repl_lsn) and (pg_in_recovery == 1)
# Number of network partition among this cluster
- record: pg:cls:partition
expr: count by (job, cls) (pg_in_recovery == 0)
# number of alive instance among cluster
- record: pg:cls:alive_count
expr: count by (job, cls) (count by (cls, ins) (pg_up==1))
# Number of leader(primary) among cluster
- record: pg:cls:primary_count
expr: count by (job, cls) (max by (cls, ins) (pg_in_recovery{}) == 0)
# Number of follower(replica) among cluster
- record: pg:cls:replica_count
expr: count by (job, cls) (max by (cls, ins) (pg_in_recovery{}) == 1)
# Number of sync replica (standby) among cluster
- record: pg:cls:standby_count
expr: max by (job, cls) (pg_sync_standby_enabled) > bool 0
# Number of bridge instance among cluster
- record: pg:cls:bridge_count
expr: count by (job, cls) (pg:cls:bridges)
#==============================================================#
# Replication #
#==============================================================#
#--------------------------------#
# Replication Topo #
#--------------------------------#
# wal sender among cluster (job,cls,ins,ip,appname,usename,address,pid,sync_state)
- record: pg:cls:sender
expr: max by (job, cls, ins, ip, appname, usename, address, pid) (pg_repl_sync_state)
# wal receiver among cluster (job,cls,ip,appname,sender_host,sender_port,slot_name,state,tli)
- record: pg:cls:receiver
expr: |
max by ( job, cls, ip, appname, sender_host, sender_port, slot_name, state ) (
label_replace(pg_recv_flush_tli, "appname" , "$1", "ins", "(.*)" )
)
#--------------------------------#
# Timeline #
#--------------------------------#
- record: pg:ins:timeline
expr: pg_checkpoint_tli
- record: pg:cls:timeline
expr: max by (job, cls) (max_over_time(pg_checkpoint_tli{}[30s]))
#--------------------------------#
# Sync State #
#--------------------------------#
# sync status 0-3 for async|potential|sync|quorum, (-1 for unknown)
- record: pg:ins:sync_state
expr: max by (job,cls,ins,instance) (label_replace(pg_repl_sync_state, "ins", "$1", "appname", "(.+)"))
# is this cluster a sync cluster?
- record: pg:cls:sync_state
expr: (max by (job, cls) (pg:ins:sync_state)) > bool 0
#--------------------------------#
# Replication Lag #
#--------------------------------#
# lag in bytes from sender's view (accurate)
- record: pg:ins:lag_bytes
expr: |
max without (address, appname, usename, pid) (
label_replace(pg_repl_replay_diff{appname!~'walreceiver|pg_receivewal'}, "ins", "$1", "appname", "(.+)")
)
- record: pg:cls:lag_bytes
expr: max by (job, cls) (pg:ins:lag_bytes)
- record: pg:env:lag_bytes
expr: max by (job) (pg:cls:lag_bytes)
# lag in seconds from sender's view (roughly accurate, )
- record: pg:ins:lag_seconds
expr: |
max without (address, appname, usename, pid) (
label_replace(pg_repl_replay_lag{appname!~'walreceiver|pg_receivewal'}, "ins", "$1", "appname", "(.+)")
)
- record: pg:cls:lag_seconds
expr: max by (job, cls) (pg:ins:lag_seconds)
- record: pg:env:lag_seconds
expr: max by (job) (pg:cls:lag_seconds)
#--------------------------------#
# Replication Slot #
#--------------------------------#
# cls active slot
- record: pg:cls:slot_count
expr: count by (job, cls) (pg_slot_active > bool 0)
# replication slot retained bytes of single instance
- record: pg:ins:slot_retained_bytes
expr: max without (slot_name) (pg_slot_retained_bytes)
- record: pg:cls:slot_retained_bytes
expr: max by (job, cls) (pg:ins:slot_retained_bytes)
#==============================================================#
# Activity #
#==============================================================#
#--------------------------------#
# Commit/Rollback #
#--------------------------------#
# xact_commit_rate1m is xact_commit rate of last 1m
- record: pg:db:xact_commit_rate1m
expr: rate(pg_db_xact_commit[1m])
- record: pg:ins:xact_commit_rate1m
expr: sum without(datname) (pg:db:xact_commit_rate1m)
- record: pg:cls:xact_commit_rate1m
expr: sum by (job, cls) (pg:ins:xact_commit_rate1m)
- record: pg:env:xact_commit_rate1m
expr: sum by (job) (pg:cls:xact_commit_rate1m)
# xact_commit_rate5m is xact_commit rate of last 5m
- record: pg:db:xact_commit_rate5m
expr: rate(pg_db_xact_commit[5m])
- record: pg:ins:xact_commit_rate5m
expr: sum without(datname) (pg:db:xact_commit_rate5m)
- record: pg:cls:xact_commit_rate5m
expr: sum by (job, cls) (pg:ins:xact_commit_rate5m)
- record: pg:env:xact_commit_rate5m
expr: sum by (job) (pg:cls:xact_commit_rate5m)
# xact_commit_rate15m is xact_commit rate of last 15m
- record: pg:db:xact_commit_rate15m
expr: rate(pg_db_xact_commit[15m])
- record: pg:ins:xact_commit_rate15m
expr: sum without(datname) (pg:db:xact_commit_rate15m)
- record: pg:cls:xact_commit_rate15m
expr: sum by (job, cls) (pg:ins:xact_commit_rate15m)
- record: pg:env:xact_commit_rate15m
expr: sum by (job) (pg:cls:xact_commit_rate15m)
# xact_rollback_rate1m is xact_rollback rate of last 1m
- record: pg:db:xact_rollback_rate1m
expr: rate(pg_db_xact_rollback[1m])
- record: pg:ins:xact_rollback_rate1m
expr: sum without(datname) (pg:db:xact_rollback_rate1m)
- record: pg:cls:xact_rollback_rate1m
expr: sum by (job, cls) (pg:ins:xact_rollback_rate1m)
- record: pg:env:xact_rollback_rate1m
expr: sum by (job) (pg:cls:xact_rollback_rate1m)
# xact_rollback_rate5m is xact_rollback rate of last 5m
- record: pg:db:xact_rollback_rate5m
expr: rate(pg_db_xact_rollback[5m])
- record: pg:ins:xact_rollback_rate5m
expr: sum without(datname) (pg:db:xact_rollback_rate5m)
- record: pg:cls:xact_rollback_rate5m
expr: sum by (job, cls) (pg:ins:xact_rollback_rate5m)
- record: pg:env:xact_rollback_rate5m
expr: sum by (job) (pg:cls:xact_rollback_rate5m)
# xact_rollback_rate15m is xact_rollback rate of last 15m
- record: pg:db:xact_rollback_rate15m
expr: rate(pg_db_xact_rollback[15m])
- record: pg:ins:xact_rollback_rate15m
expr: sum without(datname) (pg:db:xact_rollback_rate15m)
- record: pg:cls:xact_rollback_rate15m
expr: sum by (job, cls) (pg:ins:xact_rollback_rate15m)
- record: pg:env:xact_rollback_rate15m
expr: sum by (job) (pg:cls:xact_rollback_rate15m)
# xact_total_rate1m is xact_total rate of last 1m
- record: pg:db:xact_total_rate1m
expr: rate(pg_db_xact_total[1m])
- record: pg:ins:xact_total_rate1m
expr: sum without(datname) (pg:db:xact_total_rate1m)
- record: pg:cls:xact_total_rate1m
expr: sum by (job, cls) (pg:ins:xact_total_rate1m)
- record: pg:env:xact_total_rate1m
expr: sum by (job) (pg:cls:xact_total_rate1m)
# xact_total_rate5m is xact_total rate of last 5m
- record: pg:db:xact_total_rate5m
expr: rate(pg_db_xact_total[5m])
- record: pg:ins:xact_total_rate5m
expr: sum without(datname) (pg:db:xact_total_rate5m)
- record: pg:cls:xact_total_rate5m
expr: sum by (job, cls) (pg:ins:xacts_total_rate5m)
- record: pg:env:xact_total_rate5m
expr: sum by (job) (pg:cls:xact_total_rate5m)
# xact_total_rate15m is xact_total rate of last 15m
- record: pg:db:xact_total_rate15m
expr: rate(pg_db_xact_total[15m])
- record: pg:ins:xact_total_rate15m
expr: sum without(datname) (pg:db:xact_total_rate15m)
- record: pg:cls:xact_total_rate15m
expr: sum by (job, cls) (pg:ins:xact_total_rate15m)
- record: pg:env:xact_total_rate15m
expr: sum by (job) (pg:cls:xact_total_rate15m)
# xacts σ: sigma
- record: pg:db:xact_total_sigma15m
expr: stddev_over_time(pg:db:xact_total_rate1m[15m])
- record: pg:ins:xact_total_sigma15m
expr: stddev_over_time(pg:ins:xact_total_rate1m[15m])
- record: pg:cls:xact_total_sigma15m
expr: stddev_over_time(pg:cls:xact_total_rate1m[15m])
- record: pg:env:xact_total_sigma15m
expr: stddev_over_time(pg:env:xact_total_rate1m[15m])
#--------------------------------#
# Locks #
#--------------------------------#
# lock count by mode
# - record: pg:db:locks
# expr: pg_lock_count
- record: pg:ins:locks
expr: sum without (datname) (pg_lock_count)
- record: pg:cls:locks
expr: sum by (job, cls, mode) (pg:ins:locks)
# total lock count
- record: pg:db:lock_count
expr: sum without (mode) (pg_lock_count)
- record: pg:ins:lock_count
expr: sum without(mode) (pg:ins:locks)
- record: pg:cls:lock_count
expr: sum without(mode) (pg:cls:locks)
# read category lock
- record: pg:db:rlock_count
expr: sum without (mode) (pg_lock_count{mode="AccessShareLock"})
- record: pg:ins:rlock_count
expr: sum without(datname) (pg:db:rlock_count)
- record: pg:cls:rlock_count
expr: sum by (job, cls) (pg:ins:rlock_count)
# write category lock (insert|update|delete)
- record: pg:db:wlock_count
expr: sum without (mode) (pg_lock_count{mode=~"RowShareLock|RowExclusiveLock"})
- record: pg:ins:wlock_count
expr: sum without(datname) (pg:db:wlock_count)
- record: pg:cls:wlock_count
expr: sum by (job, cls) (pg:ins:wlock)
# exclusive category lock
- record: pg:db:xlock_count
expr: sum without (mode) (pg_lock_count{mode!~"AccessShareLock|RowShareLock|RowExclusiveLock"})
# ALTERNATIVE: expr: sum without (mode) (pg_lock_count{mode=~"AccessExclusiveLock|ExclusiveLock|ShareRowExclusiveLock|ShareLock|ShareUpdateExclusiveLock"})
- record: pg:ins:xlock_count
expr: sum without(datname) (pg:db:xlock_count)
- record: pg:cls:xlock_count
expr: sum by (job, cls) (pg:ins:xlock_count)
#==============================================================#
# PG14 New Database Metrics #
#==============================================================#
#--------------------------------#
# Session/Idle Time #
#--------------------------------#
# Time spent by database sessions per second, in seconds (ms->s)
- record: pg:db:session_time_rate1m
expr: rate(pg_db_session_time[1m])
- record: pg:ins:session_time_rate1m
expr: sum without(datname) (pg:db:session_time_rate1m)
- record: pg:cls:session_time_rate1m
expr: sum by (job, cls) (pg:ins:session_time_rate1m)
- record: pg:env:session_time_rate1m
expr: sum by (job) (pg:cls:session_time_rate1m)
# Time spent in idle per second, in seconds
- record: pg:db:ixact_time_rate1m
expr: rate(pg_db_ixact_time[1m])
- record: pg:ins:ixact_time_rate1m
expr: sum without(datname) (pg:db:ixact_time_rate1m)
- record: pg:cls:ixact_time_rate1m
expr: sum by (job, cls) (pg:ins:ixact_time_rate1m)
- record: pg:env:ixact_time_rate1m
expr: sum by (job) (pg:cls:ixact_time_rate1m)
#--------------------------------#
# Active Time #
#--------------------------------#
# Time rate spent executing SQL statements in this database, in seconds
# active_time_rate1m
- record: pg:db:active_time_rate1m
expr: rate(pg_db_active_time[1m])
- record: pg:ins:active_time_rate1m
expr: sum without(datname) (pg:db:active_time_rate1m)
- record: pg:cls:active_time_rate1m
expr: sum by (job, cls) (pg:ins:active_time_rate1m)
- record: pg:env:active_time_rate1m
expr: sum by (job) (pg:cls:active_time_rate1m)
# active_time_rate5m
- record: pg:db:active_time_rate5m
expr: rate(pg_db_active_time[5m])
- record: pg:ins:active_time_rate5m
expr: sum without(datname) (pg:db:active_time_rate5m)
- record: pg:cls:active_time_rate5m
expr: sum by (job, cls) (pg:ins:active_time_rate5m)
- record: pg:env:active_time_rate5m
expr: sum by (job) (pg:cls:active_time_rate5m)
# active_time_rate1m
- record: pg:db:active_time_rate15m
expr: rate(pg_db_active_time[15m])
- record: pg:ins:active_time_rate15m
expr: sum without(datname) (pg:db:active_time_rate15m)
- record: pg:cls:active_time_rate15m
expr: sum by (job, cls) (pg:ins:active_time_rate15m)
- record: pg:env:active_time_rate15m
expr: sum by (job) (pg:cls:active_time_rate15m)
#--------------------------------#
# PG Load #
#--------------------------------#
# instance level pg load 0,1,5,15
# it will be -1 if not applicable
- record: pg:ins:load1
expr: pg:ins:active_time_rate1m / pg:ins:cpu_count or (0 * pg_up - 1)
- record: pg:ins:load5
expr: pg:ins:active_time_rate5m / pg:ins:cpu_count or (0 * pg_up - 1)
- record: pg:ins:load15
expr: pg:ins:active_time_rate15m / pg:ins:cpu_count or (0 * pg_up - 1)
# cluster level pg load: 0,1,5,15
- record: pg:cls:load1
expr: pg:cls:active_time_rate1m / pg:cls:cpu_count or (0 * pg:cls:size - 1)
- record: pg:cls:load5
expr: pg:cls:active_time_rate5m / pg:cls:cpu_count or (0 * pg:cls:size - 1)
- record: pg:cls:load15
expr: pg:cls:active_time_rate15m / pg:cls:cpu_count or (0 * pg:cls:size - 1)
#==============================================================#
# Session #
#==============================================================#
#--------------------------------#
# Postgres Backend #
#--------------------------------#
# all backends
- record: pg:db:num_backends
expr: pg_db_numbackends
- record: pg:ins:num_backends
expr: sum without (datname) (pg:db:num_backends)
- record: pg:cls:num_backends
expr: sum by (job, cls) (pg:ins:num_backends)
# active backend
- record: pg:db:active_backends
expr: sum without(state) (pg_activity_count{state="active"})
- record: pg:ins:active_backends
expr: sum without(datname) (pg:db:active_backends)
- record: pg:cls:active_backends
expr: sum by (job, cls) (pg:ins:active_backends)
- record: pg:env:active_backends
expr: sum by (job) (pg:cls:active_backends)
# idle in transaction backend (abort included)
- record: pg:db:ixact_backends
expr: sum without(state) (pg_activity_count{state=~"idle in.*"})
- record: pg:ins:ixact_backends
expr: sum without(datname) (pg:db:ixact_backends)
- record: pg:cls:ixact_backends
expr: sum by (job, cls) (pg:ins:ixact_backends)
- record: pg:env:ixact_backends
expr: sum by (job) (pg:cls:ixact_backends)
#--------------------------------#
# Connection Limit #
#--------------------------------#
# instance connection limit (by max_connection)
- record: pg:ins:conn_limit
expr: pg_setting_max_connections
# database connection limit (by DDL) (if not set, instance max_connections are used)
- record: pg:db:conn_limit
expr: (pg_db_conn_limit != -1 ) or (0 * pg_db_conn_limit + on(cls, ins) group_left pg_setting_max_connections)
#--------------------------------#
# Connection Usage #
#--------------------------------#
# instance connection usage
- record: pg:ins:conn_usage
expr: pg:ins:num_backends / pg:ins:conn_limit
# database connection usage
- record: pg:db:conn_usage
expr: pg:db:num_backends / pg:db:conn_limit
#==============================================================#
# Database #
#==============================================================#
#--------------------------------#
# Age #
#--------------------------------#
# pg database age usage
- record: pg:db:age
expr: pg_db_age / 2147483647
- record: pg:ins:age
expr: max without (datname) (pg:db:age{datname!~"template[0-9]"})
- record: pg:cls:age
expr: max by (job, cls) (pg:ins:age)
- record: pg:env:age
expr: max by (job) (pg:cls:age)
# age derive in last 1h
- record: pg:db:age_deriv1h
expr: deriv(pg_db_age[1h])
# age exhaust time in seconds (according to last1h)
- record: pg:db:age_exhaust
expr: (2147483648 - pg_db_age) / pg:db:age_deriv1h
#--------------------------------#
# CRUD #
#--------------------------------#
# tup fetched are 1m rate of tuple been accessed
- record: pg:db:tup_fetched_rate1m
expr: rate(pg_db_tup_fetched[1m])
- record: pg:ins:tup_fetched_rate1m
expr: sum without(datname) (pg:db:tup_fetched_rate1m)
- record: pg:cls:tup_fetched_rate1m
expr: sum by (job, cls) (pg:ins:tup_fetched_rate1m)
- record: pg:env:tup_fetched_rate1m
expr: sum by (job) (pg:cls:tup_fetched_rate1m)
# tup returned are 1m rate of tuple been returned
- record: pg:db:tup_returned_rate1m
expr: rate(pg_db_tup_returned[1m])
- record: pg:ins:tup_returned_rate1m
expr: sum without(datname) (pg:db:tup_returned_rate1m)
- record: pg:cls:tup_returned_rate1m
expr: sum by (job, cls) (pg:ins:tup_returned_rate1m)
- record: pg:env:tup_returned_rate1m
expr: sum by (job) (pg:cls:tup_returned_rate1m)
# inserted
- record: pg:db:tup_inserted_rate1m
expr: rate(pg_db_tup_inserted[1m])
- record: pg:ins:tup_inserted_rate1m
expr: sum without(datname) (pg:db:tup_inserted_rate1m)
- record: pg:cls:tup_inserted_rate1m
expr: sum by (job, cls) (pg:ins:tup_inserted_rate1m)
- record: pg:env:tup_inserted_rate1m
expr: sum by (job) (pg:cls:tup_inserted_rate1m)
# updated
- record: pg:db:tup_updated_rate1m
expr: pg:db:tup_updated_rate1m
- record: pg:ins:tup_updated_rate1m
expr: sum without(datname) (pg:db:tup_updated_rate1m)
- record: pg:cls:tup_updated_rate1m
expr: sum by (job, cls) (pg:ins:tup_updated_rate1m)
- record: pg:env:tup_updated_rate1m
expr: sum by (job) (pg:cls:tup_updated_rate1m)
# deleted
- record: pg:db:tup_deleted_rate1m
expr: rate(pg_db_tup_deleted[1m])
- record: pg:ins:tup_deleted_rate1m
expr: sum without(datname) (pg:db:tup_deleted_rate1m)
- record: pg:cls:tup_deleted_rate1m
expr: sum by (job, cls) (pg:ins:tup_deleted_rate1m)
- record: pg:env:tup_deleted_rate1m
expr: sum by (job) (pg:cls:tup_deleted_rate1m)
# modified
- record: pg:db:tup_modified_rate1m
expr: rate(pg_db_tup_modified[1m])
- record: pg:ins:tup_modified_rate1m
expr: sum without(datname) (pg:db:tup_modified_rate1m)
- record: pg:cls:tup_modified_rate1m
expr: sum by (job, cls) (pg:ins:tup_modified_rate1m)
- record: pg:env:tup_modified_rate1m
expr: sum by (job) (pg:cls:tup_modified_rate1m)
#==============================================================#
# Persist #
#==============================================================#
#--------------------------------#
# Temp File #
#--------------------------------#
# temp file generated in last 1m in bytes
- record: pg:db:temp_bytes_rate1m
expr: rate(pg_db_temp_bytes[1m])
- record: pg:ins:temp_bytes
expr: sum without (datname) (pg:db:temp_bytes)
- record: pg:cls:temp_bytes
expr: sum by (job, cls) (pg:ins:temp_bytes)
# temp file count generated in last 1m
- record: pg:db:temp_files_1m
expr: increase(pg_db_temp_files[1m])
- record: pg:ins:temp_files_1m
expr: sum without(datname) (pg:db:temp_files)
- record: pg:cls:temp_files_1m
expr: sum by (job, cls) (pg:ins:temp_files)
#--------------------------------#
# File Size #
#--------------------------------#
# database size
- record: pg:db:db_size
expr: pg_size_bytes{datname!~"wal|log"}
- record: pg:ins:db_size
expr: sum without (datname) (pg:db:db_size)
- record: pg:cls:db_size
expr: sum by (job, cls) (pg:ins:db_size)
# wal size
- record: pg:ins:wal_size
expr: sum without (datname) (pg_size_bytes{datname="wal"})
- record: pg:cls:wal_size
expr: sum by (job, cls) (pg:ins:wal_size)
# log size
- record: pg:ins:log_size
expr: sum without (datname) (pg_size_bytes{datname="log"})
- record: pg:cls:log_size
expr: sum by (job, cls) (pg:ins:log_size)
# db + wal + log size
- record: pg:ins:file_size
expr: sum without(datname) (pg_size_bytes)
- record: pg:cls:file_size
expr: sum by (job, cls) (pg:ins:file_size)
#--------------------------------#
# Disk/Mem Blocks #
#--------------------------------#
# blocks read in last 1m
- record: pg:db:blks_read_1m
expr: increase(pg_db_blks_read[1m])
# blocks hit in last 1m
- record: pg:db:blks_hit_1m
expr: increase(pg_db_blks_hit[1m])
# blocks access (hit + read) in last 1m
- record: pg:db:blks_access_1m
expr: increase(pg_db_blks_access[1m])
# blocks hit rate in (1m)
- record: pg:db:blks_hit_ratio1m
expr: pg:db:blks_hit_1m / pg:db:blks_access_1m
- record: pg:ins:blks_hit_ratio1m
expr: sum without (datname) (pg:db:blks_hit_1m) / sum without (datname) (pg:db:blks_access_1m)
# block read time spent
- record: pg:db:blk_read_time_seconds_rate1m
expr: rate(pg_db_blk_read_time[1m])
# block write time spent
- record: pg:db:blk_write_time_seconds_rate1m
expr: rate(pg_db_blk_write_time[1m])
# block io (read + write) time spent
- record: pg:db:blk_io_time_seconds_rate1m
expr: pg:db:blk_read_time_seconds_rate1m + pg:db:blk_write_time_seconds_rate1m
#--------------------------------#
# Shared Buffer #
#--------------------------------#
# buffer flushed by checkpoint (good)
- record: pg:ins:buf_flush_checkpoint_rate1m
expr: rate(pg_bgwriter_buffers_checkpoint[1m])
- record: pg:cls:buf_flush_checkpoint_rate1m
expr: sum by (job, cls) (pg:ins:buf_flush_checkpoint_rate1m)
# buffer flushed by backend (bad)
- record: pg:ins:buf_flush_backend_rate1m
expr: rate(pg_bgwriter_buffers_backend[1m])
- record: pg:cls:buf_flush_backend_rate1m
expr: sum by (job, cls) (pg:ins:buf_flush_backend_rate1m)
# buffer cleaned
- record: pg:ins:buf_clean_rate1m
expr: rate(pg_bgwriter_buffers_clean[1m])
- record: pg:cls:buf_clean_rate1m
expr: sum by (job, cls) (pg:ins:buf_clean_rate1m)
# buffer flushed (checkpoint + backend) ins,cls,env
- record: pg:ins:buf_write_rate1m
expr: pg:ins:buf_flush_backend + pg:ins:buf_flush_checkpoint + pg:ins:buf_clean_rate1m
- record: pg:cls:buf_write_rate1m
expr: sum by (job, cls) (pg:ins:buf_flush_rate1m)
# buffer allocated
- record: pg:ins:buf_alloc_rate1m
expr: rate(pg_bgwriter_buffers_alloc[1m])
- record: pg:cls:buf_alloc_rate1m
expr: sum by (job, cls) (pg:ins:buf_alloc_rate1m)
#--------------------------------#
# Checkpoint #
#--------------------------------#
# checkpoint stats
# True(1) if scheduled checkpoint occurs on last 1 minute
- record: pg:ins:ckpt_timed_1m
expr: increase(pg_bgwriter_checkpoints_timed[1m])
# True(1) if requested checkpoint occurs on last 1 minute
- record: pg:ins:ckpt_req_1m
expr: increase(pg_bgwriter_checkpoints_req[1m])
# Number of checkpoint happened in last 1 hour
- record: pg:ins:ckpt_1h
expr: increase(pg_bgwriter_checkpoints_timed[1h]) + increase(pg_bgwriter_checkpoints_req[1h])
# 1m wal rate of single instance
- record: pg:ins:lsn_rate1m
expr: rate(pg_lsn[1m])
# 1m wal rate of a cluster
- record: pg:cls:lsn_rate1m
expr: sum by (job, cls) (pg:ins:lsn_rate1m * on(ins) group_left() (1 - pg_in_recovery))
# 1m wal rate of entire environment
- record: pg:env:lsn_rate1m
expr: sum by (job) (pg:cls:lsn_rate1m)
#==============================================================#
# Object Access #
#==============================================================#
# table scan
- record: pg:table:scan_rate1m
expr: rate(pg_table_tbl_scan[1m])
# query cenv rate during last 1m
- record: pg:query:call_rate1m
expr: rate(pg_query_calls[1m])
# query response time during last 1m
- record: pg:query:rt_1m
expr: rate(pg_query_exec_time[1m]) / pg:query:call_rate1m
# index scan
# - record: pg:index:scan_rate1m
# expr: rate(pg_index_idx_scan{}[1m])
################################################################
# Pgbouncer Rules #
################################################################
- name: pgbouncer-rules
rules:
################################################################
# Pgbouncer #
################################################################
#==============================================================#
# List #
#==============================================================#
# object list
- record: pgbouncer:ins:pools
expr: sum without (list) (pgbouncer_list_items{list="pools"})
- record: pgbouncer:ins:pool_databases
expr: sum without (list) (pgbouncer_list_items{list="databases"})
- record: pgbouncer:ins:pool_users
expr: sum without (list) (pgbouncer_list_items{list="users"})
- record: pgbouncer:ins:login_clients
expr: sum without (list) (pgbouncer_list_items{list="login_clients"})
- record: pgbouncer:ins:free_clients
expr: sum without (list) (pgbouncer_list_items{list="free_clients"})
- record: pgbouncer:ins:used_clients
expr: sum without (list) (pgbouncer_list_items{list="used_clients"})
- record: pgbouncer:ins:free_servers
expr: sum without (list) (pgbouncer_list_items{list="free_servers"})
#==============================================================#
# Database #
#==============================================================#
- record: pgbouncer:db:pool_max_conn
expr: pgbouncer_database_pool_size{datname!="pgbouncer"} + pgbouncer_database_reserve_pool{datname!="pgbouncer"}
- record: pgbouncer:db:pool_size
expr: pgbouncer_database_pool_size{datname!="pgbouncer"}
- record: pgbouncer:db:pool_reserve_size
expr: pgbouncer_database_reserve_pool{datname!="pgbouncer"}
- record: pgbouncer:db:pool_current_conn
expr: pgbouncer_database_current_connections{datname!="pgbouncer"}
- record: pgbouncer:db:pool_paused
expr: pgbouncer_database_paused{datname!="pgbouncer"}
- record: pgbouncer:db:pool_disabled
expr: pgbouncer_database_disabled{datname!="pgbouncer"}
# pgbouncer database connection usage
- record: pgbouncer:db:conn_usage
expr: pgbouncer:db:pool_current_conn / pgbouncer:db:pool_size
# pgbouncer database connection usage (with additional reserved conn)
- record: pgbouncer:db:conn_usage_reserve
expr: pgbouncer:db:pool_current_conn / pgbouncer:db:pool_max_conn
#==============================================================#
# Pool #
#==============================================================#
# 5 different level: pool, db, ins, cls, env
# level 0: pool {cls,ins,datname,user} # atomic level
# level 1: db {cls,ins,datname} # agg without (user) (pool)
# level 2: ins {cls,ins} # agg without (datname) (db)
# level 3: cls {cls} # agg by (job, cls) (ins)
# level 4: env {} # agg (cls)
# max wait time (in µs, transfer to seconds)
- record: pgbouncer:pool:maxwait
expr: pgbouncer_pool_maxwait{datname!="pgbouncer"} + pgbouncer_pool_maxwait_us{datname!="pgbouncer"} / 1000000
- record: pgbouncer:db:maxwait
expr: max without(user, pool_mode) (pgbouncer:pool:maxwait)
- record: pgbouncer:ins:maxwait
expr: max without(datname) (pgbouncer:db:maxwait)
- record: pgbouncer:cls:maxwait
expr: max by (job, cls) (pgbouncer:ins:maxwait)
- record: pgbouncer:env:maxwait
expr: max by (job) (pgbouncer:cls:maxwait)
# active servers
- record: pgbouncer:pool:active_servers
expr: pgbouncer_pool_active_servers{datname!="pgbouncer"}
- record: pgbouncer:db:active_servers
expr: sum without(user, pool_mode) (pgbouncer:pool:active_servers)
- record: pgbouncer:ins:active_servers
expr: sum without(datname) (pgbouncer:db:active_servers)
- record: pgbouncer:cls:active_servers
expr: sum by (job, cls) (pgbouncer:ins:active_servers)
- record: pgbouncer:env:active_servers
expr: sum by (job) (pgbouncer:cls:active_servers)
# idle servers
- record: pgbouncer:pool:idle_servers
expr: pgbouncer_pool_idle_servers{datname!="pgbouncer"}
- record: pgbouncer:db:idle_servers
expr: max without(user, pool_mode) (pgbouncer:pool:idle_servers)
- record: pgbouncer:ins:idle_servers
expr: sum without(datname) (pgbouncer:db:idle_servers)
- record: pgbouncer:cls:idle_servers
expr: sum by (job, cls) (pgbouncer:ins:idle_servers)
- record: pgbouncer:env:idle_servers
expr: sum by (job) (pgbouncer:cls:idle_servers)
# used servers
- record: pgbouncer:pool:used_servers
expr: pgbouncer_pool_used_servers{datname!="pgbouncer"}
- record: pgbouncer:db:used_servers
expr: max without(user, pool_mode) (pgbouncer:pool:used_servers)
- record: pgbouncer:ins:used_servers
expr: sum without(datname) (pgbouncer:db:used_servers)
- record: pgbouncer:cls:used_servers
expr: sum by (job, cls) (pgbouncer:ins:used_servers)
- record: pgbouncer:env:used_servers
expr: sum by (job) (pgbouncer:cls:used_servers)
# tested servers
- record: pgbouncer:pool:tested_servers
expr: pgbouncer_pool_tested_servers{datname!="pgbouncer"}
- record: pgbouncer:db:tested_servers
expr: max without(user, pool_mode) (pgbouncer:pool:tested_servers)
- record: pgbouncer:ins:tested_servers
expr: sum without(datname) (pgbouncer:db:tested_servers)
- record: pgbouncer:cls:tested_servers
expr: sum by (job, cls) (pgbouncer:ins:tested_servers)
- record: pgbouncer:env:tested_servers
expr: sum by (job) (pgbouncer:cls:tested_servers)
# login servers
- record: pgbouncer:pool:login_servers
expr: pgbouncer_pool_login_servers{datname!="pgbouncer"}
- record: pgbouncer:db:login_servers
expr: max without(user, pool_mode) (pgbouncer:pool:login_servers)
- record: pgbouncer:ins:login_servers
expr: sum without(datname) (pgbouncer:db:login_servers)
- record: pgbouncer:cls:login_servers
expr: sum by (job, cls) (pgbouncer:ins:login_servers)
- record: pgbouncer:env:login_servers
expr: sum by (job) (pgbouncer:cls:login_servers)
# active clients
- record: pgbouncer:pool:active_clients
expr: pgbouncer_pool_active_clients{datname!="pgbouncer"}
- record: pgbouncer:db:active_clients
expr: max without(user, pool_mode) (pgbouncer:pool:active_clients)
- record: pgbouncer:ins:active_clients
expr: sum without(datname) (pgbouncer:db:active_clients)
- record: pgbouncer:cls:active_clients
expr: sum by (job, cls) (pgbouncer:ins:active_clients)
- record: pgbouncer:env:active_clients
expr: sum by (job) (pgbouncer:cls:active_clients)
# waiting clients
- record: pgbouncer:pool:waiting_clients
expr: pgbouncer_pool_waiting_clients{datname!="pgbouncer"}
- record: pgbouncer:db:waiting_clients
expr: max without(user, pool_mode) (pgbouncer:pool:waiting_clients)
- record: pgbouncer:ins:waiting_clients
expr: sum without(datname) (pgbouncer:db:waiting_clients)
- record: pgbouncer:cls:waiting_clients
expr: sum by (job, cls) (pgbouncer:ins:waiting_clients)
- record: pgbouncer:env:waiting_clients
expr: sum by (job) (pgbouncer:cls:waiting_clients)
#==============================================================#
# Stat #
#==============================================================#
# transmit bandwidth (sent, out) (db,ins,cls,env)
- record: pgbouncer:db:tx_rate1m