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

Commit 1bf83a1

Browse files
authored
Clean up the interface for injecting opentracing over HTTP (#10143)
* Remove unused helper functions * Clean up the interface for injecting opentracing over HTTP * changelog
1 parent c7f3fb2 commit 1bf83a1

File tree

4 files changed

+26
-92
lines changed

4 files changed

+26
-92
lines changed

changelog.d/10143.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean up the interface for injecting opentracing over HTTP.

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 & 83 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

@@ -574,59 +574,22 @@ def set_operation_name(operation_name):
574574
# Injection and extraction
575575

576576

577-
@ensure_active_span("inject the span into a header")
578-
def inject_active_span_twisted_headers(headers, destination, check_destination=True):
577+
@ensure_active_span("inject the span into a header dict")
578+
def inject_header_dict(
579+
headers: Dict[bytes, List[bytes]],
580+
destination: Optional[str] = None,
581+
check_destination: bool = True,
582+
) -> None:
579583
"""
580-
Injects a span context into twisted headers in-place
584+
Injects a span context into a dict of HTTP headers
581585
582586
Args:
583-
headers (twisted.web.http_headers.Headers)
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
587+
headers: the dict to inject headers into
588+
destination: address of entity receiving the span context. Must be given unless
589+
check_destination is False. The context will only be injected if the
590+
destination matches the opentracing whitelist
587591
check_destination (bool): If false, destination will be ignored and the context
588592
will always be injected.
589-
span (opentracing.Span)
590-
591-
Returns:
592-
In-place modification of headers
593-
594-
Note:
595-
The headers set by the tracer are custom to the tracer implementation which
596-
should be unique enough that they don't interfere with any headers set by
597-
synapse or twisted. If we're still using jaeger these headers would be those
598-
here:
599-
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
600-
"""
601-
602-
if check_destination and not whitelisted_homeserver(destination):
603-
return
604-
605-
span = opentracing.tracer.active_span
606-
carrier = {} # type: Dict[str, str]
607-
opentracing.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier)
608-
609-
for key, value in carrier.items():
610-
headers.addRawHeaders(key, value)
611-
612-
613-
@ensure_active_span("inject the span into a byte dict")
614-
def inject_active_span_byte_dict(headers, destination, check_destination=True):
615-
"""
616-
Injects a span context into a dict where the headers are encoded as byte
617-
strings
618-
619-
Args:
620-
headers (dict)
621-
destination (str): address of entity receiving the span context. If check_destination
622-
is true the context will only be injected if the destination matches the
623-
opentracing whitelist
624-
check_destination (bool): If false, destination will be ignored and the context
625-
will always be injected.
626-
span (opentracing.Span)
627-
628-
Returns:
629-
In-place modification of headers
630593
631594
Note:
632595
The headers set by the tracer are custom to the tracer implementation which
@@ -635,8 +598,13 @@ def inject_active_span_byte_dict(headers, destination, check_destination=True):
635598
here:
636599
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
637600
"""
638-
if check_destination and not whitelisted_homeserver(destination):
639-
return
601+
if check_destination:
602+
if destination is None:
603+
raise ValueError(
604+
"destination must be given unless check_destination is False"
605+
)
606+
if not whitelisted_homeserver(destination):
607+
return
640608

641609
span = opentracing.tracer.active_span
642610

@@ -647,38 +615,6 @@ def inject_active_span_byte_dict(headers, destination, check_destination=True):
647615
headers[key.encode()] = [value.encode()]
648616

649617

650-
@ensure_active_span("inject the span into a text map")
651-
def inject_active_span_text_map(carrier, destination, check_destination=True):
652-
"""
653-
Injects a span context into a dict
654-
655-
Args:
656-
carrier (dict)
657-
destination (str): address of entity receiving the span context. If check_destination
658-
is true the context will only be injected if the destination matches the
659-
opentracing whitelist
660-
check_destination (bool): If false, destination will be ignored and the context
661-
will always be injected.
662-
663-
Returns:
664-
In-place modification of carrier
665-
666-
Note:
667-
The headers set by the tracer are custom to the tracer implementation which
668-
should be unique enough that they don't interfere with any headers set by
669-
synapse or twisted. If we're still using jaeger these headers would be those
670-
here:
671-
https://github.com/jaegertracing/jaeger-client-python/blob/master/jaeger_client/constants.py
672-
"""
673-
674-
if check_destination and not whitelisted_homeserver(destination):
675-
return
676-
677-
opentracing.tracer.inject(
678-
opentracing.tracer.active_span.context, opentracing.Format.TEXT_MAP, carrier
679-
)
680-
681-
682618
@ensure_active_span("get the active span context as a dict", ret={})
683619
def get_active_span_text_map(destination=None):
684620
"""

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)