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

Commit

Permalink
Add opentracing to all client servlets (#5983)
Browse files Browse the repository at this point in the history
  • Loading branch information
JorikSchellekens committed Sep 5, 2019
1 parent a0d294c commit 909827b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.d/5983.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add minimum opentracing for client servlets.
6 changes: 5 additions & 1 deletion synapse/federation/transport/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ def register(self, server):
continue

server.register_paths(
method, (pattern,), self._wrap(code), self.__class__.__name__
method,
(pattern,),
self._wrap(code),
self.__class__.__name__,
trace=False,
)


Expand Down
13 changes: 12 additions & 1 deletion synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
UnrecognizedRequestError,
)
from synapse.logging.context import preserve_fn
from synapse.logging.opentracing import trace_servlet
from synapse.util.caches import intern_dict

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -257,7 +258,9 @@ def __init__(self, hs, canonical_json=True):
self.path_regexs = {}
self.hs = hs

def register_paths(self, method, path_patterns, callback, servlet_classname):
def register_paths(
self, method, path_patterns, callback, servlet_classname, trace=True
):
"""
Registers a request handler against a regular expression. Later request URLs are
checked against these regular expressions in order to identify an appropriate
Expand All @@ -273,8 +276,16 @@ def register_paths(self, method, path_patterns, callback, servlet_classname):
servlet_classname (str): The name of the handler to be used in prometheus
and opentracing logs.
trace (bool): Whether we should start a span to trace the servlet.
"""
method = method.encode("utf-8") # method is bytes on py3

if trace:
# We don't extract the context from the servlet because we can't
# trust the sender
callback = trace_servlet(servlet_classname)(callback)

for path_pattern in path_patterns:
logger.debug("Registering for %s %s", method, path_pattern.pattern)
self.path_regexs.setdefault(method, []).append(
Expand Down
6 changes: 1 addition & 5 deletions synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from canonicaljson import json

from synapse.api.errors import Codes, SynapseError
from synapse.logging.opentracing import trace_servlet

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -298,10 +297,7 @@ def register(self, http_server):
servlet_classname = self.__class__.__name__
method_handler = getattr(self, "on_%s" % (method,))
http_server.register_paths(
method,
patterns,
trace_servlet(servlet_classname)(method_handler),
servlet_classname,
method, patterns, method_handler, servlet_classname
)

else:
Expand Down
2 changes: 1 addition & 1 deletion synapse/logging/opentracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def whitelisted_homeserver(destination):
Args:
destination (str)
"""
_homeserver_whitelist

if _homeserver_whitelist:
return _homeserver_whitelist.match(destination)
return False
Expand Down
16 changes: 6 additions & 10 deletions synapse/replication/http/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

from twisted.internet import defer

import synapse.logging.opentracing as opentracing
from synapse.api.errors import (
CodeMessageException,
HttpResponseException,
RequestSendFailed,
SynapseError,
)
from synapse.logging.opentracing import inject_active_span_byte_dict, trace_servlet
from synapse.util.caches.response_cache import ResponseCache
from synapse.util.stringutils import random_string

Expand Down Expand Up @@ -167,9 +167,7 @@ def send_request(**kwargs):
# the master, and so whether we should clean up or not.
while True:
headers = {}
opentracing.inject_active_span_byte_dict(
headers, None, check_destination=False
)
inject_active_span_byte_dict(headers, None, check_destination=False)
try:
result = yield request_func(uri, data, headers=headers)
break
Expand Down Expand Up @@ -210,13 +208,11 @@ def register(self, http_server):
args = "/".join("(?P<%s>[^/]+)" % (arg,) for arg in url_args)
pattern = re.compile("^/_synapse/replication/%s/%s$" % (self.NAME, args))

handler = trace_servlet(self.__class__.__name__, extract_context=True)(handler)
# We don't let register paths trace this servlet using the default tracing
# options because we wish to extract the context explicitly.
http_server.register_paths(
method,
[pattern],
opentracing.trace_servlet(self.__class__.__name__, extract_context=True)(
handler
),
self.__class__.__name__,
method, [pattern], handler, self.__class__.__name__, trace=False
)

def _cached_handler(self, request, txn_id, **kwargs):
Expand Down

0 comments on commit 909827b

Please sign in to comment.