Compare commits

..

5 Commits

Author SHA1 Message Date
Eric Eastwood
648a24f08f
Remove duplicate word typo
Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com>
2025-10-02 10:19:38 -05:00
Eric Eastwood
7b31c569c1
Fix missing word typo
Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com>
2025-10-02 10:19:21 -05:00
Eric Eastwood
db2835e294 Fix lints 2025-10-02 00:03:17 -05:00
Eric Eastwood
5cc63af08b Use previous pattern 2025-10-01 23:59:40 -05:00
Eric Eastwood
094c01db4b Fix lints 2025-10-01 23:39:12 -05:00
3 changed files with 35 additions and 27 deletions

View File

@ -1 +1 @@
Switch back to own custom `LogContextScopeManager` instead of OpenTracing's `ContextVarsScopeManager` which was causing problems when using the experimental `SYNAPSE_ASYNC_IO_REACTOR` option with tracing enabled.
Switch back to our own custom `LogContextScopeManager` instead of OpenTracing's `ContextVarsScopeManager` which was causing problems when using the experimental `SYNAPSE_ASYNC_IO_REACTOR` option with tracing enabled.

View File

@ -268,7 +268,7 @@ def run_as_background_process(
"""
# Since we track the tracing scope in the `LoggingContext`, before we move to the
# sentinel logcontext (or a new new `LoggingContext`), grab the currently active
# sentinel logcontext (or a new `LoggingContext`), grab the currently active
# tracing span (if any) so that we can create a cross-link to the background process
# trace.
original_active_tracing_span = active_span(tracer=test_only_tracer)

View File

@ -19,7 +19,7 @@
#
#
from typing import Awaitable, Dict, cast
from typing import Awaitable, Optional, cast
from twisted.internet import defer
from twisted.internet.testing import MemoryReactorClock
@ -35,7 +35,6 @@ from synapse.logging.opentracing import (
tag_args,
trace_with_opname,
)
from synapse.logging.scopecontextmanager import LogContextScopeManager
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.util.clock import Clock
@ -49,8 +48,11 @@ except ImportError:
try:
import opentracing
from synapse.logging.scopecontextmanager import LogContextScopeManager
except ImportError:
opentracing = None # type: ignore
LogContextScopeManager = None # type: ignore
import logging
@ -70,7 +72,7 @@ class LogContextScopeManagerTestCase(TestCase):
opentracing backend is Jaeger.
"""
if opentracing is None:
if opentracing is None or LogContextScopeManager is None:
skip = "Requires opentracing" # type: ignore[unreachable]
if jaeger_client is None:
skip = "Requires jaeger_client" # type: ignore[unreachable]
@ -327,11 +329,12 @@ class LogContextScopeManagerTestCase(TestCase):
reactor, clock = get_clock()
callback_finished = False
active_span_in_callback = None
active_span_in_callback: Optional[jaeger_client.Span] = None
async def bg_task() -> None:
nonlocal callback_finished, active_span_in_callback
try:
assert isinstance(self._tracer.active_span, jaeger_client.Span)
active_span_in_callback = self._tracer.active_span
finally:
# When exceptions happen, we still want to mark the callback as finished
@ -388,11 +391,12 @@ class LogContextScopeManagerTestCase(TestCase):
reactor, clock = get_clock()
callback_finished = False
active_span_in_callback = None
active_span_in_callback: Optional[jaeger_client.Span] = None
async def bg_task() -> None:
nonlocal callback_finished, active_span_in_callback
try:
assert isinstance(self._tracer.active_span, jaeger_client.Span)
active_span_in_callback = self._tracer.active_span
finally:
# When exceptions happen, we still want to mark the callback as finished
@ -452,22 +456,22 @@ class LogContextScopeManagerTestCase(TestCase):
expected_spans,
)
span_map: Dict[str, jaeger_client.SpanContext] = {
span.operation_name: span for span in self._reporter.get_spans()
}
span_map = {span.operation_name: span for span in self._reporter.get_spans()}
span_id_to_friendly_name = {
span.context.span_id: span.operation_name
for span in self._reporter.get_spans()
span.span_id: span.operation_name for span in self._reporter.get_spans()
}
def get_span_friendly_name(span_id: int) -> str:
def get_span_friendly_name(span_id: Optional[int]) -> str:
if span_id is None:
return "None"
return span_id_to_friendly_name.get(span_id, f"unknown span {span_id}")
# Ensure the background process trace/span is disconnected from the request
# trace/span.
self.assertNotEqual(
get_span_friendly_name(span_map["bgproc.some-bg-task"].context.parent_id),
get_span_friendly_name(span_map["some-request"].context.span_id),
get_span_friendly_name(span_map["bgproc.some-bg-task"].parent_id),
get_span_friendly_name(span_map["some-request"].span_id),
)
# We should see a cross-link in the request trace pointing to the background
@ -475,19 +479,21 @@ class LogContextScopeManagerTestCase(TestCase):
#
# Make sure `start_bgproc.some-bg-task` is part of the request trace
self.assertEqual(
get_span_friendly_name(
span_map["start_bgproc.some-bg-task"].context.parent_id
),
get_span_friendly_name(span_map["some-request"].context.span_id),
get_span_friendly_name(span_map["start_bgproc.some-bg-task"].parent_id),
get_span_friendly_name(span_map["some-request"].span_id),
)
# And has some references to the background process trace
self.assertIncludes(
{
f"{reference.type}:{get_span_friendly_name(reference.referenced_context.span_id)}"
for reference in span_map["start_bgproc.some-bg-task"].references
if isinstance(reference.referenced_context, jaeger_client.SpanContext)
else f"{reference.type}:None"
for reference in (
span_map["start_bgproc.some-bg-task"].references or []
)
},
{
f"follows_from:{get_span_friendly_name(span_map['bgproc.some-bg-task'].context.span_id)}"
f"follows_from:{get_span_friendly_name(span_map['bgproc.some-bg-task'].span_id)}"
},
exact=True,
)
@ -497,19 +503,21 @@ class LogContextScopeManagerTestCase(TestCase):
#
# Make sure `start_bgproc.some-bg-task` is part of the request trace
self.assertEqual(
get_span_friendly_name(
span_map["bgproc_child.some-bg-task"].context.parent_id
),
get_span_friendly_name(span_map["bgproc.some-bg-task"].context.span_id),
get_span_friendly_name(span_map["bgproc_child.some-bg-task"].parent_id),
get_span_friendly_name(span_map["bgproc.some-bg-task"].span_id),
)
# And has some references to the background process trace
self.assertIncludes(
{
f"{reference.type}:{get_span_friendly_name(reference.referenced_context.span_id)}"
for reference in span_map["bgproc_child.some-bg-task"].references
if isinstance(reference.referenced_context, jaeger_client.SpanContext)
else f"{reference.type}:None"
for reference in (
span_map["bgproc_child.some-bg-task"].references or []
)
},
{
f"follows_from:{get_span_friendly_name(span_map['some-request'].context.span_id)}"
f"follows_from:{get_span_friendly_name(span_map['some-request'].span_id)}"
},
exact=True,
)