3535
3636MODULE_NAME = 'requests'
3737
38- REQUESTS_WRAP_METHODS = ['get' , 'post' , 'put' , 'delete' , 'head' , 'options' ]
39- SESSION_WRAP_METHODS = 'request'
40- SESSION_CLASS_NAME = 'Session'
41-
4238HTTP_HOST = attributes_helper .COMMON_ATTRIBUTES ['HTTP_HOST' ]
4339HTTP_METHOD = attributes_helper .COMMON_ATTRIBUTES ['HTTP_METHOD' ]
4440HTTP_PATH = attributes_helper .COMMON_ATTRIBUTES ['HTTP_PATH' ]
@@ -57,85 +53,17 @@ def trace_integration(tracer=None):
5753 # not handle None being used in the execution context.
5854 execution_context .set_opencensus_tracer (tracer )
5955
60- # Wrap the requests functions
61- for func in REQUESTS_WRAP_METHODS :
62- requests_func = getattr (requests , func )
63- wrapped = wrap_requests (requests_func )
64- setattr (requests , requests_func .__name__ , wrapped )
65-
6656 # Wrap Session class
57+ # Since
58+ # https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1
59+ # (v0.7.0, Oct 23, 2011), get, post, etc are implemented via request which
60+ # again, is implemented via Session.request (`Session` was named `session`
61+ # before v1.0.0, Dec 17, 2012, see
62+ # https://github.com/psf/requests/commit/4e5c4a6ab7bb0195dececdd19bb8505b872fe120)
6763 wrapt .wrap_function_wrapper (
6864 MODULE_NAME , 'Session.request' , wrap_session_request )
6965
7066
71- def wrap_requests (requests_func ):
72- """Wrap the requests function to trace it."""
73- def call (url , * args , ** kwargs ):
74- # Check if request was sent from an exporter. If so, do not wrap.
75- if execution_context .is_exporter ():
76- return requests_func (url , * args , ** kwargs )
77- excludelist_hostnames = execution_context .get_opencensus_attr (
78- 'excludelist_hostnames' )
79- parsed_url = urlparse (url )
80- if parsed_url .port is None :
81- dest_url = parsed_url .hostname
82- else :
83- dest_url = '{}:{}' .format (parsed_url .hostname , parsed_url .port )
84- if utils .disable_tracing_hostname (dest_url , excludelist_hostnames ):
85- return requests_func (url , * args , ** kwargs )
86-
87- path = parsed_url .path if parsed_url .path else '/'
88-
89- _tracer = execution_context .get_opencensus_tracer ()
90- _span = _tracer .start_span ()
91- _span .name = '{}' .format (path )
92- _span .span_kind = span_module .SpanKind .CLIENT
93-
94- # Add the component type to attributes
95- _tracer .add_attribute_to_current_span (
96- "component" , "HTTP" )
97-
98- # Add the requests host to attributes
99- _tracer .add_attribute_to_current_span (
100- HTTP_HOST , dest_url )
101-
102- # Add the requests method to attributes
103- _tracer .add_attribute_to_current_span (
104- HTTP_METHOD , requests_func .__name__ .upper ())
105-
106- # Add the requests path to attributes
107- _tracer .add_attribute_to_current_span (
108- HTTP_PATH , path )
109-
110- # Add the requests url to attributes
111- _tracer .add_attribute_to_current_span (HTTP_URL , url )
112-
113- try :
114- result = requests_func (url , * args , ** kwargs )
115- except requests .Timeout :
116- _span .set_status (exceptions_status .TIMEOUT )
117- raise
118- except requests .URLRequired :
119- _span .set_status (exceptions_status .INVALID_URL )
120- raise
121- except Exception as e :
122- _span .set_status (exceptions_status .unknown (e ))
123- raise
124- else :
125- # Add the status code to attributes
126- _tracer .add_attribute_to_current_span (
127- HTTP_STATUS_CODE , result .status_code
128- )
129- _span .set_status (
130- utils .status_from_http_code (result .status_code )
131- )
132- return result
133- finally :
134- _tracer .end_span ()
135-
136- return call
137-
138-
13967def wrap_session_request (wrapped , instance , args , kwargs ):
14068 """Wrap the session function to trace it."""
14169 # Check if request was sent from an exporter. If so, do not wrap.
0 commit comments