|
| 1 | +# |
| 2 | +# This file is licensed under the Affero General Public License (AGPL) version 3. |
| 3 | +# |
| 4 | +# Copyright 2019 The Matrix.org Foundation C.I.C. |
| 5 | +# Copyright (C) 2023 New Vector, Ltd |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Affero General Public License as |
| 9 | +# published by the Free Software Foundation, either version 3 of the |
| 10 | +# License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# See the GNU Affero General Public License for more details: |
| 13 | +# <https://www.gnu.org/licenses/agpl-3.0.html>. |
| 14 | +# |
| 15 | +# Originally licensed under the Apache License, Version 2.0: |
| 16 | +# <http://www.apache.org/licenses/LICENSE-2.0>. |
| 17 | +# |
| 18 | +# [This file includes modifications made by New Vector Limited] |
| 19 | +# |
| 20 | +# |
| 21 | + |
| 22 | +import logging |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +from opentracing import Scope, ScopeManager, Span |
| 26 | + |
| 27 | +from synapse.logging.context import ( |
| 28 | + LoggingContext, |
| 29 | + current_context, |
| 30 | + nested_logging_context, |
| 31 | +) |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class LogContextScopeManager(ScopeManager): |
| 37 | + """ |
| 38 | + The LogContextScopeManager tracks the active scope in opentracing |
| 39 | + by using the log contexts which are native to synapse. This is so |
| 40 | + that the basic opentracing api can be used across twisted defereds. |
| 41 | +
|
| 42 | + It would be nice just to use opentracing's ContextVarsScopeManager, |
| 43 | + but currently that doesn't work due to https://twistedmatrix.com/trac/ticket/10301. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self) -> None: |
| 47 | + pass |
| 48 | + |
| 49 | + @property |
| 50 | + def active(self) -> Optional[Scope]: |
| 51 | + """ |
| 52 | + Returns the currently active Scope which can be used to access the |
| 53 | + currently active Scope.span. |
| 54 | + If there is a non-null Scope, its wrapped Span |
| 55 | + becomes an implicit parent of any newly-created Span at |
| 56 | + Tracer.start_active_span() time. |
| 57 | +
|
| 58 | + Return: |
| 59 | + The Scope that is active, or None if not available. |
| 60 | + """ |
| 61 | + ctx = current_context() |
| 62 | + return ctx.scope |
| 63 | + |
| 64 | + def activate(self, span: Span, finish_on_close: bool) -> Scope: |
| 65 | + """ |
| 66 | + Makes a Span active. |
| 67 | + Args |
| 68 | + span: the span that should become active. |
| 69 | + finish_on_close: whether Span should be automatically finished when |
| 70 | + Scope.close() is called. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Scope to control the end of the active period for |
| 74 | + *span*. It is a programming error to neglect to call |
| 75 | + Scope.close() on the returned instance. |
| 76 | + """ |
| 77 | + |
| 78 | + ctx = current_context() |
| 79 | + |
| 80 | + if not ctx: |
| 81 | + logger.error("Tried to activate scope outside of loggingcontext") |
| 82 | + return Scope(None, span) # type: ignore[arg-type] |
| 83 | + |
| 84 | + if ctx.scope is not None: |
| 85 | + # start a new logging context as a child of the existing one. |
| 86 | + # Doing so -- rather than updating the existing logcontext -- means that |
| 87 | + # creating several concurrent spans under the same logcontext works |
| 88 | + # correctly. |
| 89 | + ctx = nested_logging_context("") |
| 90 | + enter_logcontext = True |
| 91 | + else: |
| 92 | + # if there is no span currently associated with the current logcontext, we |
| 93 | + # just store the scope in it. |
| 94 | + # |
| 95 | + # This feels a bit dubious, but it does hack around a problem where a |
| 96 | + # span outlasts its parent logcontext (which would otherwise lead to |
| 97 | + # "Re-starting finished log context" errors). |
| 98 | + enter_logcontext = False |
| 99 | + |
| 100 | + scope = _LogContextScope(self, span, ctx, enter_logcontext, finish_on_close) |
| 101 | + ctx.scope = scope |
| 102 | + if enter_logcontext: |
| 103 | + ctx.__enter__() |
| 104 | + |
| 105 | + return scope |
| 106 | + |
| 107 | + |
| 108 | +class _LogContextScope(Scope): |
| 109 | + """ |
| 110 | + A custom opentracing scope, associated with a LogContext |
| 111 | +
|
| 112 | + * When the scope is closed, the logcontext's active scope is reset to None. |
| 113 | + and - if enter_logcontext was set - the logcontext is finished too. |
| 114 | + """ |
| 115 | + |
| 116 | + def __init__( |
| 117 | + self, |
| 118 | + manager: LogContextScopeManager, |
| 119 | + span: Span, |
| 120 | + logcontext: LoggingContext, |
| 121 | + enter_logcontext: bool, |
| 122 | + finish_on_close: bool, |
| 123 | + ): |
| 124 | + """ |
| 125 | + Args: |
| 126 | + manager: |
| 127 | + the manager that is responsible for this scope. |
| 128 | + span: |
| 129 | + the opentracing span which this scope represents the local |
| 130 | + lifetime for. |
| 131 | + logcontext: |
| 132 | + the log context to which this scope is attached. |
| 133 | + enter_logcontext: |
| 134 | + if True the log context will be exited when the scope is finished |
| 135 | + finish_on_close: |
| 136 | + if True finish the span when the scope is closed |
| 137 | + """ |
| 138 | + super().__init__(manager, span) |
| 139 | + self.logcontext = logcontext |
| 140 | + self._finish_on_close = finish_on_close |
| 141 | + self._enter_logcontext = enter_logcontext |
| 142 | + |
| 143 | + def __str__(self) -> str: |
| 144 | + return f"Scope<{self.span}>" |
| 145 | + |
| 146 | + def close(self) -> None: |
| 147 | + active_scope = self.manager.active |
| 148 | + if active_scope is not self: |
| 149 | + logger.error( |
| 150 | + "Closing scope %s which is not the currently-active one %s", |
| 151 | + self, |
| 152 | + active_scope, |
| 153 | + ) |
| 154 | + |
| 155 | + if self._finish_on_close: |
| 156 | + self.span.finish() |
| 157 | + |
| 158 | + self.logcontext.scope = None |
| 159 | + |
| 160 | + if self._enter_logcontext: |
| 161 | + self.logcontext.__exit__(None, None, None) |
0 commit comments