def before(self, environ: dict, start_response: Callable) \ -> Tuple[dict, Callable]: """ Inject request time in CUL classic format, and start TTFB clock. Parameters ---------- environ : dict WSGI request environment. start_response : function Function used to begin the HTTP response. See https://www.python.org/dev/peps/pep-0333/#the-start-response-callable Returns ------- dict Request environment (``environ``). function ``start_response`` function. """ self.start = datetime.now() # Add the request ID as a uWSGI log variable. uwsgi.set_logvar('requestid', environ.get('REQUEST_ID', '')) # Express the time that the request was received in the classic # format. rtime = datetime.now(tz=EASTERN).strftime('%d/%b/%Y:%H:%M:%S %z') uwsgi.set_logvar('rtime', rtime) return environ, start_response
def set_logvar(key, value): """ Attempts to set the logvar in the request scope, or ignores it if not running in an environment that supports it. """ if value: if uwsgi: uwsgi.set_logvar(key, value)
def response(cls, data, request): try: parsed_data = request.parsed_data except BadRequest: parsed_data = {} if (isinstance(request, JSONRequest) and set(parsed_data.keys()) == {'id', 'method', 'params'}): response = {'id': parsed_data.get('id', 0)} if isinstance(data, TrytonException): response['error'] = data.args elif isinstance(data, Exception): # report exception back to server response['error'] = (str(data), data.__format_traceback__) else: response['result'] = data else: if isinstance(data, Exception): return InternalServerError(data) response = data # AKE: log RPC method (uwsgi and header) if uwsgi: uwsgi.set_logvar('rpc', parsed_data['method']) headers = Headers() headers.add('RPC-Method', parsed_data['method']) return Response(json.dumps(response, cls=JSONEncoder, separators=(',', ':')), content_type='application/json', headers=headers)
def __call__(self, environ, start_response): try: import uwsgi getter = environ.get request_id = getter('HTTP_X_REQUEST_ID', '') uwsgi.set_logvar('request_id', request_id) except ImportError: pass return super(RPTWSGIHandler, self).__call__(environ, start_response)
def middleware(env, start_response): # NOTE(kgriffs): For now, this is the only thing the # middleware does, so keep it inline. for header_name, logvar_name in logvar_map: try: value = env[header_name] except KeyError: value = 'None' uwsgi.set_logvar(logvar_name, value) # Carry on return app(env, start_response)
def process_request(self, request): # If this is a downstream request, use existing CID and return in response header cid = get_correlation_id(request) if cid: request.cid = cid request.propagate_response = True # Otherwise create a new CID and don't return in header else: request.cid = get_unique_id() request.propagate_response = False if uwsgi and hasattr(uwsgi, 'set_logvar'): uwsgi.set_logvar('cid', str(request.cid))
def log_uwsgi(request, response): # https://github.com/unbit/uwsgi-docs/blob/master/LogFormat.rst # http://qph.is.quoracdn.net/main-qimg-83b508e02a70cab9ffcb9fb454adead4 # http://qph.is.quoracdn.net/main-qimg-d2b92db82ed27aecc6bf5788d75e6a26 # http://qph.is.quoracdn.net/main-qimg-fde4d84bf459d14aff0ef930e4f8c7fe # TODO - http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python import socket, uwsgi try: hostname,alias,addresslist = socket.gethostbyaddr(request.remote_addr) except: hostname = 'Unknown' uwsgi.set_logvar('hostname', hostname)
def application(*args, **kwargs): response = djangoapplication(*args, **kwargs) if UWSGI: if hasattr(response, '_request'): request = getattr(response, '_request') if hasattr(request, 'user') and request.user.is_authenticated(): uwsgi.set_logvar('django_user', str(request.user)) else: uwsgi.set_logvar('django_user', '') uwsgi.set_logvar('django_dnt', str(getattr(request, 'DNT', None)).lower()) else: uwsgi.set_logvar('django_user', '') uwsgi.set_logvar('django_dnt', 'null') return response
def application(environ, start_response): global _application uwsgi.set_logvar('worker_id', str(uwsgi.worker_id())) if not os.environ.get('DJANGO_SETTINGS_MODULE'): os.environ['DJANGO_SETTINGS_MODULE'] = environ.get('DJANGO_SETTINGS_MODULE', 'settings') if _application is None: try: from django.core.wsgi import get_wsgi_application except ImportError: import django.core.handlers.wsgi _application = django.core.handlers.wsgi.WSGIHandler() else: _application = get_wsgi_application() return _application(environ, start_response)
def response(cls, data, request): try: parsed_data = request.parsed_data except BadRequest: parsed_data = {} if (isinstance(request, JSONRequest) and set(parsed_data.keys()) == {'id', 'method', 'params'}): response = {'id': parsed_data.get('id', 0)} if isinstance(data, TrytonException): response['error'] = data.args elif isinstance(data, Exception): # report exception back to server response['error'] = (str(data), data.__format_traceback__) else: response['result'] = data else: if isinstance(data, UserWarning): return Conflict(data) elif isinstance(data, LoginException): return Forbidden(data) elif isinstance(data, ConcurrencyException): return Locked(data) elif isinstance(data, RateLimitException): return TooManyRequests(data) elif isinstance(data, MissingDependenciesException): return InternalServerError(data) elif isinstance(data, TrytonException): return BadRequest(data) elif isinstance(data, Exception): return InternalServerError(data) response = data # AKE: log RPC method (uwsgi and header) if uwsgi: uwsgi.set_logvar(b'rpc', parsed_data['method'].encode('utf-8')) headers = Headers() headers.add('RPC-Method', parsed_data['method']) return Response(json.dumps(response, cls=JSONEncoder, separators=(',', ':')), content_type='application/json', headers=headers)
def after(self, response: Iterable) -> Iterable: """ Set TTFB log variable. Parameters ---------- response : iterable Iterable that generates the HTTP response. See https://www.python.org/dev/peps/pep-0333/#the-application-framework-side Returns ------- iterable Iterable that generates the HTTP response. See https://www.python.org/dev/peps/pep-0333/#the-application-framework-side """ # This is a close approximation of "TTFB" (time from the # request received to the start of the response). ttfb = (datetime.now() - self.start).microseconds uwsgi.set_logvar('ttfb', str(ttfb).encode('ascii')) return response
def dispatcher(router, request, response): try: if uwsgi is not None: uwsgi.set_logvar('request_id', request.id) except: # pylint: disable=bare-except request.logger.error("Error setting request_id log var", exc_info=True) try: rv = router.default_dispatcher(request, response) if rv is not None: response.write( json.dumps(rv, default=encoder.custom_json_serializer)) response.headers[ 'Content-Type'] = 'application/json; charset=utf-8' except webapp2.HTTPException as e: util.send_json_http_exception(response, str(e), e.code) except Exception as e: # pylint: disable=broad-except request.logger.error("Error dispatching request", exc_info=True) if config.get_item('core', 'debug'): message = traceback.format_exc() else: message = 'Internal Server Error' util.send_json_http_exception(response, message, 500)
def add_app_user_logvar(response): if uwsgi is not None and current_user.is_authenticated: uwsgi.set_logvar('app_user', current_user.username) return response
def process_request(self, request): if request.path_info == '/_hc': if uwsgi and hasattr(uwsgi, 'set_logvar'): uwsgi.set_logvar('cid', 'null') return HttpResponse(content_type='text/plain')
def set_logvar(key, value): """ Attempts to set the logvar in the request scope , or ignores it if not running in uwsgi """ if uwsgi and value: uwsgi.set_logvar(key, value)