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

Commit ed53bf3

Browse files
authored
Set opentracing priority before setting other tags (#10092)
... because tags on spans which aren't being sampled get thrown away.
1 parent 3f96dbb commit ed53bf3

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

changelog.d/10092.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in the `force_tracing_for_users` option introduced in Synapse v1.35 which meant that the OpenTracing spans produced were missing most tags.

synapse/api/auth.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ async def get_user_by_req(
206206
requester = create_requester(user_id, app_service=app_service)
207207

208208
request.requester = user_id
209+
if user_id in self._force_tracing_for_users:
210+
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
209211
opentracing.set_tag("authenticated_entity", user_id)
210212
opentracing.set_tag("user_id", user_id)
211213
opentracing.set_tag("appservice_id", app_service.id)
212-
if user_id in self._force_tracing_for_users:
213-
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
214214

215215
return requester
216216

@@ -259,12 +259,12 @@ async def get_user_by_req(
259259
)
260260

261261
request.requester = requester
262+
if user_info.token_owner in self._force_tracing_for_users:
263+
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
262264
opentracing.set_tag("authenticated_entity", user_info.token_owner)
263265
opentracing.set_tag("user_id", user_info.user_id)
264266
if device_id:
265267
opentracing.set_tag("device_id", device_id)
266-
if user_info.token_owner in self._force_tracing_for_users:
267-
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
268268

269269
return requester
270270
except KeyError:

synapse/federation/transport/server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
)
3838
from synapse.logging.context import run_in_background
3939
from synapse.logging.opentracing import (
40+
SynapseTags,
4041
start_active_span,
4142
start_active_span_from_request,
4243
tags,
@@ -314,7 +315,7 @@ async def new_func(request, *args, **kwargs):
314315
raise
315316

316317
request_tags = {
317-
"request_id": request.get_request_id(),
318+
SynapseTags.REQUEST_ID: request.get_request_id(),
318319
tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER,
319320
tags.HTTP_METHOD: request.get_method(),
320321
tags.HTTP_URL: request.get_redacted_uri(),

synapse/logging/opentracing.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ class SynapseTags:
265265
# Whether the sync response has new data to be returned to the client.
266266
SYNC_RESULT = "sync.new_data"
267267

268+
# incoming HTTP request ID (as written in the logs)
269+
REQUEST_ID = "request_id"
270+
271+
# HTTP request tag (used to distinguish full vs incremental syncs, etc)
272+
REQUEST_TAG = "request_tag"
273+
268274

269275
# Block everything by default
270276
# A regex which matches the server_names to expose traces for.
@@ -824,7 +830,7 @@ def trace_servlet(request: "SynapseRequest", extract_context: bool = False):
824830
return
825831

826832
request_tags = {
827-
"request_id": request.get_request_id(),
833+
SynapseTags.REQUEST_ID: request.get_request_id(),
828834
tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER,
829835
tags.HTTP_METHOD: request.get_method(),
830836
tags.HTTP_URL: request.get_redacted_uri(),
@@ -833,9 +839,9 @@ def trace_servlet(request: "SynapseRequest", extract_context: bool = False):
833839

834840
request_name = request.request_metrics.name
835841
if extract_context:
836-
scope = start_active_span_from_request(request, request_name, tags=request_tags)
842+
scope = start_active_span_from_request(request, request_name)
837843
else:
838-
scope = start_active_span(request_name, tags=request_tags)
844+
scope = start_active_span(request_name)
839845

840846
with scope:
841847
try:
@@ -845,4 +851,11 @@ def trace_servlet(request: "SynapseRequest", extract_context: bool = False):
845851
# with JsonResource).
846852
scope.span.set_operation_name(request.request_metrics.name)
847853

848-
scope.span.set_tag("request_tag", request.request_metrics.start_context.tag)
854+
# set the tags *after* the servlet completes, in case it decided to
855+
# prioritise the span (tags will get dropped on unprioritised spans)
856+
request_tags[
857+
SynapseTags.REQUEST_TAG
858+
] = request.request_metrics.start_context.tag
859+
860+
for k, v in request_tags.items():
861+
scope.span.set_tag(k, v)

synapse/metrics/background_process_metrics.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
from twisted.internet import defer
2323

2424
from synapse.logging.context import LoggingContext, PreserveLoggingContext
25-
from synapse.logging.opentracing import noop_context_manager, start_active_span
25+
from synapse.logging.opentracing import (
26+
SynapseTags,
27+
noop_context_manager,
28+
start_active_span,
29+
)
2630
from synapse.util.async_helpers import maybe_awaitable
2731

2832
if TYPE_CHECKING:
@@ -202,7 +206,9 @@ async def run():
202206
try:
203207
ctx = noop_context_manager()
204208
if bg_start_span:
205-
ctx = start_active_span(desc, tags={"request_id": str(context)})
209+
ctx = start_active_span(
210+
desc, tags={SynapseTags.REQUEST_ID: str(context)}
211+
)
206212
with ctx:
207213
return await maybe_awaitable(func(*args, **kwargs))
208214
except Exception:

0 commit comments

Comments
 (0)