diff --git a/synapse/__init__.py b/synapse/__init__.py
index 79232c4de1..781f5ac3a2 100644
--- a/synapse/__init__.py
+++ b/synapse/__init__.py
@@ -48,7 +48,7 @@ try:
except ImportError:
pass
-__version__ = "1.32.0"
+__version__ = "1.32.2"
if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
# We import here so that we don't have to install a bunch of deps when
diff --git a/synapse/logging/context.py b/synapse/logging/context.py
index dbd7d3a33a..7fc11a9ac2 100644
--- a/synapse/logging/context.py
+++ b/synapse/logging/context.py
@@ -258,7 +258,8 @@ class LoggingContext:
child to the parent
Args:
- name (str): Name for the context for debugging.
+ name: Name for the context for logging. If this is omitted, it is
+ inherited from the parent context.
parent_context (LoggingContext|None): The parent of the new context
"""
@@ -277,12 +278,11 @@ class LoggingContext:
def __init__(
self,
- name: str,
+ name: Optional[str] = None,
parent_context: "Optional[LoggingContext]" = None,
request: Optional[ContextRequest] = None,
) -> None:
self.previous_context = current_context()
- self.name = name
# track the resources used by this context so far
self._resource_usage = ContextResourceUsage()
@@ -314,6 +314,15 @@ class LoggingContext:
# the request param overrides the request from the parent context
self.request = request
+ # if we don't have a `name`, but do have a parent context, use its name.
+ if self.parent_context and name is None:
+ name = str(self.parent_context)
+ if name is None:
+ raise ValueError(
+ "LoggingContext must be given either a name or a parent context"
+ )
+ self.name = name
+
def __str__(self) -> str:
return self.name
diff --git a/synapse/metrics/background_process_metrics.py b/synapse/metrics/background_process_metrics.py
index 78e9cfbc26..3f621539f3 100644
--- a/synapse/metrics/background_process_metrics.py
+++ b/synapse/metrics/background_process_metrics.py
@@ -16,7 +16,7 @@
import logging
import threading
from functools import wraps
-from typing import TYPE_CHECKING, Dict, Optional, Set
+from typing import TYPE_CHECKING, Dict, Optional, Set, Union
from prometheus_client.core import REGISTRY, Counter, Gauge
@@ -199,7 +199,7 @@ def run_as_background_process(desc: str, func, *args, bg_start_span=True, **kwar
_background_process_start_count.labels(desc).inc()
_background_process_in_flight_count.labels(desc).inc()
- with BackgroundProcessLoggingContext("%s-%s" % (desc, count)) as context:
+ with BackgroundProcessLoggingContext(desc, count) as context:
try:
ctx = noop_context_manager()
if bg_start_span:
@@ -244,8 +244,20 @@ class BackgroundProcessLoggingContext(LoggingContext):
__slots__ = ["_proc"]
- def __init__(self, name: str):
- super().__init__(name)
+ def __init__(self, name: str, instance_id: Optional[Union[int, str]] = None):
+ """
+
+ Args:
+ name: The name of the background process. Each distinct `name` gets a
+ separate prometheus time series.
+
+ instance_id: an identifer to add to `name` to distinguish this instance of
+ the named background process in the logs. If this is `None`, one is
+ made up based on id(self).
+ """
+ if instance_id is None:
+ instance_id = id(self)
+ super().__init__("%s-%s" % (name, instance_id))
self._proc = _BackgroundProcess(name, self)
def start(self, rusage: "Optional[resource._RUsage]"):
diff --git a/synapse/replication/tcp/protocol.py b/synapse/replication/tcp/protocol.py
index ba753318bd..d10d574246 100644
--- a/synapse/replication/tcp/protocol.py
+++ b/synapse/replication/tcp/protocol.py
@@ -185,7 +185,7 @@ class BaseReplicationStreamProtocol(LineOnlyReceiver):
# a logcontext which we use for processing incoming commands. We declare it as a
# background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext(
- "replication-conn-%s" % (self.conn_id,)
+ "replication-conn", self.conn_id
)
def connectionMade(self):
|