From 25c2887b5aac8a2540abef7692fec50221ee0c27 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 18 May 2024 12:05:05 +0200 Subject: [PATCH] feat: Track local users by appservice ID Introduces a new metric `synapse_local_users_appservice` to count local users by their associated appservice ID, enhancing the granularity of user analytics within the Synapse Prometheus exporter. This update involves modifications to the changelog and README to document the addition, and updates to the main Python script to implement the collection and reporting of this new metric. The change aims to provide administrators and developers with better insights into appservice usage and user distribution, facilitating improved resource allocation and service optimization. --- CHANGELOG.md | 8 +++++++- README.md | 1 + src/synapse_prometheus_exporter/__main__.py | 22 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a138261..3ad9134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog -## v0.0.1 +## v0.0.2 (2024-05-18) + +### Added + +- Added `synapse_local_users_appservice` metric to count local users by appservice ID + +## v0.0.1 (2024-05-18) ### Added diff --git a/README.md b/README.md index 53d2bdb..d79d333 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The exporter currently provides the following metrics: - `synapse_local_users_state`: Number of local users by state (active, disabled) - `synapse_local_users_type`: Number of local users by type (guest, user, appservice) - `synapse_local_users_moderation`: Number of local users by moderation status (active, shadow_banned, locked) +- `synapse_local_users_appservice`: Number of local users by appservice ID - `synapse_total_devices`: Total number of devices - `synapse_total_rooms`: Total number of rooms - `synapse_total_events`: Total number of events diff --git a/src/synapse_prometheus_exporter/__main__.py b/src/synapse_prometheus_exporter/__main__.py index 7a58830..a4de1f9 100644 --- a/src/synapse_prometheus_exporter/__main__.py +++ b/src/synapse_prometheus_exporter/__main__.py @@ -24,6 +24,13 @@ LOCAL_USERS_MODERATION = Gauge( ["status", "host"], ) +# Gauge for appservice users by appservice ID +LOCAL_USERS_APPSERVICE = Gauge( + "synapse_local_users_appservice", + "Number of local users by appservice ID", + ["appservice_id", "host"], +) + # Gauge for total number of devices, rooms, and events TOTAL_DEVICES = Gauge( "synapse_total_devices", "Total number of devices in the database", ["host"] @@ -81,11 +88,19 @@ def get_synapse_stats(db_config): "appservice": sum([1 for user in local_users if user[1]]), } local_users_moderation = { - "active": sum([1 for user in local_users if user[4] is False and user[5] is False]), + "active": sum( + [1 for user in local_users if user[4] is False and user[5] is False] + ), "shadow_banned": sum([1 for user in local_users if user[4] is True]), "locked": sum([1 for user in local_users if user[5] is True]), } + local_users_appservice = {} + for user in local_users: + if user[1]: + local_users_appservice[user[1]] = ( + local_users_appservice.get(user[1], 0) + 1 + ) LOCAL_USERS_STATE.labels(state="active", host=host_identifier).set( local_users_state["active"] @@ -114,6 +129,11 @@ def get_synapse_stats(db_config): local_users_moderation["locked"] ) + for appservice_id, count in local_users_appservice.items(): + LOCAL_USERS_APPSERVICE.labels( + appservice_id=appservice_id, host=host_identifier + ).set(count) + # Get the total number of devices, rooms, and events cursor.execute( """