mirror of
https://github.com/element-hq/synapse
synced 2024-09-19 22:15:11 +00:00
Fix tests and some lints
This commit is contained in:
parent
00be06cfd9
commit
6255a1a622
4 changed files with 31 additions and 8 deletions
|
@ -25,6 +25,7 @@ from synapse.logging.tracing import (
|
|||
Link,
|
||||
StatusCode,
|
||||
extract_text_map,
|
||||
get_span_context_from_context,
|
||||
set_status,
|
||||
start_active_span,
|
||||
whitelisted_homeserver,
|
||||
|
@ -89,11 +90,15 @@ class TransactionManager:
|
|||
keep_destination = whitelisted_homeserver(destination)
|
||||
|
||||
for edu in edus:
|
||||
context = edu.get_context()
|
||||
if context:
|
||||
span_contexts.append(extract_text_map(json_decoder.decode(context)))
|
||||
tracing_context_json = edu.get_tracing_context_json()
|
||||
if tracing_context_json:
|
||||
context = extract_text_map(json_decoder.decode(tracing_context_json))
|
||||
if context:
|
||||
span_context = get_span_context_from_context(context)
|
||||
if span_context:
|
||||
span_contexts.append(span_context)
|
||||
if keep_destination:
|
||||
edu.strip_context()
|
||||
edu.strip_tracing_context()
|
||||
|
||||
with start_active_span(
|
||||
"send_transaction",
|
||||
|
|
|
@ -55,12 +55,12 @@ class Edu:
|
|||
"destination": self.destination,
|
||||
}
|
||||
|
||||
def get_context(self) -> str:
|
||||
def get_tracing_context_json(self) -> str:
|
||||
return getattr(self, "content", {}).get(
|
||||
EventContentFields.TRACING_CONTEXT, "{}"
|
||||
)
|
||||
|
||||
def strip_context(self) -> None:
|
||||
def strip_tracing_context(self) -> None:
|
||||
getattr(self, "content", {})[EventContentFields.TRACING_CONTEXT] = "{}"
|
||||
|
||||
|
||||
|
|
|
@ -310,7 +310,7 @@ R = TypeVar("R")
|
|||
|
||||
|
||||
def only_if_tracing(func: Callable[P, R]) -> Callable[P, Optional[R]]:
|
||||
"""Executes the function only if we're tracing. Otherwise returns None."""
|
||||
"""Decorator function that executes the function only if we're tracing. Otherwise returns None."""
|
||||
|
||||
@wraps(func)
|
||||
def _only_if_tracing_inner(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
|
||||
|
@ -544,8 +544,12 @@ def start_active_span(
|
|||
start_time=start_time,
|
||||
record_exception=record_exception,
|
||||
set_status_on_exception=set_status_on_exception,
|
||||
tracer=tracer,
|
||||
)
|
||||
|
||||
ctx = opentelemetry.trace.propagation.set_span_in_context(span)
|
||||
logger.info("efwfewaafwewffew ctx=%s span_context=%s", ctx, span.get_span_context())
|
||||
|
||||
# Equivalent to `tracer.start_as_current_span`
|
||||
return opentelemetry.trace.use_span(
|
||||
span,
|
||||
|
@ -587,6 +591,20 @@ def get_active_span() -> Optional["opentelemetry.trace.span.Span"]:
|
|||
return opentelemetry.trace.get_current_span()
|
||||
|
||||
|
||||
def get_span_context_from_context(
|
||||
context: "opentelemetry.context.context.Context",
|
||||
) -> Optional["opentelemetry.trace.span.SpanContext"]:
|
||||
"""Utility function to convert a `Context` to a `SpanContext`
|
||||
|
||||
Based on https://github.com/open-telemetry/opentelemetry-python/blob/43288ca9a36144668797c11ca2654836ec8b5e99/opentelemetry-api/src/opentelemetry/trace/propagation/tracecontext.py#L99-L102
|
||||
"""
|
||||
span = opentelemetry.trace.get_current_span(context=context)
|
||||
span_context = span.get_span_context()
|
||||
if span_context == opentelemetry.trace.INVALID_SPAN_CONTEXT:
|
||||
return None
|
||||
return span_context
|
||||
|
||||
|
||||
@ensure_active_span("set a tag")
|
||||
def set_attribute(key: str, value: Union[str, bool, int, float]) -> None:
|
||||
"""Sets a tag on the active span"""
|
||||
|
|
|
@ -32,7 +32,7 @@ except ImportError:
|
|||
opentelemetry = None # type: ignore[assignment]
|
||||
|
||||
|
||||
class LogContextScopeManagerTestCase(TestCase):
|
||||
class TracingTestCase(TestCase):
|
||||
"""
|
||||
Test logging contexts and active opentelemetry spans.
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue