NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

urllib.py (1739B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\beeline\patch\urllib.py
      2 from wrapt import wrap_function_wrapper
      3 import beeline
      4 import beeline.propagation
      5 import urllib.request
      6 
      7 def _urllibopen(_urlopen, instance, args, kwargs):
      8     if type(args[0]) != urllib.request.Request:
      9         args = (urllib.request.Request(args[0]),) + tuple(args[1:])
     10     span = beeline.start_span(context={'meta.type': 'http_client'})
     11     b = beeline.get_beeline()
     12     if b and b.http_trace_propagation_hook is not None:
     13         new_headers = beeline.http_trace_propagation_hook()
     14         if new_headers:
     15             b.log('urllib lib - adding trace context to outbound request: %s', new_headers)
     16             args[0].headers.update(new_headers)
     17     try:
     18         resp = None
     19         beeline.add_context({'name': 'urllib_%s' % args[0].get_method(),
     20          'request.method': args[0].get_method(),
     21          'request.uri': args[0].full_url})
     22         resp = _urlopen(*args, **kwargs)
     23         return resp
     24     except Exception as e:
     25         beeline.add_context({'request.error_type': str(type(e)),
     26          'request.error': beeline.internal.stringify_exception(e)})
     27         raise
     28     finally:
     29         if resp:
     30             beeline.add_context_field('response.status_code', resp.status)
     31             content_type = resp.getheader('content-type')
     32             if content_type:
     33                 beeline.add_context_field('response.content_type', content_type)
     34             content_length = resp.getheader('content-length')
     35             if content_length:
     36                 beeline.add_context_field('response.content_length', content_length)
     37         beeline.finish_span(span)
     38 
     39 
     40 wrap_function_wrapper('urllib.request', 'urlopen', _urllibopen)