def install()

in skywalking/plugins/sw_sanic.py [0:0]


def install():
    from sanic import Sanic, handlers, response
    # TODO: format_http1_response is removed from response in later versions.

    _format_http1_response = response.format_http1_response
    _handle_request = Sanic.handle_request
    _handlers_ErrorHandler_response = handlers.ErrorHandler.response # noqa

    def _sw_format_http1_response(status: int, headers, body=b''):
        if status is not None:
            entry_span = get_context().active_span
            if entry_span is not None and type(entry_span) is not NoopSpan:
                if status >= 400:
                    entry_span.error_occurred = True
                entry_span.tag(TagHttpStatusCode(status))

        return _format_http1_response(status, headers, body)

    def _sw_handlers_ErrorHandler_response(self: handlers.ErrorHandler, req, e): # noqa
        if e is not None:
            entry_span = get_context().active_span
            if entry_span is not None and type(entry_span) is not NoopSpan:
                entry_span.raised()

        return _handlers_ErrorHandler_response(self, req, e)

    response.format_http1_response = _sw_format_http1_response
    Sanic.handle_request = _gen_sw_handle_request(_handle_request)
    handlers.ErrorHandler.response = _sw_handlers_ErrorHandler_response