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

Commit 0780047

Browse files
committed
When we force tracing, set a baggage item
... so that we can check again later.
1 parent ebc89f0 commit 0780047

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

synapse/api/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async def get_user_by_req(
207207

208208
request.requester = user_id
209209
if user_id in self._force_tracing_for_users:
210-
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
210+
opentracing.force_tracing()
211211
opentracing.set_tag("authenticated_entity", user_id)
212212
opentracing.set_tag("user_id", user_id)
213213
opentracing.set_tag("appservice_id", app_service.id)
@@ -260,7 +260,7 @@ async def get_user_by_req(
260260

261261
request.requester = requester
262262
if user_info.token_owner in self._force_tracing_for_users:
263-
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
263+
opentracing.force_tracing()
264264
opentracing.set_tag("authenticated_entity", user_info.token_owner)
265265
opentracing.set_tag("user_id", user_info.user_id)
266266
if device_id:

synapse/logging/opentracing.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,19 @@ class SynapseTags:
278278
DB_TXN_ID = "db.txn_id"
279279

280280

281+
class SynapseBaggage:
282+
FORCE_TRACING = "synapse-force-tracing"
283+
284+
281285
# Block everything by default
282286
# A regex which matches the server_names to expose traces for.
283287
# None means 'block everything'.
284288
_homeserver_whitelist = None # type: Optional[Pattern[str]]
285289

286290
# Util methods
287291

292+
Sentinel = object()
293+
288294

289295
def only_if_tracing(func):
290296
"""Executes the function only if we're tracing. Otherwise returns None."""
@@ -575,6 +581,33 @@ def set_operation_name(operation_name):
575581
opentracing.tracer.active_span.set_operation_name(operation_name)
576582

577583

584+
@only_if_tracing
585+
def force_tracing(span=Sentinel) -> None:
586+
"""Force sampling for the active/given span and its children.
587+
588+
Args:
589+
span: span to force tracing for. By default, the active span.
590+
"""
591+
if span is Sentinel:
592+
span = opentracing.tracer.active_span
593+
if span is None:
594+
logger.error("No active span in force_tracing")
595+
return
596+
597+
span.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
598+
599+
# also set a bit of baggage, so that we have a way of figuring out if
600+
# it is enabled later
601+
span.set_baggage_item(SynapseBaggage.FORCE_TRACING, "1")
602+
603+
604+
def is_context_forced_tracing(span_context) -> bool:
605+
"""Check if sampling has been force for the given span context."""
606+
if span_context is None:
607+
return False
608+
return span_context.baggage.get(SynapseBaggage.FORCE_TRACING) is not None
609+
610+
578611
# Injection and extraction
579612

580613

0 commit comments

Comments
 (0)