Skip to content

Commit

Permalink
Add monitoring for app connections to the db (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
acasajus authored Sep 27, 2024
1 parent 20c1145 commit df4c528
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion events/event_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def __listen(self, on_event: Callable[[SyncEvent], NoReturn]):
Session.close() # Ensure we get a new connection and we don't leave a dangling tx

def __connect(self):
self.__connection = psycopg2.connect(self.__connection_string)
self.__connection = psycopg2.connect(
self.__connection_string, application_name="sl-event-listen"
)

from app.db import Session

Expand Down
15 changes: 15 additions & 0 deletions monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ def log_nb_db_connection():
newrelic.agent.record_custom_metric("Custom/nb_db_connections", nb_connection)


@newrelic.agent.background_task()
def log_nb_db_connection_by_app_name():
# get the number of connections to the DB
rows = Session.execute(
"SELECT application_name, count(datid) FROM pg_stat_activity group by application_name"
)
for row in rows:
if row[0].find("sl-") == 0:
LOG.d("number of db connections for app %s = %s", row[0], row[1])
newrelic.agent.record_custom_metric(
f"Custom/nb_db_app_connection/{row[0]}", row[1]
)


@newrelic.agent.background_task()
def log_pending_to_process_events():
r = Session.execute("select count(*) from sync_event WHERE taken_time IS NULL;")
Expand Down Expand Up @@ -148,6 +162,7 @@ def log_failed_events():
log_pending_to_process_events()
log_events_pending_dead_letter()
log_failed_events()
log_nb_db_connection_by_app_name()
Session.close()

exporter.run()
Expand Down

0 comments on commit df4c528

Please sign in to comment.