Log when we receive a request, when we send a response and how long it took to process it.

This commit is contained in:
Erik Johnston 2015-02-09 13:46:22 +00:00
parent ecb0f78063
commit 24cc6979fb
4 changed files with 24 additions and 5 deletions

View File

@ -67,7 +67,7 @@ class SynapseHomeServer(HomeServer):
return ClientV2AlphaRestResource(self) return ClientV2AlphaRestResource(self)
def build_resource_for_federation(self): def build_resource_for_federation(self):
return JsonResource() return JsonResource(self)
def build_resource_for_web_client(self): def build_resource_for_web_client(self):
syweb_path = os.path.dirname(syweb.__file__) syweb_path = os.path.dirname(syweb.__file__)

View File

@ -69,9 +69,10 @@ class JsonResource(HttpServer, resource.Resource):
_PathEntry = collections.namedtuple("_PathEntry", ["pattern", "callback"]) _PathEntry = collections.namedtuple("_PathEntry", ["pattern", "callback"])
def __init__(self): def __init__(self, hs):
resource.Resource.__init__(self) resource.Resource.__init__(self)
self.clock = hs.get_clock()
self.path_regexs = {} self.path_regexs = {}
def register_path(self, method, path_pattern, callback): def register_path(self, method, path_pattern, callback):
@ -111,6 +112,7 @@ class JsonResource(HttpServer, resource.Resource):
This checks if anyone has registered a callback for that method and This checks if anyone has registered a callback for that method and
path. path.
""" """
code = None
try: try:
# Just say yes to OPTIONS. # Just say yes to OPTIONS.
if request.method == "OPTIONS": if request.method == "OPTIONS":
@ -130,6 +132,13 @@ class JsonResource(HttpServer, resource.Resource):
urllib.unquote(u).decode("UTF-8") for u in m.groups() urllib.unquote(u).decode("UTF-8") for u in m.groups()
] ]
logger.info(
"Received request: %s %s",
request.method, request.path
)
start = self.clock.time_msec()
code, response = yield path_entry.callback( code, response = yield path_entry.callback(
request, request,
*args *args
@ -145,9 +154,11 @@ class JsonResource(HttpServer, resource.Resource):
logger.info("%s SynapseError: %s - %s", request, e.code, e.msg) logger.info("%s SynapseError: %s - %s", request, e.code, e.msg)
else: else:
logger.exception(e) logger.exception(e)
code = e.code
self._send_response( self._send_response(
request, request,
e.code, code,
cs_exception(e), cs_exception(e),
response_code_message=e.response_code_message response_code_message=e.response_code_message
) )
@ -158,6 +169,14 @@ class JsonResource(HttpServer, resource.Resource):
500, 500,
{"error": "Internal server error"} {"error": "Internal server error"}
) )
finally:
code = str(code) if code else "-"
end = self.clock.time_msec()
logger.info(
"Processed request: %dms %s %s %s",
end-start, code, request.method, request.path
)
def _send_response(self, request, code, response_json_object, def _send_response(self, request, code, response_json_object,
response_code_message=None): response_code_message=None):

View File

@ -25,7 +25,7 @@ class ClientV1RestResource(JsonResource):
"""A resource for version 1 of the matrix client API.""" """A resource for version 1 of the matrix client API."""
def __init__(self, hs): def __init__(self, hs):
JsonResource.__init__(self) JsonResource.__init__(self, hs)
self.register_servlets(self, hs) self.register_servlets(self, hs)
@staticmethod @staticmethod

View File

@ -25,7 +25,7 @@ class ClientV2AlphaRestResource(JsonResource):
"""A resource for version 2 alpha of the matrix client API.""" """A resource for version 2 alpha of the matrix client API."""
def __init__(self, hs): def __init__(self, hs):
JsonResource.__init__(self) JsonResource.__init__(self, hs)
self.register_servlets(self, hs) self.register_servlets(self, hs)
@staticmethod @staticmethod