diff options
author | Richard van der Hoff <1389908+richvdh@users.noreply.github.com> | 2021-06-18 11:43:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-18 11:43:22 +0100 |
commit | 91fa9cca99f7cd1ba96baaf3f2c1b5c045dd1a7c (patch) | |
tree | 53f8813d658832eac092acec56d431d27fe91c5c | |
parent | Remove support for ACME v1 (#10194) (diff) | |
download | synapse-91fa9cca99f7cd1ba96baaf3f2c1b5c045dd1a7c.tar.xz |
Expose opentracing trace id in response headers (#10199)
Fixes: #9480
Diffstat (limited to '')
-rw-r--r-- | changelog.d/10199.misc | 1 | ||||
-rw-r--r-- | synapse/federation/transport/server.py | 3 | ||||
-rw-r--r-- | synapse/logging/opentracing.py | 21 |
3 files changed, 25 insertions, 0 deletions
diff --git a/changelog.d/10199.misc b/changelog.d/10199.misc new file mode 100644 index 0000000000..69b18aeacc --- /dev/null +++ b/changelog.d/10199.misc @@ -0,0 +1 @@ +Expose opentracing trace id in response headers. diff --git a/synapse/federation/transport/server.py b/synapse/federation/transport/server.py index 16d740cf58..bed47f8abd 100644 --- a/synapse/federation/transport/server.py +++ b/synapse/federation/transport/server.py @@ -35,6 +35,7 @@ from synapse.http.servlet import ( parse_string_from_args, parse_strings_from_args, ) +from synapse.logging import opentracing from synapse.logging.context import run_in_background from synapse.logging.opentracing import ( SynapseTags, @@ -345,6 +346,8 @@ class BaseFederationServlet: ) with scope: + opentracing.inject_response_headers(request.responseHeaders) + if origin and self.RATELIMIT: with ratelimiter.ratelimit(origin) as d: await d diff --git a/synapse/logging/opentracing.py b/synapse/logging/opentracing.py index 4f18792c99..140ed711e3 100644 --- a/synapse/logging/opentracing.py +++ b/synapse/logging/opentracing.py @@ -173,6 +173,7 @@ from typing import TYPE_CHECKING, Collection, Dict, List, Optional, Pattern, Typ import attr from twisted.internet import defer +from twisted.web.http_headers import Headers from synapse.config import ConfigError from synapse.util import json_decoder, json_encoder @@ -668,6 +669,25 @@ def inject_header_dict( headers[key.encode()] = [value.encode()] +def inject_response_headers(response_headers: Headers) -> None: + """Inject the current trace id into the HTTP response headers""" + if not opentracing: + return + span = opentracing.tracer.active_span + if not span: + return + + # This is a bit implementation-specific. + # + # Jaeger's Spans have a trace_id property; other implementations (including the + # dummy opentracing.span.Span which we use if init_tracer is not called) do not + # expose it + trace_id = getattr(span, "trace_id", None) + + if trace_id is not None: + response_headers.addRawHeader("Synapse-Trace-Id", f"{trace_id:x}") + + @ensure_active_span("get the active span context as a dict", ret={}) def get_active_span_text_map(destination=None): """ @@ -843,6 +863,7 @@ def trace_servlet(request: "SynapseRequest", extract_context: bool = False): scope = start_active_span(request_name) with scope: + inject_response_headers(request.responseHeaders) try: yield finally: |