Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 9597e84

Browse files
realtyemFizzadar
authored andcommitted
Add a locality to a few presence metrics (matrix-org#15952)
1 parent f8ab853 commit 9597e84

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

changelog.d/15952.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update presence metrics to differentiate remote vs local users.

synapse/handlers/presence.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@
9595
get_updates_counter = Counter("synapse_handler_presence_get_updates", "", ["type"])
9696

9797
notify_reason_counter = Counter(
98-
"synapse_handler_presence_notify_reason", "", ["reason"]
98+
"synapse_handler_presence_notify_reason", "", ["locality", "reason"]
9999
)
100100
state_transition_counter = Counter(
101-
"synapse_handler_presence_state_transition", "", ["from", "to"]
101+
"synapse_handler_presence_state_transition", "", ["locality", "from", "to"]
102102
)
103103

104-
105104
# If a user was last active in the last LAST_ACTIVE_GRANULARITY, consider them
106105
# "currently_active"
107106
LAST_ACTIVE_GRANULARITY = 60 * 1000
@@ -567,8 +566,8 @@ async def process_replication_rows(
567566
for new_state in states:
568567
old_state = self.user_to_current_state.get(new_state.user_id)
569568
self.user_to_current_state[new_state.user_id] = new_state
570-
571-
if not old_state or should_notify(old_state, new_state):
569+
is_mine = self.is_mine_id(new_state.user_id)
570+
if not old_state or should_notify(old_state, new_state, is_mine):
572571
state_to_notify.append(new_state)
573572

574573
stream_id = token
@@ -1499,23 +1498,31 @@ async def _handle_state_delta(self, room_id: str, deltas: List[JsonDict]) -> Non
14991498
)
15001499

15011500

1502-
def should_notify(old_state: UserPresenceState, new_state: UserPresenceState) -> bool:
1501+
def should_notify(
1502+
old_state: UserPresenceState, new_state: UserPresenceState, is_mine: bool
1503+
) -> bool:
15031504
"""Decides if a presence state change should be sent to interested parties."""
1505+
user_location = "remote"
1506+
if is_mine:
1507+
user_location = "local"
1508+
15041509
if old_state == new_state:
15051510
return False
15061511

15071512
if old_state.status_msg != new_state.status_msg:
1508-
notify_reason_counter.labels("status_msg_change").inc()
1513+
notify_reason_counter.labels(user_location, "status_msg_change").inc()
15091514
return True
15101515

15111516
if old_state.state != new_state.state:
1512-
notify_reason_counter.labels("state_change").inc()
1513-
state_transition_counter.labels(old_state.state, new_state.state).inc()
1517+
notify_reason_counter.labels(user_location, "state_change").inc()
1518+
state_transition_counter.labels(
1519+
user_location, old_state.state, new_state.state
1520+
).inc()
15141521
return True
15151522

15161523
if old_state.state == PresenceState.ONLINE:
15171524
if new_state.currently_active != old_state.currently_active:
1518-
notify_reason_counter.labels("current_active_change").inc()
1525+
notify_reason_counter.labels(user_location, "current_active_change").inc()
15191526
return True
15201527

15211528
if (
@@ -1524,12 +1531,16 @@ def should_notify(old_state: UserPresenceState, new_state: UserPresenceState) ->
15241531
):
15251532
# Only notify about last active bumps if we're not currently active
15261533
if not new_state.currently_active:
1527-
notify_reason_counter.labels("last_active_change_online").inc()
1534+
notify_reason_counter.labels(
1535+
user_location, "last_active_change_online"
1536+
).inc()
15281537
return True
15291538

15301539
elif new_state.last_active_ts - old_state.last_active_ts > LAST_ACTIVE_GRANULARITY:
15311540
# Always notify for a transition where last active gets bumped.
1532-
notify_reason_counter.labels("last_active_change_not_online").inc()
1541+
notify_reason_counter.labels(
1542+
user_location, "last_active_change_not_online"
1543+
).inc()
15331544
return True
15341545

15351546
return False
@@ -1989,7 +2000,7 @@ def handle_update(
19892000
)
19902001

19912002
# Check whether the change was something worth notifying about
1992-
if should_notify(prev_state, new_state):
2003+
if should_notify(prev_state, new_state, is_mine):
19932004
new_state = new_state.copy_and_replace(last_federation_update_ts=now)
19942005
persist_and_notify = True
19952006

0 commit comments

Comments
 (0)