Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix behaviour when tracing is not enabled
Browse files Browse the repository at this point in the history
`start_active_span` always returns a truthy result, so we need to go further
round the houses to get the span.
  • Loading branch information
richvdh committed Dec 20, 2021
1 parent f71e964 commit c302d93
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions synapse/util/caches/response_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit c302d93

Please sign in to comment.