requests.py (1776B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\beeline\patch\requests.py 2 import beeline 3 from wrapt import wrap_function_wrapper 4 import requests 5 6 def request(_request, instance, args, kwargs): 7 span = beeline.start_span(context={'meta.type': 'http_client'}) 8 b = beeline.get_beeline() 9 if b and b.http_trace_propagation_hook is not None: 10 new_headers = beeline.http_trace_propagation_hook() 11 if new_headers: 12 b.log('requests lib - adding trace context to outbound request: %s', new_headers) 13 instance.headers.update(new_headers) 14 else: 15 b.log('requests lib - no trace context found') 16 try: 17 resp = None 18 method = kwargs.get('method') or args[0] 19 beeline.add_context({'name': 'requests_%s' % method, 20 'request.method': method, 21 'request.url': kwargs.get('url') or args[1]}) 22 resp = _request(*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 is not None: 30 content_type = resp.headers.get('content-type') 31 if content_type: 32 beeline.add_context_field('response.content_type', content_type) 33 content_length = resp.headers.get('content-length') 34 if content_length: 35 beeline.add_context_field('response.content_length', content_length) 36 if hasattr(resp, 'status_code'): 37 beeline.add_context_field('response.status_code', resp.status_code) 38 beeline.finish_span(span) 39 40 41 wrap_function_wrapper('requests.sessions', 'Session.request', request)