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

Commit 9a6f4a6

Browse files
authored
Cleanup opentracing logging for syncs (#10828)
We added a bunch of spans in #10704, but this ended up adding a lot of redundant spans for rooms where nothing changed, so instead we only start the span if there might be something interesting going on.
1 parent 474edce commit 9a6f4a6

File tree

2 files changed

+142
-126
lines changed

2 files changed

+142
-126
lines changed

changelog.d/10828.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added opentrace logging to help debug #9424.

synapse/handlers/sync.py

Lines changed: 141 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,21 +1533,18 @@ async def _generate_sync_entry_for_rooms(
15331533
newly_left_rooms = room_changes.newly_left_rooms
15341534

15351535
async def handle_room_entries(room_entry: "RoomSyncResultBuilder"):
1536-
with start_active_span("generate_room_entry"):
1537-
set_tag("room_id", room_entry.room_id)
1538-
log_kv({"events": len(room_entry.events or [])})
1539-
logger.debug("Generating room entry for %s", room_entry.room_id)
1540-
res = await self._generate_room_entry(
1541-
sync_result_builder,
1542-
ignored_users,
1543-
room_entry,
1544-
ephemeral=ephemeral_by_room.get(room_entry.room_id, []),
1545-
tags=tags_by_room.get(room_entry.room_id),
1546-
account_data=account_data_by_room.get(room_entry.room_id, {}),
1547-
always_include=sync_result_builder.full_state,
1548-
)
1549-
logger.debug("Generated room entry for %s", room_entry.room_id)
1550-
return res
1536+
logger.debug("Generating room entry for %s", room_entry.room_id)
1537+
res = await self._generate_room_entry(
1538+
sync_result_builder,
1539+
ignored_users,
1540+
room_entry,
1541+
ephemeral=ephemeral_by_room.get(room_entry.room_id, []),
1542+
tags=tags_by_room.get(room_entry.room_id),
1543+
account_data=account_data_by_room.get(room_entry.room_id, {}),
1544+
always_include=sync_result_builder.full_state,
1545+
)
1546+
logger.debug("Generated room entry for %s", room_entry.room_id)
1547+
return res
15511548

15521549
await concurrently_execute(handle_room_entries, room_entries, 10)
15531550

@@ -1960,139 +1957,157 @@ async def _generate_room_entry(
19601957
room_id = room_builder.room_id
19611958
since_token = room_builder.since_token
19621959
upto_token = room_builder.upto_token
1963-
log_kv(
1964-
{
1965-
"since_token": since_token,
1966-
"upto_token": upto_token,
1967-
}
1968-
)
19691960

1970-
batch = await self._load_filtered_recents(
1971-
room_id,
1972-
sync_config,
1973-
now_token=upto_token,
1974-
since_token=since_token,
1975-
potential_recents=events,
1976-
newly_joined_room=newly_joined,
1977-
)
1978-
log_kv(
1979-
{
1980-
"batch_events": len(batch.events),
1981-
"prev_batch": batch.prev_batch,
1982-
"batch_limited": batch.limited,
1983-
}
1984-
)
1961+
with start_active_span("generate_room_entry"):
1962+
set_tag("room_id", room_id)
1963+
log_kv({"events": len(events or ())})
19851964

1986-
# Note: `batch` can be both empty and limited here in the case where
1987-
# `_load_filtered_recents` can't find any events the user should see
1988-
# (e.g. due to having ignored the sender of the last 50 events).
1965+
log_kv(
1966+
{
1967+
"since_token": since_token,
1968+
"upto_token": upto_token,
1969+
}
1970+
)
19891971

1990-
if newly_joined:
1991-
# debug for https://github.com/matrix-org/synapse/issues/4422
1992-
issue4422_logger.debug(
1993-
"Timeline events after filtering in newly-joined room %s: %r",
1972+
batch = await self._load_filtered_recents(
19941973
room_id,
1995-
batch,
1974+
sync_config,
1975+
now_token=upto_token,
1976+
since_token=since_token,
1977+
potential_recents=events,
1978+
newly_joined_room=newly_joined,
1979+
)
1980+
log_kv(
1981+
{
1982+
"batch_events": len(batch.events),
1983+
"prev_batch": batch.prev_batch,
1984+
"batch_limited": batch.limited,
1985+
}
19961986
)
19971987

1998-
# When we join the room (or the client requests full_state), we should
1999-
# send down any existing tags. Usually the user won't have tags in a
2000-
# newly joined room, unless either a) they've joined before or b) the
2001-
# tag was added by synapse e.g. for server notice rooms.
2002-
if full_state:
2003-
user_id = sync_result_builder.sync_config.user.to_string()
2004-
tags = await self.store.get_tags_for_room(user_id, room_id)
1988+
# Note: `batch` can be both empty and limited here in the case where
1989+
# `_load_filtered_recents` can't find any events the user should see
1990+
# (e.g. due to having ignored the sender of the last 50 events).
20051991

2006-
# If there aren't any tags, don't send the empty tags list down
2007-
# sync
2008-
if not tags:
2009-
tags = None
1992+
if newly_joined:
1993+
# debug for https://github.com/matrix-org/synapse/issues/4422
1994+
issue4422_logger.debug(
1995+
"Timeline events after filtering in newly-joined room %s: %r",
1996+
room_id,
1997+
batch,
1998+
)
20101999

2011-
account_data_events = []
2012-
if tags is not None:
2013-
account_data_events.append({"type": "m.tag", "content": {"tags": tags}})
2000+
# When we join the room (or the client requests full_state), we should
2001+
# send down any existing tags. Usually the user won't have tags in a
2002+
# newly joined room, unless either a) they've joined before or b) the
2003+
# tag was added by synapse e.g. for server notice rooms.
2004+
if full_state:
2005+
user_id = sync_result_builder.sync_config.user.to_string()
2006+
tags = await self.store.get_tags_for_room(user_id, room_id)
20142007

2015-
for account_data_type, content in account_data.items():
2016-
account_data_events.append({"type": account_data_type, "content": content})
2008+
# If there aren't any tags, don't send the empty tags list down
2009+
# sync
2010+
if not tags:
2011+
tags = None
20172012

2018-
account_data_events = sync_config.filter_collection.filter_room_account_data(
2019-
account_data_events
2020-
)
2013+
account_data_events = []
2014+
if tags is not None:
2015+
account_data_events.append({"type": "m.tag", "content": {"tags": tags}})
20212016

2022-
ephemeral = sync_config.filter_collection.filter_room_ephemeral(ephemeral)
2017+
for account_data_type, content in account_data.items():
2018+
account_data_events.append(
2019+
{"type": account_data_type, "content": content}
2020+
)
20232021

2024-
if not (
2025-
always_include or batch or account_data_events or ephemeral or full_state
2026-
):
2027-
return
2022+
account_data_events = (
2023+
sync_config.filter_collection.filter_room_account_data(
2024+
account_data_events
2025+
)
2026+
)
20282027

2029-
state = await self.compute_state_delta(
2030-
room_id, batch, sync_config, since_token, now_token, full_state=full_state
2031-
)
2028+
ephemeral = sync_config.filter_collection.filter_room_ephemeral(ephemeral)
20322029

2033-
summary: Optional[JsonDict] = {}
2034-
2035-
# we include a summary in room responses when we're lazy loading
2036-
# members (as the client otherwise doesn't have enough info to form
2037-
# the name itself).
2038-
if sync_config.filter_collection.lazy_load_members() and (
2039-
# we recalculate the summary:
2040-
# if there are membership changes in the timeline, or
2041-
# if membership has changed during a gappy sync, or
2042-
# if this is an initial sync.
2043-
any(ev.type == EventTypes.Member for ev in batch.events)
2044-
or (
2045-
# XXX: this may include false positives in the form of LL
2046-
# members which have snuck into state
2047-
batch.limited
2048-
and any(t == EventTypes.Member for (t, k) in state)
2049-
)
2050-
or since_token is None
2051-
):
2052-
summary = await self.compute_summary(
2053-
room_id, sync_config, batch, state, now_token
2054-
)
2030+
if not (
2031+
always_include
2032+
or batch
2033+
or account_data_events
2034+
or ephemeral
2035+
or full_state
2036+
):
2037+
return
20552038

2056-
if room_builder.rtype == "joined":
2057-
unread_notifications: Dict[str, int] = {}
2058-
room_sync = JoinedSyncResult(
2059-
room_id=room_id,
2060-
timeline=batch,
2061-
state=state,
2062-
ephemeral=ephemeral,
2063-
account_data=account_data_events,
2064-
unread_notifications=unread_notifications,
2065-
summary=summary,
2066-
unread_count=0,
2039+
state = await self.compute_state_delta(
2040+
room_id,
2041+
batch,
2042+
sync_config,
2043+
since_token,
2044+
now_token,
2045+
full_state=full_state,
20672046
)
20682047

2069-
if room_sync or always_include:
2070-
notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
2048+
summary: Optional[JsonDict] = {}
2049+
2050+
# we include a summary in room responses when we're lazy loading
2051+
# members (as the client otherwise doesn't have enough info to form
2052+
# the name itself).
2053+
if sync_config.filter_collection.lazy_load_members() and (
2054+
# we recalculate the summary:
2055+
# if there are membership changes in the timeline, or
2056+
# if membership has changed during a gappy sync, or
2057+
# if this is an initial sync.
2058+
any(ev.type == EventTypes.Member for ev in batch.events)
2059+
or (
2060+
# XXX: this may include false positives in the form of LL
2061+
# members which have snuck into state
2062+
batch.limited
2063+
and any(t == EventTypes.Member for (t, k) in state)
2064+
)
2065+
or since_token is None
2066+
):
2067+
summary = await self.compute_summary(
2068+
room_id, sync_config, batch, state, now_token
2069+
)
2070+
2071+
if room_builder.rtype == "joined":
2072+
unread_notifications: Dict[str, int] = {}
2073+
room_sync = JoinedSyncResult(
2074+
room_id=room_id,
2075+
timeline=batch,
2076+
state=state,
2077+
ephemeral=ephemeral,
2078+
account_data=account_data_events,
2079+
unread_notifications=unread_notifications,
2080+
summary=summary,
2081+
unread_count=0,
2082+
)
20712083

2072-
unread_notifications["notification_count"] = notifs["notify_count"]
2073-
unread_notifications["highlight_count"] = notifs["highlight_count"]
2084+
if room_sync or always_include:
2085+
notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
20742086

2075-
room_sync.unread_count = notifs["unread_count"]
2087+
unread_notifications["notification_count"] = notifs["notify_count"]
2088+
unread_notifications["highlight_count"] = notifs["highlight_count"]
20762089

2077-
sync_result_builder.joined.append(room_sync)
2090+
room_sync.unread_count = notifs["unread_count"]
20782091

2079-
if batch.limited and since_token:
2080-
user_id = sync_result_builder.sync_config.user.to_string()
2081-
logger.debug(
2082-
"Incremental gappy sync of %s for user %s with %d state events"
2083-
% (room_id, user_id, len(state))
2092+
sync_result_builder.joined.append(room_sync)
2093+
2094+
if batch.limited and since_token:
2095+
user_id = sync_result_builder.sync_config.user.to_string()
2096+
logger.debug(
2097+
"Incremental gappy sync of %s for user %s with %d state events"
2098+
% (room_id, user_id, len(state))
2099+
)
2100+
elif room_builder.rtype == "archived":
2101+
archived_room_sync = ArchivedSyncResult(
2102+
room_id=room_id,
2103+
timeline=batch,
2104+
state=state,
2105+
account_data=account_data_events,
20842106
)
2085-
elif room_builder.rtype == "archived":
2086-
archived_room_sync = ArchivedSyncResult(
2087-
room_id=room_id,
2088-
timeline=batch,
2089-
state=state,
2090-
account_data=account_data_events,
2091-
)
2092-
if archived_room_sync or always_include:
2093-
sync_result_builder.archived.append(archived_room_sync)
2094-
else:
2095-
raise Exception("Unrecognized rtype: %r", room_builder.rtype)
2107+
if archived_room_sync or always_include:
2108+
sync_result_builder.archived.append(archived_room_sync)
2109+
else:
2110+
raise Exception("Unrecognized rtype: %r", room_builder.rtype)
20962111

20972112
async def get_rooms_for_user_at(
20982113
self, user_id: str, room_key: RoomStreamToken

0 commit comments

Comments
 (0)