Skip to content
Snippets Groups Projects
Unverified Commit 5d281c10 authored by Richard van der Hoff's avatar Richard van der Hoff Committed by GitHub
Browse files

Stop BackgroundProcessLoggingContext making new prometheus timeseries (#9854)

This undoes part of b076bc27.
parent 05fa0683
No related branches found
No related tags found
No related merge requests found
Fix a regression in Synapse 1.32.0 which caused Synapse to report large numbers of Prometheus time series, potentially overwhelming Prometheus instances.
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
import logging import logging
import threading import threading
from functools import wraps 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 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 ...@@ -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_start_count.labels(desc).inc()
_background_process_in_flight_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: try:
ctx = noop_context_manager() ctx = noop_context_manager()
if bg_start_span: if bg_start_span:
...@@ -244,8 +244,20 @@ class BackgroundProcessLoggingContext(LoggingContext): ...@@ -244,8 +244,20 @@ class BackgroundProcessLoggingContext(LoggingContext):
__slots__ = ["_proc"] __slots__ = ["_proc"]
def __init__(self, name: str): def __init__(self, name: str, instance_id: Optional[Union[int, str]] = None):
super().__init__(name) """
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) self._proc = _BackgroundProcess(name, self)
def start(self, rusage: "Optional[resource._RUsage]"): def start(self, rusage: "Optional[resource._RUsage]"):
......
...@@ -185,7 +185,7 @@ class BaseReplicationStreamProtocol(LineOnlyReceiver): ...@@ -185,7 +185,7 @@ class BaseReplicationStreamProtocol(LineOnlyReceiver):
# a logcontext which we use for processing incoming commands. We declare it as a # 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. # background process so that the CPU stats get reported to prometheus.
self._logging_context = BackgroundProcessLoggingContext( self._logging_context = BackgroundProcessLoggingContext(
"replication-conn-%s" % (self.conn_id,) "replication-conn", self.conn_id
) )
def connectionMade(self): def connectionMade(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment