synapse/changelog.d
Eric Eastwood 5143f93dc9
Fix server_name in logging context for multiple Synapse instances in one process (#18868)
### Background

As part of Element's plan to support a light form of vhosting (virtual
host) (multiple instances of Synapse in the same Python process), we're
currently diving into the details and implications of running multiple
instances of Synapse in the same Python process.

"Per-tenant logging" tracked internally by
https://github.com/element-hq/synapse-small-hosts/issues/48

### Prior art

Previously, we exposed `server_name` by providing a static logging
`MetadataFilter` that injected the values:


205d9e4fc4/synapse/config/logger.py (L216)

While this can work fine for the normal case of one Synapse instance per
Python process, this configures things globally and isn't compatible
when we try to start multiple Synapse instances because each subsequent
tenant will overwrite the previous tenant.


### What does this PR do?

We remove the `MetadataFilter` and replace it by tracking the
`server_name` in the `LoggingContext` and expose it with our existing
[`LoggingContextFilter`](205d9e4fc4/synapse/logging/context.py (L584-L622))
that we already use to expose information about the `request`.

This means that the `server_name` value follows wherever we log as
expected even when we have multiple Synapse instances running in the
same process.


### A note on logcontext

Anywhere, Synapse mistakenly uses the `sentinel` logcontext to log
something, we won't know which server sent the log. We've been fixing up
`sentinel` logcontext usage as tracked by
https://github.com/element-hq/synapse/issues/18905

Any further `sentinel` logcontext usage we find in the future can be
fixed piecemeal as normal.


d2a966f922/docs/log_contexts.md (L71-L81)


### Testing strategy

1. Adjust your logging config to include `%(server_name)s` in the format
    ```yaml
    formatters:
        precise:
format: '%(asctime)s - %(server_name)s - %(name)s - %(lineno)d -
%(levelname)s - %(request)s - %(message)s'
    ```
1. Start Synapse: `poetry run synapse_homeserver --config-path
homeserver.yaml`
1. Make some requests (`curl
http://localhost:8008/_matrix/client/versions`, etc)
1. Open the homeserver logs and notice the `server_name` in the logs as
expected. `unknown_server_from_sentinel_context` is expected for the
`sentinel` logcontext (things outside of Synapse).
2025-09-26 17:10:48 -05:00
..
.gitignore
18721.bugfix Document and fix room_config param when user_may_create_room callback is invoked for a room upgrade (#18721) 2025-09-24 21:42:19 +00:00
18767.misc Updated providers.json to use X instead of Twitter following rebrand and schema change (#18767) 2025-09-26 11:06:50 +01:00
18868.misc Fix server_name in logging context for multiple Synapse instances in one process (#18868) 2025-09-26 17:10:48 -05:00
18911.feature Add an Admin API to query a piece of local or cached remote media by ID (#18911) 2025-09-23 16:25:56 -05:00
18913.misc Fix triage_labelled GHA workflow (#18913) 2025-09-24 14:17:14 +01:00
18914.doc Explain how Deferred callbacks interact with logcontexts (#18914) 2025-09-24 16:20:42 -05:00
18932.misc Disconnect background process work from request trace (#18932) 2025-09-25 21:45:18 -05:00
18934.feature Use signature support from policy servers when available (#18934) 2025-09-25 19:30:24 +00:00
18939.misc Bump batch size from 50 to 1000 for _get_e2e_cross_signing_signatures_for_devices query (#18939) 2025-09-23 15:47:29 +01:00
18947.misc Update MSC4326 error code (#18947) 2025-09-24 11:57:24 +01:00
18948.bugfix fix: Compute user last seen timestamp from last seen devices (#18948) 2025-09-24 11:59:11 +01:00
18959.misc Fix no active span when trying to log tracing error on startup (#18959) 2025-09-24 10:12:08 -05:00
18964.misc Fix run_coroutine_in_background(...) incorrectly handling logcontext (#18964) 2025-09-24 15:24:47 +00:00
18971.misc Update URLs in dockerfile metadata (#18971) 2025-09-26 12:40:50 +01:00
18973.misc Note optional Element Commecial License in SPDX specifiers (#18973) 2025-09-26 12:43:07 +01:00
18974.misc Fix logcontext handling in timeout_deferred tests (#18974) 2025-09-26 11:10:02 -05:00