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

Commit

Permalink
Clean up some LoggingContext stuff (#7120)
Browse files Browse the repository at this point in the history
* Pull Sentinel out of LoggingContext

... and drop a few unnecessary references to it

* Factor out LoggingContext.current_context

move `current_context` and `set_context` out to top-level functions.

Mostly this means that I can more easily trace what's actually referring to
LoggingContext, but I think it's generally neater.

* move copy-to-parent into `stop`

this really just makes `start` and `stop` more symetric. It also means that it
behaves correctly if you manually `set_log_context` rather than using the
context manager.

* Replace `LoggingContext.alive` with `finished`

Turn `alive` into `finished` and make it a bit better defined.
  • Loading branch information
richvdh authored Mar 24, 2020
1 parent 1fcf9c6 commit 39230d2
Show file tree
Hide file tree
Showing 24 changed files with 232 additions and 222 deletions.
1 change: 1 addition & 0 deletions changelog.d/7120.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clean up some LoggingContext code.
5 changes: 2 additions & 3 deletions docs/log_contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ from synapse.logging import context # omitted from future snippets
def handle_request(request_id):
request_context = context.LoggingContext()

calling_context = context.LoggingContext.current_context()
context.LoggingContext.set_current_context(request_context)
calling_context = context.set_current_context(request_context)
try:
request_context.request = request_id
do_request_handling()
logger.debug("finished")
finally:
context.LoggingContext.set_current_context(calling_context)
context.set_current_context(calling_context)

def do_request_handling():
logger.debug("phew") # this will be logged against request_id
Expand Down
4 changes: 2 additions & 2 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
SynapseError,
)
from synapse.logging.context import (
LoggingContext,
PreserveLoggingContext,
current_context,
make_deferred_yieldable,
preserve_fn,
run_in_background,
Expand Down Expand Up @@ -236,7 +236,7 @@ def _start_key_lookups(self, verify_requests):
"""

try:
ctx = LoggingContext.current_context()
ctx = current_context()

# map from server name to a set of outstanding request ids
server_to_request_ids = {}
Expand Down
4 changes: 2 additions & 2 deletions synapse/federation/federation_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from synapse.events.utils import prune_event
from synapse.http.servlet import assert_params_in_dict
from synapse.logging.context import (
LoggingContext,
PreserveLoggingContext,
current_context,
make_deferred_yieldable,
)
from synapse.types import JsonDict, get_domain_from_id
Expand Down Expand Up @@ -78,7 +78,7 @@ def _check_sigs_and_hashes(
"""
deferreds = _check_sigs_on_pdus(self.keyring, room_version, pdus)

ctx = LoggingContext.current_context()
ctx = current_context()

def callback(_, pdu: EventBase):
with PreserveLoggingContext(ctx):
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from synapse.api.constants import EventTypes, Membership
from synapse.api.filtering import FilterCollection
from synapse.events import EventBase
from synapse.logging.context import LoggingContext
from synapse.logging.context import current_context
from synapse.push.clientformat import format_push_rules_for_user
from synapse.storage.roommember import MemberSummary
from synapse.storage.state import StateFilter
Expand Down Expand Up @@ -301,7 +301,7 @@ async def _wait_for_sync_for_user(
else:
sync_type = "incremental_sync"

context = LoggingContext.current_context()
context = current_context()
if context:
context.tag = sync_type

Expand Down
6 changes: 3 additions & 3 deletions synapse/http/request_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from prometheus_client.core import Counter, Histogram

from synapse.logging.context import LoggingContext
from synapse.logging.context import current_context
from synapse.metrics import LaterGauge

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -148,7 +148,7 @@ def _get_in_flight_counts():
class RequestMetrics(object):
def start(self, time_sec, name, method):
self.start = time_sec
self.start_context = LoggingContext.current_context()
self.start_context = current_context()
self.name = name
self.method = method

Expand All @@ -163,7 +163,7 @@ def stop(self, time_sec, response_code, sent_bytes):
with _in_flight_requests_lock:
_in_flight_requests.discard(self)

context = LoggingContext.current_context()
context = current_context()

tag = ""
if context:
Expand Down
4 changes: 2 additions & 2 deletions synapse/logging/_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
TerseJSONToConsoleLogObserver,
TerseJSONToTCPLogObserver,
)
from synapse.logging.context import LoggingContext
from synapse.logging.context import current_context


def stdlib_log_level_to_twisted(level: str) -> LogLevel:
Expand Down Expand Up @@ -86,7 +86,7 @@ def __call__(self, event: dict) -> None:
].startswith("Timing out client"):
return

context = LoggingContext.current_context()
context = current_context()

# Copy the context information to the log event.
if context is not None:
Expand Down
Loading

0 comments on commit 39230d2

Please sign in to comment.