mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-11-17 08:59:11 -05:00
Fixes for opentracing scopes (#11869)
`start_active_span` was inconsistent as to whether it would activate the span immediately, or wait for `scope.__enter__` to happen (it depended on whether the current logcontext already had an associated scope). The inconsistency was rather confusing if you were hoping to set up a couple of separate spans before activating either. Looking at the other implementations of opentracing `ScopeManager`s, the intention is that it *should* be activated immediately, as the name implies. Indeed, the idea is that you don't have to use the scope as a contextmanager at all - you can just call `.close` on the result. Hence, our cleanup has to happen in `.close` rather than `.__exit__`. So, the main change here is to ensure that `start_active_span` does activate the span, and that `scope.close()` does close the scope. We also add some tests, which requires a `tracer` param so that we don't have to rely on the global variable in unit tests.
This commit is contained in:
parent
a8da046907
commit
31b554c297
4 changed files with 255 additions and 35 deletions
|
|
@ -443,10 +443,14 @@ def start_active_span(
|
|||
start_time=None,
|
||||
ignore_active_span=False,
|
||||
finish_on_close=True,
|
||||
*,
|
||||
tracer=None,
|
||||
):
|
||||
"""Starts an active opentracing span. Note, the scope doesn't become active
|
||||
until it has been entered, however, the span starts from the time this
|
||||
message is called.
|
||||
"""Starts an active opentracing span.
|
||||
|
||||
Records the start time for the span, and sets it as the "active span" in the
|
||||
scope manager.
|
||||
|
||||
Args:
|
||||
See opentracing.tracer
|
||||
Returns:
|
||||
|
|
@ -456,7 +460,11 @@ def start_active_span(
|
|||
if opentracing is None:
|
||||
return noop_context_manager() # type: ignore[unreachable]
|
||||
|
||||
return opentracing.tracer.start_active_span(
|
||||
if tracer is None:
|
||||
# use the global tracer by default
|
||||
tracer = opentracing.tracer
|
||||
|
||||
return tracer.start_active_span(
|
||||
operation_name,
|
||||
child_of=child_of,
|
||||
references=references,
|
||||
|
|
@ -468,7 +476,11 @@ def start_active_span(
|
|||
|
||||
|
||||
def start_active_span_follows_from(
|
||||
operation_name: str, contexts: Collection, inherit_force_tracing=False
|
||||
operation_name: str,
|
||||
contexts: Collection,
|
||||
*,
|
||||
inherit_force_tracing=False,
|
||||
tracer=None,
|
||||
):
|
||||
"""Starts an active opentracing span, with additional references to previous spans
|
||||
|
||||
|
|
@ -477,12 +489,17 @@ def start_active_span_follows_from(
|
|||
contexts: the previous spans to inherit from
|
||||
inherit_force_tracing: if set, and any of the previous contexts have had tracing
|
||||
forced, the new span will also have tracing forced.
|
||||
tracer: override the opentracing tracer. By default the global tracer is used.
|
||||
"""
|
||||
if opentracing is None:
|
||||
return noop_context_manager() # type: ignore[unreachable]
|
||||
|
||||
references = [opentracing.follows_from(context) for context in contexts]
|
||||
scope = start_active_span(operation_name, references=references)
|
||||
scope = start_active_span(
|
||||
operation_name,
|
||||
references=references,
|
||||
tracer=tracer,
|
||||
)
|
||||
|
||||
if inherit_force_tracing and any(
|
||||
is_context_forced_tracing(ctx) for ctx in contexts
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue