mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-10-01 11:49:51 -04:00
Add type hints to the logging context code. (#8939)
This commit is contained in:
parent
9dde9c9f01
commit
06fefe0bb1
1
changelog.d/8939.misc
Normal file
1
changelog.d/8939.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Various clean-ups to the structured logging and logging context code.
|
@ -252,7 +252,12 @@ class LoggingContext:
|
|||||||
"scope",
|
"scope",
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, name=None, parent_context=None, request=None) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: Optional[str] = None,
|
||||||
|
parent_context: "Optional[LoggingContext]" = None,
|
||||||
|
request: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
self.previous_context = current_context()
|
self.previous_context = current_context()
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
@ -536,20 +541,20 @@ class LoggingContextFilter(logging.Filter):
|
|||||||
def __init__(self, request: str = ""):
|
def __init__(self, request: str = ""):
|
||||||
self._default_request = request
|
self._default_request = request
|
||||||
|
|
||||||
def filter(self, record) -> Literal[True]:
|
def filter(self, record: logging.LogRecord) -> Literal[True]:
|
||||||
"""Add each fields from the logging contexts to the record.
|
"""Add each fields from the logging contexts to the record.
|
||||||
Returns:
|
Returns:
|
||||||
True to include the record in the log output.
|
True to include the record in the log output.
|
||||||
"""
|
"""
|
||||||
context = current_context()
|
context = current_context()
|
||||||
record.request = self._default_request
|
record.request = self._default_request # type: ignore
|
||||||
|
|
||||||
# context should never be None, but if it somehow ends up being, then
|
# context should never be None, but if it somehow ends up being, then
|
||||||
# we end up in a death spiral of infinite loops, so let's check, for
|
# we end up in a death spiral of infinite loops, so let's check, for
|
||||||
# robustness' sake.
|
# robustness' sake.
|
||||||
if context is not None:
|
if context is not None:
|
||||||
# Logging is interested in the request.
|
# Logging is interested in the request.
|
||||||
record.request = context.request
|
record.request = context.request # type: ignore
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -616,9 +621,7 @@ def set_current_context(context: LoggingContextOrSentinel) -> LoggingContextOrSe
|
|||||||
return current
|
return current
|
||||||
|
|
||||||
|
|
||||||
def nested_logging_context(
|
def nested_logging_context(suffix: str) -> LoggingContext:
|
||||||
suffix: str, parent_context: Optional[LoggingContext] = None
|
|
||||||
) -> LoggingContext:
|
|
||||||
"""Creates a new logging context as a child of another.
|
"""Creates a new logging context as a child of another.
|
||||||
|
|
||||||
The nested logging context will have a 'request' made up of the parent context's
|
The nested logging context will have a 'request' made up of the parent context's
|
||||||
@ -632,20 +635,23 @@ def nested_logging_context(
|
|||||||
# ... do stuff
|
# ... do stuff
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
suffix (str): suffix to add to the parent context's 'request'.
|
suffix: suffix to add to the parent context's 'request'.
|
||||||
parent_context (LoggingContext|None): parent context. Will use the current context
|
|
||||||
if None.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
LoggingContext: new logging context.
|
LoggingContext: new logging context.
|
||||||
"""
|
"""
|
||||||
if parent_context is not None:
|
curr_context = current_context()
|
||||||
context = parent_context # type: LoggingContextOrSentinel
|
if not curr_context:
|
||||||
|
logger.warning(
|
||||||
|
"Starting nested logging context from sentinel context: metrics will be lost"
|
||||||
|
)
|
||||||
|
parent_context = None
|
||||||
|
prefix = ""
|
||||||
else:
|
else:
|
||||||
context = current_context()
|
assert isinstance(curr_context, LoggingContext)
|
||||||
return LoggingContext(
|
parent_context = curr_context
|
||||||
parent_context=context, request=str(context.request) + "-" + suffix
|
prefix = str(parent_context.request)
|
||||||
)
|
return LoggingContext(parent_context=parent_context, request=prefix + "-" + suffix)
|
||||||
|
|
||||||
|
|
||||||
def preserve_fn(f):
|
def preserve_fn(f):
|
||||||
@ -822,10 +828,18 @@ def defer_to_threadpool(reactor, threadpool, f, *args, **kwargs):
|
|||||||
Deferred: A Deferred which fires a callback with the result of `f`, or an
|
Deferred: A Deferred which fires a callback with the result of `f`, or an
|
||||||
errback if `f` throws an exception.
|
errback if `f` throws an exception.
|
||||||
"""
|
"""
|
||||||
logcontext = current_context()
|
curr_context = current_context()
|
||||||
|
if not curr_context:
|
||||||
|
logger.warning(
|
||||||
|
"Calling defer_to_threadpool from sentinel context: metrics will be lost"
|
||||||
|
)
|
||||||
|
parent_context = None
|
||||||
|
else:
|
||||||
|
assert isinstance(curr_context, LoggingContext)
|
||||||
|
parent_context = curr_context
|
||||||
|
|
||||||
def g():
|
def g():
|
||||||
with LoggingContext(parent_context=logcontext):
|
with LoggingContext(parent_context=parent_context):
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
return make_deferred_yieldable(threads.deferToThreadPool(reactor, threadpool, g))
|
return make_deferred_yieldable(threads.deferToThreadPool(reactor, threadpool, g))
|
||||||
|
@ -42,7 +42,6 @@ from synapse.api.errors import StoreError
|
|||||||
from synapse.config.database import DatabaseConnectionConfig
|
from synapse.config.database import DatabaseConnectionConfig
|
||||||
from synapse.logging.context import (
|
from synapse.logging.context import (
|
||||||
LoggingContext,
|
LoggingContext,
|
||||||
LoggingContextOrSentinel,
|
|
||||||
current_context,
|
current_context,
|
||||||
make_deferred_yieldable,
|
make_deferred_yieldable,
|
||||||
)
|
)
|
||||||
@ -671,12 +670,15 @@ class DatabasePool:
|
|||||||
Returns:
|
Returns:
|
||||||
The result of func
|
The result of func
|
||||||
"""
|
"""
|
||||||
parent_context = current_context() # type: Optional[LoggingContextOrSentinel]
|
curr_context = current_context()
|
||||||
if not parent_context:
|
if not curr_context:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Starting db connection from sentinel context: metrics will be lost"
|
"Starting db connection from sentinel context: metrics will be lost"
|
||||||
)
|
)
|
||||||
parent_context = None
|
parent_context = None
|
||||||
|
else:
|
||||||
|
assert isinstance(curr_context, LoggingContext)
|
||||||
|
parent_context = curr_context
|
||||||
|
|
||||||
start_time = monotonic_time()
|
start_time = monotonic_time()
|
||||||
|
|
||||||
|
@ -108,7 +108,15 @@ class Measure:
|
|||||||
def __init__(self, clock, name):
|
def __init__(self, clock, name):
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.name = name
|
self.name = name
|
||||||
parent_context = current_context()
|
curr_context = current_context()
|
||||||
|
if not curr_context:
|
||||||
|
logger.warning(
|
||||||
|
"Starting metrics collection from sentinel context: metrics will be lost"
|
||||||
|
)
|
||||||
|
parent_context = None
|
||||||
|
else:
|
||||||
|
assert isinstance(curr_context, LoggingContext)
|
||||||
|
parent_context = curr_context
|
||||||
self._logging_context = LoggingContext(
|
self._logging_context = LoggingContext(
|
||||||
"Measure[%s]" % (self.name,), parent_context
|
"Measure[%s]" % (self.name,), parent_context
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user