| 52 | self.router = router |
| 53 | |
| 54 | def __call__(self, environ, start_response): |
| 55 | start_time = clock() |
| 56 | status_code = [] |
| 57 | content_length = [] |
| 58 | |
| 59 | request = Request(environ) |
| 60 | |
| 61 | query_params = request.GET.dict_of_lists() |
| 62 | |
| 63 | # Mask secret / sensitive query params |
| 64 | secret_query_params = SECRET_QUERY_PARAMS + cfg.CONF.log.mask_secrets_blacklist |
| 65 | for param_name in secret_query_params: |
| 66 | if param_name in query_params: |
| 67 | query_params[param_name] = MASKED_ATTRIBUTE_VALUE |
| 68 | |
| 69 | # Log the incoming request |
| 70 | values = { |
| 71 | "method": request.method, |
| 72 | "path": request.path, |
| 73 | "remote_addr": request.remote_addr, |
| 74 | "query": query_params, |
| 75 | "request_id": request.headers.get(REQUEST_ID_HEADER, None), |
| 76 | } |
| 77 | |
| 78 | LOG.info( |
| 79 | "%(request_id)s - %(method)s %(path)s with query=%(query)s" % values, |
| 80 | extra=values, |
| 81 | ) |
| 82 | |
| 83 | def custom_start_response(status, headers, exc_info=None): |
| 84 | status_code.append(int(status.split(" ")[0])) |
| 85 | |
| 86 | for name, value in headers: |
| 87 | if name.lower() == "content-length": |
| 88 | content_length.append(int(value)) |
| 89 | break |
| 90 | |
| 91 | return start_response(status, headers, exc_info) |
| 92 | |
| 93 | retval = self.app(environ, custom_start_response) |
| 94 | |
| 95 | try: |
| 96 | endpoint, path_vars = self.router.match(request) |
| 97 | except NotFoundException: |
| 98 | endpoint = {} |
| 99 | |
| 100 | log_result = endpoint.get("x-log-result", True) |
| 101 | |
| 102 | if isinstance(retval, (types.GeneratorType, itertools.chain)): |
| 103 | # Note: We don't log the result when return value is a generator, because this would |
| 104 | # result in calling str() on the generator and as such, exhausting it |
| 105 | content_length = [0] |
| 106 | log_result = False |
| 107 | |
| 108 | # Log the response |
| 109 | values = { |
| 110 | "method": request.method, |
| 111 | "path": request.path, |