def get_request(self): pipeline = redis_client.pipeline() key = self.request_key pipeline.hgetall(key) key = self.request_headers_key pipeline.hgetall(key) key = self.response_key pipeline.hgetall(key) key = self.reponse_headers_key pipeline.hgetall(key) http_request, request_headers, response, response_headers = pipeline.execute( ) if not http_request: return HttpResponse(status=404) try: content = response.get('content') content = json.loads(content) except Exception: content = 'HTTP monitor json decode error.' response['content'] = content result = { 'request': http_request, 'request_headers': request_headers, 'response': response, 'response_headers': response_headers } return result
def add_request(self, request, response): path = request.get_full_path() pipeline = redis_client.pipeline() pipeline.rpush(self.list_key, self.request_id) pipeline.ltrim(self.list_key, -10000, -1) key = self.request_key pipeline.hmset( key, { 'path': path, 'method': request.method, 'body': request._http_request_body, 'host': request.META.get('HTTP_HOST'), 'status_code': response.status_code, 'request_id': self.request_id, 'created_at': datetime.now().isoformat() }) pipeline.expire(key, expire_seconds) key = self.request_headers_key headers = { key: value for key, value in request.META.items() if key.startswith('HTTP_') } pipeline.hmset(key, headers) pipeline.expire(key, expire_seconds) key = self.response_key content = response.content pipeline.hmset( key, { 'status_code': response.status_code, 'content': content, 'host': request.META.get('HTTP_HOST'), }) pipeline.expire(key, expire_seconds) key = self.response_headers_key headers = { key: ', '.join(value) for key, value in response._headers.items() } pipeline.hmset(key, headers) pipeline.expire(key, expire_seconds) key = self.request_user user_info = dict() if hasattr(request, 'user'): user_info = dict(username=request.user.id, id=request.user.username) pipeline.hmset(key, user_info) pipeline.expire(key, expire_seconds) pipeline.execute() return self.request_id
def get_requests(self, size, page): request_ids = redis_client.lrange(self.list_key, -size * page, -((page - 1) * size + 1)) request_ids.reverse() pipeline = redis_client.pipeline() for request_id in request_ids: item_key_base = self.key_base + '{requests_id}'.format( requests_id=request_id) key = item_key_base + ':request' pipeline.hgetall(key) return pipeline.execute()
def requests(request): size = int(request.GET.get('size', 20)) page = int(request.GET.get('page', 1)) request_ids = redis_client.lrange(store_prefix + 'requests:requests-list', -size * page, -((page - 1) * size + 1)) request_ids.reverse() pipeline = redis_client.pipeline() for request_id in request_ids: item_key_base = store_prefix + 'requests:{requests_id}'.format( requests_id=request_id) key = item_key_base + ':request' pipeline.hgetall(key) http_requests = pipeline.execute() return HttpResponse(json.dumps(http_requests), content_type='application/json')
def request(request, request_id): item_key_base = store_prefix + 'requests:{requests_id}'.format( requests_id=request_id) pipeline = redis_client.pipeline() key = item_key_base + ':request' pipeline.hgetall(key) key = item_key_base + ':request-headers' pipeline.hgetall(key) key = item_key_base + ':response' pipeline.hgetall(key) key = item_key_base + ':response-headers' pipeline.hgetall(key) http_request, request_headers, response, response_headers = pipeline.execute( ) if not http_request: return HttpResponse(status=404) try: content = response.get('content') content = json.loads(content) except Exception: content = 'HTTP monitor json decode error.' response['content'] = content result = { 'request': http_request, 'request_headers': request_headers, 'response': response, 'response_headers': response_headers } return HttpResponse(json.dumps(result), content_type='application/json')
def process_response(self, request, response): path = request.path if not settings.DEBUG: return response for url_prefix in url_prefix_list: if not path.startswith(url_prefix): return response for url_prefix in exclude_url_prefix_list: if path.startswith(url_prefix): return response request_id = str(uuid.uuid4()) pipeline = redis_client.pipeline() list_key = store_prefix + 'requests:requests-list' pipeline.rpush(list_key, request_id) pipeline.ltrim(list_key, 0, 100000) item_key_base = store_prefix + 'requests:{requests_id}'.format( requests_id=request_id) key = item_key_base + ':request' pipeline.hmset( key, { 'path': path, 'method': request.method, 'body': request._http_request_body, 'host': request.META.get('HTTP_HOST'), 'status_code': response.status_code, 'request_id': request_id, 'created_at': datetime.now().isoformat() }) pipeline.expire(key, expire_seconds) key = item_key_base + ':request-headers' headers = { key: value for key, value in request.META.items() if key.startswith('HTTP_') } pipeline.hmset(key, headers) pipeline.expire(key, expire_seconds) key = item_key_base + ':response' content = response.content pipeline.hmset( key, { 'status_code': response.status_code, 'content': content, 'charset': response.charset, 'host': request.META.get('HTTP_HOST'), }) pipeline.expire(key, expire_seconds) key = item_key_base + ':response-headers' headers = { key: ', '.join(value) for key, value in response._headers.items() } pipeline.hmset(key, headers) pipeline.expire(key, expire_seconds) pipeline.execute() response['Request-UUID'] = request_id return response