Skip to content

feat: add room count metric#72

Merged
itsoyou merged 1 commit into
masterfrom
syk/add-room-count-metric
Oct 7, 2025
Merged

feat: add room count metric#72
itsoyou merged 1 commit into
masterfrom
syk/add-room-count-metric

Conversation

@itsoyou

@itsoyou itsoyou commented Jul 25, 2025

Copy link
Copy Markdown
Member

Expose room count as well as locally joined room count in metrics #3242

This is revised version of the previous pr to align more consistent coding style.

  • synapse_rooms_total: Rooms currently stored in the database, for example like the query select count(*) from rooms;, but there should already be a helper function available in Synapse.
  • synapse_locally_joined_rooms_total: Rooms with at least one local member. Should be similar to this query: select count(*) from room_stats_current where local_users_in_room > 0;

If you have any other better approach please feel free to share it here 🥰

@itsoyou
itsoyou requested a review from a team as a code owner July 25, 2025 12:39
Comment thread synapse/storage/databases/main/room.py Outdated
Comment thread synapse/storage/databases/main/room.py Outdated
Comment thread synapse/storage/databases/main/room.py Outdated
Comment thread synapse/storage/databases/main/metrics.py Outdated
Comment thread tests/storage/test_room.py Outdated
Comment thread tests/storage/test_room.py Outdated
Comment thread tests/storage/databases/main/test_metrics.py Outdated
@jason-famedly

Copy link
Copy Markdown
Member

All in all, this looks really cool. I think I will pull the patch and apply it to my server to see what it looks like. I think we get database transaction metrics for free too(maybe, will check if I do patch my server with it)

Could you work up a sample query of what the prometheus query would look like? Would then be helpful for infra when they need to add it to the grafana metrics(and I can cheat and use it too, because I'm lazy 😉 )

@jason-famedly

Copy link
Copy Markdown
Member

Also looks like the one test fails on trial: https://github.com/famedly/synapse/actions/runs/16568291444

the other complement and sytest failures are bogus, ignore them

@itsoyou

itsoyou commented Jul 29, 2025

Copy link
Copy Markdown
Member Author

All in all, this looks really cool. I think I will pull the patch and apply it to my server to see what it looks like. I think we get database transaction metrics for free too(maybe, will check if I do patch my server with it)

Could you work up a sample query of what the prometheus query would look like? Would then be helpful for infra when they need to add it to the grafana metrics(and I can cheat and use it too, because I'm lazy 😉 )

you can find metrics with names synapse_known_rooms_total, synapse_locally_joined_rooms_total
I think you can also try synapse_known_rooms_total[24h] and for graph, try avg_over_time(synapse_known_rooms_total[1h]) something like this

@itsoyou
itsoyou requested a review from jason-famedly July 29, 2025 16:26
Comment thread tests/storage/databases/main/test_metrics.py Outdated
Comment thread synapse/storage/databases/main/room.py Outdated
sql = """
SELECT count(*) FROM room_stats_current
WHERE local_users_in_room > 0
"""

@itsoyou itsoyou Aug 1, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check how often room_stats_current this table gets updated!
Maybe update the metric at the spot where the table get updated...

@itsoyou itsoyou Aug 5, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I met Nico on Friday, we talked about it and came up with this:

  • Check how often room_stats_current this table gets updated!
  • Maybe update the metric at the spot where the table get updated.
  • count(*) on room/room_stats_current table wouldn’t be so expensive, unless it has millions of rows. But it would be nice if we have index on local_users_in_room

So I looked into where the room_stats_current table being updated. Mainly 2 places,

  1. Room purging https://github.com/famedly/synapse/blob/master/synapse/storage/databases/main/purge_events.py#L334
  2. update stats (background update) https://github.com/famedly/synapse/blob/master/synapse/storage/databases/main/stats.py#L135-L137

But I am still on the side that it’s better to have periodic update, since it is simpler to manage, and this is not a time-critical metrics.

And since the room count queries are not super expensive, I'd like to have it updated more often than an hour. around every 15 minutes.

This is another good place to manage all this metrics. Here they make metrics shared between the phone home stats and the prometheus exporter.
https://github.com/famedly/synapse/blob/master/synapse/metrics/common_usage_metrics.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await self.store.bulk_update_stats_delta(
is called on every new event. So I think we could hook into that update function to update our metrics, at least the room count. The other one is a bit more tricky, but should work as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event_processing_positions.labels("stats").set(max_pos)
already does that for another metric, imo.

@itsoyou
itsoyou force-pushed the syk/add-room-count-metric branch from 6e65f57 to 88bd92b Compare August 5, 2025 09:11
Comment thread synapse/storage/databases/main/metrics.py Outdated
Comment thread synapse/storage/databases/main/metrics.py Outdated
@jason-famedly

Copy link
Copy Markdown
Member

Here's the query plan

image

It may be that in the future we'll need an index on local_users_in_room. There is already one on room_id, but that isn't used here. The COUNT(*) is more efficient without it, so don't change that

@itsoyou
itsoyou requested a review from nico-famedly August 8, 2025 14:42

@nico-famedly nico-famedly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, although we should likely validate, if this causes any amount of significant CPU increase when receiving or sending events.

@itsoyou
itsoyou force-pushed the syk/add-room-count-metric branch from 85ee9b1 to 56313f0 Compare September 30, 2025 14:51
Comment thread synapse/storage/databases/main/stats.py

@jason-famedly jason-famedly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I usually question the need for metrics tests, perhaps just having some that check what the database calls do instead would be ok. That said, unless there ends up being something ridiculous around performance degradation let's go with this.

Nice job, and thanks for dealing with us being difficult! 😄

@itsoyou
itsoyou force-pushed the syk/add-room-count-metric branch from 125e601 to 596b2a2 Compare October 7, 2025 10:43
@itsoyou
itsoyou merged commit 596b2a2 into master Oct 7, 2025
20 of 23 checks passed
@itsoyou
itsoyou deleted the syk/add-room-count-metric branch October 7, 2025 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants