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

Tag trace with instance name #13761

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/13761.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tag traces with the instance name to be able to easily jump into the right logs and filter traces by instance.
7 changes: 7 additions & 0 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from synapse.http import get_request_user_agent
from synapse.http.site import SynapseRequest
from synapse.logging.opentracing import (
SynapseTags,
active_span,
force_tracing,
start_active_span,
Expand Down Expand Up @@ -161,6 +162,12 @@ async def get_user_by_req(
parent_span.set_tag(
"authenticated_entity", requester.authenticated_entity
)
# We tag the Synapse instance name so that it's an easy jumping
# off point into the logs. Can also be used to filter for an
# instance that is under load.
parent_span.set_tag(
SynapseTags.INSTANCE_NAME, self.hs.get_instance_name()
)
parent_span.set_tag("user_id", requester.user.to_string())
if requester.device_id is not None:
parent_span.set_tag("device_id", requester.device_id)
Expand Down
6 changes: 4 additions & 2 deletions synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ class SynapseTags:
# Whether the sync response has new data to be returned to the client.
SYNC_RESULT = "sync.new_data"

INSTANCE_NAME = "instance_name"

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

Expand Down Expand Up @@ -1032,11 +1034,11 @@ def trace_servlet(
# with JsonResource).
scope.span.set_operation_name(request.request_metrics.name)

# set the tags *after* the servlet completes, in case it decided to
# prioritise the span (tags will get dropped on unprioritised spans)
Comment on lines -1035 to -1036
Copy link
Contributor Author

@MadLittleMods MadLittleMods Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the comment because it seems more relevant where we actually set the tags.

request_tags[
SynapseTags.REQUEST_TAG
] = request.request_metrics.start_context.tag
Copy link
Contributor Author

@MadLittleMods MadLittleMods Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I could just add the tag here instead but I don't know how to get access to the hs object here,

request_tags[SynapseTags.INSTANCE_NAME] = hs.get_instance_name()


# set the tags *after* the servlet completes, in case it decided to
# prioritise the span (tags will get dropped on unprioritised spans)
for k, v in request_tags.items():
scope.span.set_tag(k, v)