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

Commit 6b43f6b

Browse files
committed
Clean up the interface for injecting opentracing over HTTP
1 parent 4af3123 commit 6b43f6b

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

synapse/http/matrixfederationclient.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,9 @@
6565
read_body_with_max_size,
6666
)
6767
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
68+
from synapse.logging import opentracing
6869
from synapse.logging.context import make_deferred_yieldable
69-
from synapse.logging.opentracing import (
70-
inject_active_span_byte_dict,
71-
set_tag,
72-
start_active_span,
73-
tags,
74-
)
70+
from synapse.logging.opentracing import set_tag, start_active_span, tags
7571
from synapse.types import ISynapseReactor, JsonDict
7672
from synapse.util import json_decoder
7773
from synapse.util.async_helpers import timeout_deferred
@@ -497,7 +493,7 @@ async def _send_request(
497493

498494
# Inject the span into the headers
499495
headers_dict = {} # type: Dict[bytes, List[bytes]]
500-
inject_active_span_byte_dict(headers_dict, request.destination)
496+
opentracing.inject_header_dict(headers_dict, request.destination)
501497

502498
headers_dict[b"User-Agent"] = [self.version_string_bytes]
503499

synapse/logging/opentracing.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
168168
import logging
169169
import re
170170
from functools import wraps
171-
from typing import TYPE_CHECKING, Dict, Optional, Pattern, Type
171+
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Type
172172

173173
import attr
174174

@@ -573,23 +573,22 @@ def set_operation_name(operation_name):
573573
# Injection and extraction
574574

575575

576-
@ensure_active_span("inject the span into a byte dict")
577-
def inject_active_span_byte_dict(headers, destination, check_destination=True):
576+
@ensure_active_span("inject the span into a header dict")
577+
def inject_header_dict(
578+
headers: Dict[bytes, List[bytes]],
579+
destination: Optional[str] = None,
580+
check_destination: bool = True,
581+
) -> None:
578582
"""
579-
Injects a span context into a dict where the headers are encoded as byte
580-
strings
583+
Injects a span context into a dict of HTTP headers
581584
582585
Args:
583-
headers (dict)
584-
destination (str): address of entity receiving the span context. If check_destination
585-
is true the context will only be injected if the destination matches the
586-
opentracing whitelist
586+
headers: the dict to inject headers into
587+
destination: address of entity receiving the span context. Must be given unless
588+
check_destination is False. The context will only be injected if the
589+
destination matches the opentracing whitelist
587590
check_destination (bool): If false, destination will be ignored and the context
588591
will always be injected.
589-
span (opentracing.Span)
590-
591-
Returns:
592-
In-place modification of headers
593592
594593
Note:
595594
The headers set by the tracer are custom to the tracer implementation which
@@ -598,8 +597,13 @@ def inject_active_span_byte_dict(headers, destination, check_destination=True):
598597
here:
599598
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
600599
"""
601-
if check_destination and not whitelisted_homeserver(destination):
602-
return
600+
if check_destination:
601+
if destination is None:
602+
raise ValueError(
603+
"destination must be given unless check_destination is False"
604+
)
605+
if not whitelisted_homeserver(destination):
606+
return
603607

604608
span = opentracing.tracer.active_span
605609

synapse/replication/http/_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
from synapse.api.errors import HttpResponseException, SynapseError
2525
from synapse.http import RequestTimedOutError
26-
from synapse.logging.opentracing import inject_active_span_byte_dict, trace
26+
from synapse.logging import opentracing
27+
from synapse.logging.opentracing import trace
2728
from synapse.util.caches.response_cache import ResponseCache
2829
from synapse.util.stringutils import random_string
2930

@@ -235,7 +236,7 @@ async def send_request(*, instance_name="master", **kwargs):
235236
# Add an authorization header, if configured.
236237
if replication_secret:
237238
headers[b"Authorization"] = [b"Bearer " + replication_secret]
238-
inject_active_span_byte_dict(headers, None, check_destination=False)
239+
opentracing.inject_header_dict(headers, check_destination=False)
239240
try:
240241
result = await request_func(uri, data, headers=headers)
241242
break

0 commit comments

Comments
 (0)