From c302d93897edf209891bee7f61d0a4e7efd68e02 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 20 Dec 2021 15:09:32 +0000 Subject: [PATCH] Fix behaviour when tracing is not enabled `start_active_span` always returns a truthy result, so we need to go further round the houses to get the span. --- synapse/util/caches/response_cache.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/synapse/util/caches/response_cache.py b/synapse/util/caches/response_cache.py index 3d9ae53acbec..e81d72f68889 100644 --- a/synapse/util/caches/response_cache.py +++ b/synapse/util/caches/response_cache.py @@ -20,6 +20,7 @@ from synapse.logging.context import make_deferred_yieldable, run_in_background from synapse.logging.opentracing import ( + active_span, start_active_span, start_active_span_follows_from, ) @@ -225,11 +226,10 @@ async def handle_request(request): async def cb() -> RV: # NB it is important that we do not `await` before setting span_context! nonlocal span_context - with start_active_span( - f"ResponseCache[{self._name}].calculate" - ) as scope: - if scope: - span_context = scope.span.context + with start_active_span(f"ResponseCache[{self._name}].calculate"): + span = active_span() + if span: + span_context = span.context return await callback(*args, **kwargs) d = run_in_background(cb) @@ -244,8 +244,9 @@ async def cb() -> RV: "[%s]: using incomplete cached result for [%s]", self._name, key ) + span_context = entry.opentracing_span_context with start_active_span_follows_from( f"ResponseCache[{self._name}].wait", - contexts=[entry.opentracing_span_context], + contexts=(span_context,) if span_context else (), ): return await make_deferred_yieldable(result)