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

Commit

Permalink
Stop BackgroundProcessLoggingContext making new prometheus timeseries (
Browse files Browse the repository at this point in the history
…#9854)

This undoes part of b076bc2.
  • Loading branch information
richvdh committed Apr 21, 2021
1 parent 05fa068 commit 5d281c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.d/9854.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a regression in Synapse 1.32.0 which caused Synapse to report large numbers of Prometheus time series, potentially overwhelming Prometheus instances.
20 changes: 16 additions & 4 deletions synapse/metrics/background_process_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -199,7 +199,7 @@ async def run():
_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:
Expand Down Expand Up @@ -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]"):
Expand Down
2 changes: 1 addition & 1 deletion synapse/replication/tcp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def __init__(self, clock: Clock, handler: "ReplicationCommandHandler"):
# 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):
Expand Down

0 comments on commit 5d281c1

Please sign in to comment.