Skip to content

Commit

Permalink
Set opentracing priority before setting other tags (matrix-org#10092)
Browse files Browse the repository at this point in the history
... because tags on spans which aren't being sampled get thrown away.
  • Loading branch information
richvdh authored May 28, 2021
1 parent 3f96dbb commit ed53bf3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/10092.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +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.
8 changes: 4 additions & 4 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ async def get_user_by_req(
requester = create_requester(user_id, app_service=app_service)

request.requester = user_id
if user_id in self._force_tracing_for_users:
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
opentracing.set_tag("authenticated_entity", user_id)
opentracing.set_tag("user_id", user_id)
opentracing.set_tag("appservice_id", app_service.id)
if user_id in self._force_tracing_for_users:
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)

return requester

Expand Down Expand Up @@ -259,12 +259,12 @@ async def get_user_by_req(
)

request.requester = requester
if user_info.token_owner in self._force_tracing_for_users:
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)
opentracing.set_tag("authenticated_entity", user_info.token_owner)
opentracing.set_tag("user_id", user_info.user_id)
if device_id:
opentracing.set_tag("device_id", device_id)
if user_info.token_owner in self._force_tracing_for_users:
opentracing.set_tag(opentracing.tags.SAMPLING_PRIORITY, 1)

return requester
except KeyError:
Expand Down
3 changes: 2 additions & 1 deletion synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)
from synapse.logging.context import run_in_background
from synapse.logging.opentracing import (
SynapseTags,
start_active_span,
start_active_span_from_request,
tags,
Expand Down Expand Up @@ -314,7 +315,7 @@ async def new_func(request, *args, **kwargs):
raise

request_tags = {
"request_id": request.get_request_id(),
SynapseTags.REQUEST_ID: request.get_request_id(),
tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER,
tags.HTTP_METHOD: request.get_method(),
tags.HTTP_URL: request.get_redacted_uri(),
Expand Down
21 changes: 17 additions & 4 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ class SynapseTags:
# Whether the sync response has new data to be returned to the client.
SYNC_RESULT = "sync.new_data"

# incoming HTTP request ID (as written in the logs)
REQUEST_ID = "request_id"

# HTTP request tag (used to distinguish full vs incremental syncs, etc)
REQUEST_TAG = "request_tag"


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

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

request_name = request.request_metrics.name
if extract_context:
scope = start_active_span_from_request(request, request_name, tags=request_tags)
scope = start_active_span_from_request(request, request_name)
else:
scope = start_active_span(request_name, tags=request_tags)
scope = start_active_span(request_name)

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

scope.span.set_tag("request_tag", request.request_metrics.start_context.tag)
# set the tags *after* the servlet completes, in case it decided to
# prioritise the span (tags will get dropped on unprioritised spans)
request_tags[
SynapseTags.REQUEST_TAG
] = request.request_metrics.start_context.tag

for k, v in request_tags.items():
scope.span.set_tag(k, v)
10 changes: 8 additions & 2 deletions synapse/metrics/background_process_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from twisted.internet import defer

from synapse.logging.context import LoggingContext, PreserveLoggingContext
from synapse.logging.opentracing import noop_context_manager, start_active_span
from synapse.logging.opentracing import (
SynapseTags,
noop_context_manager,
start_active_span,
)
from synapse.util.async_helpers import maybe_awaitable

if TYPE_CHECKING:
Expand Down Expand Up @@ -202,7 +206,9 @@ async def run():
try:
ctx = noop_context_manager()
if bg_start_span:
ctx = start_active_span(desc, tags={"request_id": str(context)})
ctx = start_active_span(
desc, tags={SynapseTags.REQUEST_ID: str(context)}
)
with ctx:
return await maybe_awaitable(func(*args, **kwargs))
except Exception:
Expand Down

0 comments on commit ed53bf3

Please sign in to comment.