django 请求处理流程 链路追踪

 

 

class BaseMiddleware:

    # https://github.com/django/django/blob/master/tests/utils_tests/test_decorators.py

    def __init__(self, get_response):

        self.get_response = get_response

 

    def __call__(self, request):

        return self.get_response(request)

 

 

在请求响应头中加入request_id

class StraceRequestMiddleware(BaseMiddleware):

    def __call__(self, request):

        response = self.get_response(request)

        # 'WSGIRequest' object has no attribute 'process_view_RequestId'

        # TODO

        try:

            response['X-REQUEST-ID'] = request.process_view_RequestId

        except:

            pass

        return response

 

    def process_view(self, request, view_func, view_args, view_kwargs):

        if not hasattr(request, 'process_view_RequestId'):

            setattr(request, 'process_view_RequestId', createRequestId())

 

posted @ 2019-03-04 12:47  papering  阅读(550)  评论(0编辑  收藏  举报