def _wrap_do_method()

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


def _wrap_do_method(handler, method):
    if hasattr(handler, f'do_{method}') and inspect.ismethod(getattr(handler, f'do_{method}')):
        _do_method = getattr(handler, f'do_{method}')

        def _sw_do_method():
            carrier = Carrier()
            for item in carrier:
                item.val = handler.headers[item.key.capitalize()]
            path = handler.path or '/'

            span = NoopSpan(NoopContext()) if config.ignore_http_method_check(method) \
                else get_context().new_entry_span(op=path.split('?')[0], carrier=carrier)

            with span:
                url = f"http://{handler.headers['Host']}{path}" if 'Host' in handler.headers else path
                span.layer = Layer.Http
                span.component = Component.General
                client_address = handler.client_address
                span.peer = f'{client_address[0]}:{client_address[1]}'
                span.tag(TagHttpMethod(method))
                span.tag(TagHttpURL(url))

                try:
                    _do_method()
                finally:
                    status_code = int(getattr(handler, '_status_code', -1))
                    if status_code > -1:
                        span.tag(TagHttpStatusCode(status_code))
                        if status_code >= 400:
                            span.error_occurred = True

        setattr(handler, f'do_{method}', _sw_do_method)