2019-07-11 03:36:03 -06:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.import logging
|
|
|
|
|
|
|
|
import logging
|
2022-05-13 05:35:31 -06:00
|
|
|
from types import TracebackType
|
|
|
|
from typing import Optional, Type
|
2019-07-11 03:36:03 -06:00
|
|
|
|
2022-06-30 07:05:06 -06:00
|
|
|
from opentracing import Scope, ScopeManager, Span
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
import twisted
|
|
|
|
|
2022-06-30 07:05:06 -06:00
|
|
|
from synapse.logging.context import (
|
|
|
|
LoggingContext,
|
|
|
|
current_context,
|
|
|
|
nested_logging_context,
|
|
|
|
)
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class LogContextScopeManager(ScopeManager):
|
|
|
|
"""
|
|
|
|
The LogContextScopeManager tracks the active scope in opentracing
|
|
|
|
by using the log contexts which are native to synapse. This is so
|
|
|
|
that the basic opentracing api can be used across twisted defereds.
|
2022-02-02 15:41:57 -07:00
|
|
|
|
|
|
|
It would be nice just to use opentracing's ContextVarsScopeManager,
|
|
|
|
but currently that doesn't work due to https://twistedmatrix.com/trac/ticket/10301.
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
|
|
|
|
2022-06-30 07:05:06 -06:00
|
|
|
def __init__(self) -> None:
|
2019-07-18 08:06:54 -06:00
|
|
|
pass
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
@property
|
2022-06-30 07:05:06 -06:00
|
|
|
def active(self) -> Optional[Scope]:
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
|
|
|
Returns the currently active Scope which can be used to access the
|
|
|
|
currently active Scope.span.
|
|
|
|
If there is a non-null Scope, its wrapped Span
|
|
|
|
becomes an implicit parent of any newly-created Span at
|
|
|
|
Tracer.start_active_span() time.
|
|
|
|
|
|
|
|
Return:
|
2022-06-30 07:05:06 -06:00
|
|
|
The Scope that is active, or None if not available.
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
2020-03-24 08:45:33 -06:00
|
|
|
ctx = current_context()
|
|
|
|
return ctx.scope
|
2019-07-11 03:36:03 -06:00
|
|
|
|
2022-06-30 07:05:06 -06:00
|
|
|
def activate(self, span: Span, finish_on_close: bool) -> Scope:
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
|
|
|
Makes a Span active.
|
|
|
|
Args
|
2022-06-30 07:05:06 -06:00
|
|
|
span: the span that should become active.
|
|
|
|
finish_on_close: whether Span should be automatically finished when
|
|
|
|
Scope.close() is called.
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Scope to control the end of the active period for
|
|
|
|
*span*. It is a programming error to neglect to call
|
|
|
|
Scope.close() on the returned instance.
|
|
|
|
"""
|
|
|
|
|
2020-03-24 08:45:33 -06:00
|
|
|
ctx = current_context()
|
2019-07-11 03:36:03 -06:00
|
|
|
|
2020-03-24 08:45:33 -06:00
|
|
|
if not ctx:
|
2019-07-11 03:36:03 -06:00
|
|
|
logger.error("Tried to activate scope outside of loggingcontext")
|
2021-12-20 05:18:09 -07:00
|
|
|
return Scope(None, span) # type: ignore[arg-type]
|
2022-02-02 15:41:57 -07:00
|
|
|
|
|
|
|
if ctx.scope is not None:
|
|
|
|
# start a new logging context as a child of the existing one.
|
|
|
|
# Doing so -- rather than updating the existing logcontext -- means that
|
|
|
|
# creating several concurrent spans under the same logcontext works
|
|
|
|
# correctly.
|
2019-07-11 03:36:03 -06:00
|
|
|
ctx = nested_logging_context("")
|
|
|
|
enter_logcontext = True
|
2022-02-02 15:41:57 -07:00
|
|
|
else:
|
|
|
|
# if there is no span currently associated with the current logcontext, we
|
|
|
|
# just store the scope in it.
|
|
|
|
#
|
|
|
|
# This feels a bit dubious, but it does hack around a problem where a
|
|
|
|
# span outlasts its parent logcontext (which would otherwise lead to
|
|
|
|
# "Re-starting finished log context" errors).
|
|
|
|
enter_logcontext = False
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
scope = _LogContextScope(self, span, ctx, enter_logcontext, finish_on_close)
|
|
|
|
ctx.scope = scope
|
2022-02-02 15:41:57 -07:00
|
|
|
if enter_logcontext:
|
|
|
|
ctx.__enter__()
|
|
|
|
|
2019-07-11 03:36:03 -06:00
|
|
|
return scope
|
|
|
|
|
|
|
|
|
|
|
|
class _LogContextScope(Scope):
|
|
|
|
"""
|
2022-02-02 15:41:57 -07:00
|
|
|
A custom opentracing scope, associated with a LogContext
|
|
|
|
|
|
|
|
* filters out _DefGen_Return exceptions which arise from calling
|
|
|
|
`defer.returnValue` in Twisted code
|
|
|
|
|
|
|
|
* When the scope is closed, the logcontext's active scope is reset to None.
|
|
|
|
and - if enter_logcontext was set - the logcontext is finished too.
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
|
|
|
|
2022-05-13 05:35:31 -06:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
manager: LogContextScopeManager,
|
2022-06-30 07:05:06 -06:00
|
|
|
span: Span,
|
|
|
|
logcontext: LoggingContext,
|
2022-05-13 05:35:31 -06:00
|
|
|
enter_logcontext: bool,
|
|
|
|
finish_on_close: bool,
|
|
|
|
):
|
2019-07-11 03:36:03 -06:00
|
|
|
"""
|
|
|
|
Args:
|
2022-05-13 05:35:31 -06:00
|
|
|
manager:
|
2019-07-11 03:36:03 -06:00
|
|
|
the manager that is responsible for this scope.
|
2022-06-30 07:05:06 -06:00
|
|
|
span:
|
2019-07-11 03:36:03 -06:00
|
|
|
the opentracing span which this scope represents the local
|
|
|
|
lifetime for.
|
2022-06-30 07:05:06 -06:00
|
|
|
logcontext:
|
|
|
|
the log context to which this scope is attached.
|
2022-05-13 05:35:31 -06:00
|
|
|
enter_logcontext:
|
2022-06-30 07:05:06 -06:00
|
|
|
if True the log context will be exited when the scope is finished
|
2022-05-13 05:35:31 -06:00
|
|
|
finish_on_close:
|
2019-07-11 03:36:03 -06:00
|
|
|
if True finish the span when the scope is closed
|
|
|
|
"""
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(manager, span)
|
2019-07-11 03:36:03 -06:00
|
|
|
self.logcontext = logcontext
|
|
|
|
self._finish_on_close = finish_on_close
|
|
|
|
self._enter_logcontext = enter_logcontext
|
|
|
|
|
2022-05-13 05:35:31 -06:00
|
|
|
def __exit__(
|
|
|
|
self,
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
value: Optional[BaseException],
|
|
|
|
traceback: Optional[TracebackType],
|
|
|
|
) -> None:
|
2022-02-02 15:41:57 -07:00
|
|
|
if exc_type == twisted.internet.defer._DefGen_Return:
|
|
|
|
# filter out defer.returnValue() calls
|
|
|
|
exc_type = value = traceback = None
|
|
|
|
super().__exit__(exc_type, value, traceback)
|
2019-07-11 03:36:03 -06:00
|
|
|
|
2022-05-13 05:35:31 -06:00
|
|
|
def __str__(self) -> str:
|
2022-02-02 15:41:57 -07:00
|
|
|
return f"Scope<{self.span}>"
|
2019-07-11 03:36:03 -06:00
|
|
|
|
2022-05-13 05:35:31 -06:00
|
|
|
def close(self) -> None:
|
2022-02-02 15:41:57 -07:00
|
|
|
active_scope = self.manager.active
|
|
|
|
if active_scope is not self:
|
|
|
|
logger.error(
|
|
|
|
"Closing scope %s which is not the currently-active one %s",
|
|
|
|
self,
|
|
|
|
active_scope,
|
|
|
|
)
|
2019-07-11 03:36:03 -06:00
|
|
|
|
|
|
|
if self._finish_on_close:
|
|
|
|
self.span.finish()
|
2022-02-02 15:41:57 -07:00
|
|
|
|
|
|
|
self.logcontext.scope = None
|
|
|
|
|
|
|
|
if self._enter_logcontext:
|
|
|
|
self.logcontext.__exit__(None, None, None)
|