in skywalking/plugins/sw_http_server.py [0:0]
def wrap_werkzeug_request_handler(handler):
"""
Wrap run_wsgi of werkzeug.serving.WSGIRequestHandler to add skywalking instrument code.
"""
_run_wsgi = handler.run_wsgi
def _wrap_run_wsgi():
carrier = Carrier()
method = handler.command
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:
return _run_wsgi()
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
handler.run_wsgi = _wrap_run_wsgi
def _sw_send_response(self, code, *args, **kwargs):
self._status_code = code
return _send_response(self, code, *args, **kwargs)
WSGIRequestHandler = handler.__class__ # noqa
if not getattr(WSGIRequestHandler, '_sw_wrapped', False):
_send_response = WSGIRequestHandler.send_response
WSGIRequestHandler.send_response = _sw_send_response
WSGIRequestHandler._sw_wrapped = True