Skip to content

Commit 5137a38

Browse files
authored
Add a regexp to filter slot (#7)
1 parent fe53be9 commit 5137a38

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

main.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import sleep
55
from slack_sdk import WebClient
66
from slack_sdk.errors import SlackApiError
7+
import re
78

89
# PostgreSQL query to get replication slot size
910
REPLICATION_SLOT_QUERY = "SELECT slot_name, pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn) AS replication_lag_bytes FROM pg_replication_slots;"
@@ -48,6 +49,11 @@ def post_message_to_slack(channel, message):
4849
@click.option(
4950
"--interval-seconds", default=60, help="Interval in seconds between each check."
5051
)
52+
@click.option(
53+
"--slot-filter-regexp",
54+
default=".*",
55+
help="Regular expression to filter slots by name.",
56+
)
5157
def main(
5258
db_host,
5359
db_port,
@@ -57,18 +63,34 @@ def main(
5763
slack_channel,
5864
interval_seconds,
5965
size_threshold_mb,
66+
slot_filter_regexp,
6067
):
68+
compiled_slot_filter_regexp = re.compile(slot_filter_regexp)
6169
while True:
62-
with psycopg2.connect(host=db_host, port=db_port, user=db_user, password=db_password, dbname=db_name) as conn:
70+
with psycopg2.connect(
71+
host=db_host,
72+
port=db_port,
73+
user=db_user,
74+
password=db_password,
75+
dbname=db_name,
76+
) as conn:
6377
print(f"Connected to database '{db_name}' on '{db_host}'")
6478
try:
65-
slots = query_replication_slot_size(conn)
79+
slots = query_replication_slot_size(conn)
6680
except psycopg2.Error as e:
67-
post_message_to_slack(slack_channel, f"🔥 [{deployment_name}] Error querying replication slots: {e}")
81+
post_message_to_slack(
82+
slack_channel,
83+
f"🔥 [{deployment_name}] Error querying replication slots: {e},
84+
")
6885
continue
6986
for slot_name, size in slots:
87+
if not compiled_slot_filter_regexp.match(slot_name):
88+
continue
7089
if size is None:
71-
post_message_to_slack(slack_channel, f"⚠️ [{deployment_name}] Replication slot '{slot_name}' size is NULL.")
90+
post_message_to_slack(
91+
slack_channel,
92+
f"⚠️ [{deployment_name}] Replication slot '{slot_name}' size is NULL.",
93+
)
7294
continue
7395
size_mb = size / 1024 / 1024
7496
if size_mb > size_threshold_mb:

run.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ exec python ./main.py \
1313
--db-name="${DB_NAME}" \
1414
--slack-channel="${SLACK_CHANNEL}" \
1515
--interval-seconds=${INTERVAL_SECONDS} \
16-
--size-threshold-mb=${SIZE_THRESHOLD_MB}
16+
--size-threshold-mb=${SIZE_THRESHOLD_MB} \
17+
--slot-filter-regexp="${SLOT_FILTER_REGEXP}"

run_docker.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ python ./main.py \
66
--db-name="${DB_NAME}" \
77
--slack-channel="${SLACK_CHANNEL}" \
88
--interval-seconds=${INTERVAL_SECONDS} \
9-
--size-threshold-mb=${SIZE_THRESHOLD_MB}
9+
--size-threshold-mb=${SIZE_THRESHOLD_MB} \
10+
--slot-filter-regexp="${SLOT_FILTER_REGEXP}"

0 commit comments

Comments
 (0)