2016-04-22 11:09:55 -04:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
2018-05-09 18:05:14 -04:00
|
|
|
from twisted.web.server import Site, Request
|
|
|
|
|
2018-06-05 13:31:40 -04:00
|
|
|
from synapse.http import redact_uri
|
2018-05-09 18:05:14 -04:00
|
|
|
from synapse.http.request_metrics import RequestMetrics
|
|
|
|
from synapse.util.logcontext import LoggingContext
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-09 18:00:11 -04:00
|
|
|
_next_request_seq = 0
|
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
|
|
|
|
class SynapseRequest(Request):
|
2018-05-09 18:05:14 -04:00
|
|
|
"""Class which encapsulates an HTTP request to synapse.
|
|
|
|
|
|
|
|
All of the requests processed in synapse are of this type.
|
|
|
|
|
|
|
|
It extends twisted's twisted.web.server.Request, and adds:
|
|
|
|
* Unique request ID
|
|
|
|
* Redaction of access_token query-params in __repr__
|
|
|
|
* Logging at start and end
|
|
|
|
* Metrics to record CPU, wallclock and DB time by endpoint.
|
|
|
|
|
|
|
|
It provides a method `processing` which should be called by the Resource
|
|
|
|
which is handling the request, and returns a context manager.
|
|
|
|
|
|
|
|
"""
|
2016-04-22 11:09:55 -04:00
|
|
|
def __init__(self, site, *args, **kw):
|
|
|
|
Request.__init__(self, *args, **kw)
|
|
|
|
self.site = site
|
|
|
|
self.authenticated_entity = None
|
|
|
|
self.start_time = 0
|
|
|
|
|
2018-05-09 18:00:11 -04:00
|
|
|
global _next_request_seq
|
|
|
|
self.request_seq = _next_request_seq
|
|
|
|
_next_request_seq += 1
|
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
def __repr__(self):
|
|
|
|
# We overwrite this so that we don't log ``access_token``
|
2018-04-30 08:36:39 -04:00
|
|
|
return '<%s at 0x%x method=%r uri=%r clientproto=%r site=%r>' % (
|
2016-04-22 11:09:55 -04:00
|
|
|
self.__class__.__name__,
|
|
|
|
id(self),
|
|
|
|
self.method,
|
|
|
|
self.get_redacted_uri(),
|
|
|
|
self.clientproto,
|
|
|
|
self.site.site_tag,
|
|
|
|
)
|
|
|
|
|
2018-05-09 18:00:11 -04:00
|
|
|
def get_request_id(self):
|
|
|
|
return "%s-%i" % (self.method, self.request_seq)
|
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
def get_redacted_uri(self):
|
2018-06-05 13:31:40 -04:00
|
|
|
return redact_uri(self.uri)
|
2016-04-22 11:09:55 -04:00
|
|
|
|
|
|
|
def get_user_agent(self):
|
2018-04-03 14:41:21 -04:00
|
|
|
return self.requestHeaders.getRawHeaders(b"User-Agent", [None])[-1]
|
2016-04-22 11:09:55 -04:00
|
|
|
|
2018-05-10 13:46:59 -04:00
|
|
|
def render(self, resrc):
|
|
|
|
# override the Server header which is set by twisted
|
|
|
|
self.setHeader("Server", self.site.server_version_string)
|
|
|
|
return Request.render(self, resrc)
|
|
|
|
|
2018-05-09 18:05:14 -04:00
|
|
|
def _started_processing(self, servlet_name):
|
2018-05-28 05:10:27 -04:00
|
|
|
self.start_time = time.time()
|
2018-05-09 18:05:14 -04:00
|
|
|
self.request_metrics = RequestMetrics()
|
2018-05-21 11:03:39 -04:00
|
|
|
self.request_metrics.start(
|
|
|
|
self.start_time, name=servlet_name, method=self.method,
|
|
|
|
)
|
2018-05-09 18:05:14 -04:00
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
self.site.access_logger.info(
|
|
|
|
"%s - %s - Received request: %s %s",
|
|
|
|
self.getClientIP(),
|
|
|
|
self.site.site_tag,
|
|
|
|
self.method,
|
|
|
|
self.get_redacted_uri()
|
|
|
|
)
|
|
|
|
|
2018-05-09 18:05:14 -04:00
|
|
|
def _finished_processing(self):
|
2016-04-22 11:09:55 -04:00
|
|
|
try:
|
|
|
|
context = LoggingContext.current_context()
|
|
|
|
ru_utime, ru_stime = context.get_resource_usage()
|
|
|
|
db_txn_count = context.db_txn_count
|
2018-05-28 05:10:27 -04:00
|
|
|
db_txn_duration_sec = context.db_txn_duration_sec
|
|
|
|
db_sched_duration_sec = context.db_sched_duration_sec
|
2018-06-21 01:15:03 -04:00
|
|
|
evt_db_fetch_count = context.evt_db_fetch_count
|
2017-10-23 10:52:32 -04:00
|
|
|
except Exception:
|
2016-04-22 11:09:55 -04:00
|
|
|
ru_utime, ru_stime = (0, 0)
|
2018-05-28 05:10:27 -04:00
|
|
|
db_txn_count, db_txn_duration_sec = (0, 0)
|
2018-06-21 01:15:03 -04:00
|
|
|
evt_db_fetch_count = 0
|
2016-04-22 11:09:55 -04:00
|
|
|
|
2018-05-28 05:10:27 -04:00
|
|
|
end_time = time.time()
|
2018-05-09 18:05:14 -04:00
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
self.site.access_logger.info(
|
|
|
|
"%s - %s - {%s}"
|
2018-05-28 05:10:27 -04:00
|
|
|
" Processed request: %.3fsec (%.3fsec, %.3fsec) (%.3fsec/%.3fsec/%d)"
|
2018-06-21 01:15:03 -04:00
|
|
|
" %sB %s \"%s %s %s\" \"%s\" [%d dbevts]",
|
2016-04-22 11:09:55 -04:00
|
|
|
self.getClientIP(),
|
|
|
|
self.site.site_tag,
|
don't mix unicode strings with utf8-in-byte-strings
otherwise we explode with:
```
Traceback (most recent call last):
File /usr/lib/python2.7/logging/handlers.py, line 78, in emit
logging.FileHandler.emit(self, record)
File /usr/lib/python2.7/logging/__init__.py, line 950, in emit
StreamHandler.emit(self, record)
File /usr/lib/python2.7/logging/__init__.py, line 887, in emit
self.handleError(record)
File /usr/lib/python2.7/logging/__init__.py, line 810, in handleError
None, sys.stderr)
File /usr/lib/python2.7/traceback.py, line 124, in print_exception
_print(file, 'Traceback (most recent call last):')
File /usr/lib/python2.7/traceback.py, line 13, in _print
file.write(str+terminator)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_io.py, line 170, in write
self.log.emit(self.level, format=u{log_io}, log_io=line)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 144, in emit
self.observer(event)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 136, in __call__
errorLogger = self._errorLoggerForObserver(brokenObserver)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 156, in _errorLoggerForObserver
if obs is not observer
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 81, in __init__
self.log = Logger(observer=self)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 64, in __init__
namespace = self._namespaceFromCallingContext()
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 42, in _namespaceFromCallingContext
return currentframe(2).f_globals[__name__]
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/python/compat.py, line 93, in currentframe
for x in range(n + 1):
RuntimeError: maximum recursion depth exceeded while calling a Python object
Logged from file site.py, line 129
File /usr/lib/python2.7/logging/__init__.py, line 859, in emit
msg = self.format(record)
File /usr/lib/python2.7/logging/__init__.py, line 732, in format
return fmt.format(record)
File /usr/lib/python2.7/logging/__init__.py, line 471, in format
record.message = record.getMessage()
File /usr/lib/python2.7/logging/__init__.py, line 335, in getMessage
msg = msg % self.args
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128)
Logged from file site.py, line 129
```
...where the logger apparently recurses whilst trying to log the error, hitting the
maximum recursion depth and killing everything badly.
2018-07-01 00:08:51 -04:00
|
|
|
# need to decode as it could be raw utf-8 bytes
|
|
|
|
# from a IDN servname in an auth header
|
|
|
|
self.authenticated_entity.decode("utf-8"),
|
2018-05-09 18:05:14 -04:00
|
|
|
end_time - self.start_time,
|
2018-05-28 05:10:27 -04:00
|
|
|
ru_utime,
|
|
|
|
ru_stime,
|
|
|
|
db_sched_duration_sec,
|
|
|
|
db_txn_duration_sec,
|
2016-04-22 11:09:55 -04:00
|
|
|
int(db_txn_count),
|
|
|
|
self.sentLength,
|
|
|
|
self.code,
|
|
|
|
self.method,
|
|
|
|
self.get_redacted_uri(),
|
|
|
|
self.clientproto,
|
don't mix unicode strings with utf8-in-byte-strings
otherwise we explode with:
```
Traceback (most recent call last):
File /usr/lib/python2.7/logging/handlers.py, line 78, in emit
logging.FileHandler.emit(self, record)
File /usr/lib/python2.7/logging/__init__.py, line 950, in emit
StreamHandler.emit(self, record)
File /usr/lib/python2.7/logging/__init__.py, line 887, in emit
self.handleError(record)
File /usr/lib/python2.7/logging/__init__.py, line 810, in handleError
None, sys.stderr)
File /usr/lib/python2.7/traceback.py, line 124, in print_exception
_print(file, 'Traceback (most recent call last):')
File /usr/lib/python2.7/traceback.py, line 13, in _print
file.write(str+terminator)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_io.py, line 170, in write
self.log.emit(self.level, format=u{log_io}, log_io=line)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 144, in emit
self.observer(event)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 136, in __call__
errorLogger = self._errorLoggerForObserver(brokenObserver)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 156, in _errorLoggerForObserver
if obs is not observer
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_observer.py, line 81, in __init__
self.log = Logger(observer=self)
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 64, in __init__
namespace = self._namespaceFromCallingContext()
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/logger/_logger.py, line 42, in _namespaceFromCallingContext
return currentframe(2).f_globals[__name__]
File /home/matrix/.synapse/local/lib/python2.7/site-packages/twisted/python/compat.py, line 93, in currentframe
for x in range(n + 1):
RuntimeError: maximum recursion depth exceeded while calling a Python object
Logged from file site.py, line 129
File /usr/lib/python2.7/logging/__init__.py, line 859, in emit
msg = self.format(record)
File /usr/lib/python2.7/logging/__init__.py, line 732, in format
return fmt.format(record)
File /usr/lib/python2.7/logging/__init__.py, line 471, in format
record.message = record.getMessage()
File /usr/lib/python2.7/logging/__init__.py, line 335, in getMessage
msg = msg % self.args
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128)
Logged from file site.py, line 129
```
...where the logger apparently recurses whilst trying to log the error, hitting the
maximum recursion depth and killing everything badly.
2018-07-01 00:08:51 -04:00
|
|
|
# need to decode as could be raw utf-8 bytes
|
|
|
|
# from a utf-8 user-agent.
|
|
|
|
# N.B. if you don't do this, the logger explodes
|
|
|
|
# with maximum recursion trying to log errors about
|
|
|
|
# the charset problem.
|
|
|
|
self.get_user_agent().decode("utf-8"),
|
2018-06-21 01:15:03 -04:00
|
|
|
evt_db_fetch_count,
|
2016-04-22 11:09:55 -04:00
|
|
|
)
|
|
|
|
|
2018-05-09 18:05:14 -04:00
|
|
|
try:
|
|
|
|
self.request_metrics.stop(end_time, self)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warn("Failed to stop metrics: %r", e)
|
|
|
|
|
2016-04-22 11:09:55 -04:00
|
|
|
@contextlib.contextmanager
|
2018-05-09 18:05:14 -04:00
|
|
|
def processing(self, servlet_name):
|
|
|
|
"""Record the fact that we are processing this request.
|
|
|
|
|
|
|
|
Returns a context manager; the correct way to use this is:
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def handle_request(request):
|
|
|
|
with request.processing("FooServlet"):
|
|
|
|
yield really_handle_the_request()
|
|
|
|
|
|
|
|
This will log the request's arrival. Once the context manager is
|
|
|
|
closed, the completion of the request will be logged, and the various
|
|
|
|
metrics will be updated.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
servlet_name (str): the name of the servlet which will be
|
|
|
|
processing this request. This is used in the metrics.
|
|
|
|
|
|
|
|
It is possible to update this afterwards by updating
|
|
|
|
self.request_metrics.servlet_name.
|
|
|
|
"""
|
2018-05-10 13:46:59 -04:00
|
|
|
# TODO: we should probably just move this into render() and finish(),
|
|
|
|
# to save having to call a separate method.
|
2018-05-09 18:05:14 -04:00
|
|
|
self._started_processing(servlet_name)
|
2016-04-22 11:09:55 -04:00
|
|
|
yield
|
2018-05-09 18:05:14 -04:00
|
|
|
self._finished_processing()
|
2016-04-22 11:09:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
class XForwardedForRequest(SynapseRequest):
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
SynapseRequest.__init__(self, *args, **kw)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Add a layer on top of another request that only uses the value of an
|
|
|
|
X-Forwarded-For header as the result of C{getClientIP}.
|
|
|
|
"""
|
|
|
|
def getClientIP(self):
|
|
|
|
"""
|
|
|
|
@return: The client address (the first address) in the value of the
|
|
|
|
I{X-Forwarded-For header}. If the header is not present, return
|
|
|
|
C{b"-"}.
|
|
|
|
"""
|
|
|
|
return self.requestHeaders.getRawHeaders(
|
|
|
|
b"x-forwarded-for", [b"-"])[0].split(b",")[0].strip()
|
|
|
|
|
|
|
|
|
|
|
|
class SynapseRequestFactory(object):
|
|
|
|
def __init__(self, site, x_forwarded_for):
|
|
|
|
self.site = site
|
|
|
|
self.x_forwarded_for = x_forwarded_for
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
if self.x_forwarded_for:
|
|
|
|
return XForwardedForRequest(self.site, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
return SynapseRequest(self.site, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class SynapseSite(Site):
|
|
|
|
"""
|
|
|
|
Subclass of a twisted http Site that does access logging with python's
|
|
|
|
standard logging
|
|
|
|
"""
|
2018-05-10 13:46:59 -04:00
|
|
|
def __init__(self, logger_name, site_tag, config, resource,
|
|
|
|
server_version_string, *args, **kwargs):
|
2016-04-22 11:09:55 -04:00
|
|
|
Site.__init__(self, resource, *args, **kwargs)
|
|
|
|
|
|
|
|
self.site_tag = site_tag
|
|
|
|
|
|
|
|
proxied = config.get("x_forwarded", False)
|
|
|
|
self.requestFactory = SynapseRequestFactory(self, proxied)
|
|
|
|
self.access_logger = logging.getLogger(logger_name)
|
2018-05-10 13:46:59 -04:00
|
|
|
self.server_version_string = server_version_string
|
2016-04-22 11:09:55 -04:00
|
|
|
|
|
|
|
def log(self, request):
|
|
|
|
pass
|