Add opentracing to all client servlets (#5983)

This commit is contained in:
Jorik Schellekens 2019-09-05 14:46:04 +01:00 committed by GitHub
parent a0d294c306
commit 909827b422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 18 deletions

1
changelog.d/5983.feature Normal file
View File

@ -0,0 +1 @@
Add minimum opentracing for client servlets.

View File

@ -342,7 +342,11 @@ class BaseFederationServlet(object):
continue continue
server.register_paths( server.register_paths(
method, (pattern,), self._wrap(code), self.__class__.__name__ method,
(pattern,),
self._wrap(code),
self.__class__.__name__,
trace=False,
) )

View File

@ -40,6 +40,7 @@ from synapse.api.errors import (
UnrecognizedRequestError, UnrecognizedRequestError,
) )
from synapse.logging.context import preserve_fn from synapse.logging.context import preserve_fn
from synapse.logging.opentracing import trace_servlet
from synapse.util.caches import intern_dict from synapse.util.caches import intern_dict
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -257,7 +258,9 @@ class JsonResource(HttpServer, resource.Resource):
self.path_regexs = {} self.path_regexs = {}
self.hs = hs 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 Registers a request handler against a regular expression. Later request URLs are
checked against these regular expressions in order to identify an appropriate checked against these regular expressions in order to identify an appropriate
@ -273,8 +276,16 @@ class JsonResource(HttpServer, resource.Resource):
servlet_classname (str): The name of the handler to be used in prometheus servlet_classname (str): The name of the handler to be used in prometheus
and opentracing logs. 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 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: for path_pattern in path_patterns:
logger.debug("Registering for %s %s", method, path_pattern.pattern) logger.debug("Registering for %s %s", method, path_pattern.pattern)
self.path_regexs.setdefault(method, []).append( self.path_regexs.setdefault(method, []).append(

View File

@ -20,7 +20,6 @@ import logging
from canonicaljson import json from canonicaljson import json
from synapse.api.errors import Codes, SynapseError from synapse.api.errors import Codes, SynapseError
from synapse.logging.opentracing import trace_servlet
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -298,10 +297,7 @@ class RestServlet(object):
servlet_classname = self.__class__.__name__ servlet_classname = self.__class__.__name__
method_handler = getattr(self, "on_%s" % (method,)) method_handler = getattr(self, "on_%s" % (method,))
http_server.register_paths( http_server.register_paths(
method, method, patterns, method_handler, servlet_classname
patterns,
trace_servlet(servlet_classname)(method_handler),
servlet_classname,
) )
else: else:

View File

@ -319,7 +319,7 @@ def whitelisted_homeserver(destination):
Args: Args:
destination (str) destination (str)
""" """
_homeserver_whitelist
if _homeserver_whitelist: if _homeserver_whitelist:
return _homeserver_whitelist.match(destination) return _homeserver_whitelist.match(destination)
return False return False

View File

@ -22,13 +22,13 @@ from six.moves import urllib
from twisted.internet import defer from twisted.internet import defer
import synapse.logging.opentracing as opentracing
from synapse.api.errors import ( from synapse.api.errors import (
CodeMessageException, CodeMessageException,
HttpResponseException, HttpResponseException,
RequestSendFailed, RequestSendFailed,
SynapseError, SynapseError,
) )
from synapse.logging.opentracing import inject_active_span_byte_dict, trace_servlet
from synapse.util.caches.response_cache import ResponseCache from synapse.util.caches.response_cache import ResponseCache
from synapse.util.stringutils import random_string from synapse.util.stringutils import random_string
@ -167,9 +167,7 @@ class ReplicationEndpoint(object):
# the master, and so whether we should clean up or not. # the master, and so whether we should clean up or not.
while True: while True:
headers = {} headers = {}
opentracing.inject_active_span_byte_dict( inject_active_span_byte_dict(headers, None, check_destination=False)
headers, None, check_destination=False
)
try: try:
result = yield request_func(uri, data, headers=headers) result = yield request_func(uri, data, headers=headers)
break break
@ -210,13 +208,11 @@ class ReplicationEndpoint(object):
args = "/".join("(?P<%s>[^/]+)" % (arg,) for arg in url_args) args = "/".join("(?P<%s>[^/]+)" % (arg,) for arg in url_args)
pattern = re.compile("^/_synapse/replication/%s/%s$" % (self.NAME, 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( http_server.register_paths(
method, method, [pattern], handler, self.__class__.__name__, trace=False
[pattern],
opentracing.trace_servlet(self.__class__.__name__, extract_context=True)(
handler
),
self.__class__.__name__,
) )
def _cached_handler(self, request, txn_id, **kwargs): def _cached_handler(self, request, txn_id, **kwargs):