2121 from urlparse import urlparse
2222
2323from opencensus .trace import attributes_helper
24+ from opencensus .trace import exceptions_status
2425from opencensus .trace import execution_context
2526from opencensus .trace import span as span_module
2627from opencensus .trace import utils
3334SESSION_WRAP_METHODS = 'request'
3435SESSION_CLASS_NAME = 'Session'
3536
36- HTTP_URL = attributes_helper .COMMON_ATTRIBUTES ['HTTP_URL' ]
37+ HTTP_HOST = attributes_helper .COMMON_ATTRIBUTES ['HTTP_HOST' ]
38+ HTTP_METHOD = attributes_helper .COMMON_ATTRIBUTES ['HTTP_METHOD' ]
39+ HTTP_PATH = attributes_helper .COMMON_ATTRIBUTES ['HTTP_PATH' ]
40+ HTTP_ROUTE = attributes_helper .COMMON_ATTRIBUTES ['HTTP_ROUTE' ]
3741HTTP_STATUS_CODE = attributes_helper .COMMON_ATTRIBUTES ['HTTP_STATUS_CODE' ]
42+ HTTP_URL = attributes_helper .COMMON_ATTRIBUTES ['HTTP_URL' ]
3843
3944
4045def trace_integration (tracer = None ):
@@ -74,22 +79,47 @@ def call(url, *args, **kwargs):
7479 if utils .disable_tracing_hostname (dest_url , blacklist_hostnames ):
7580 return requests_func (url , * args , ** kwargs )
7681
82+ path = parsed_url .path if parsed_url .path else '/'
83+
7784 _tracer = execution_context .get_opencensus_tracer ()
7885 _span = _tracer .start_span ()
79- _span .name = '[requests] {}' .format (requests_func . __name__ )
86+ _span .name = '{}' .format (path )
8087 _span .span_kind = span_module .SpanKind .CLIENT
8188
82- # Add the requests url to attributes
83- _tracer .add_attribute_to_current_span (HTTP_URL , url )
89+ # Add the requests host to attributes
90+ _tracer .add_attribute_to_current_span (
91+ HTTP_HOST , dest_url )
8492
85- result = requests_func (url , * args , ** kwargs )
93+ # Add the requests method to attributes
94+ _tracer .add_attribute_to_current_span (
95+ HTTP_METHOD , requests_func .__name__ .upper ())
8696
87- # Add the status code to attributes
97+ # Add the requests path to attributes
8898 _tracer .add_attribute_to_current_span (
89- HTTP_STATUS_CODE , str ( result . status_code ) )
99+ HTTP_PATH , path )
90100
91- _tracer .end_span ()
92- return result
101+ # Add the requests url to attributes
102+ _tracer .add_attribute_to_current_span (HTTP_URL , url )
103+
104+ try :
105+ result = requests_func (url , * args , ** kwargs )
106+ except requests .Timeout :
107+ _span .set_status (exceptions_status .TIMEOUT )
108+ except requests .URLRequired :
109+ _span .set_status (exceptions_status .INVALID_URL )
110+ except Exception as e :
111+ _span .set_status (exceptions_status .unknown (e ))
112+ else :
113+ # Add the status code to attributes
114+ _tracer .add_attribute_to_current_span (
115+ HTTP_STATUS_CODE , result .status_code
116+ )
117+ _span .set_status (
118+ utils .status_from_http_code (result .status_code )
119+ )
120+ return result
121+ finally :
122+ _tracer .end_span ()
93123
94124 return call
95125
@@ -113,10 +143,12 @@ def wrap_session_request(wrapped, instance, args, kwargs):
113143 if utils .disable_tracing_hostname (dest_url , blacklist_hostnames ):
114144 return wrapped (* args , ** kwargs )
115145
146+ path = parsed_url .path if parsed_url .path else '/'
147+
116148 _tracer = execution_context .get_opencensus_tracer ()
117149 _span = _tracer .start_span ()
118150
119- _span .name = '[requests] {}' .format (method )
151+ _span .name = '{}' .format (path )
120152 _span .span_kind = span_module .SpanKind .CLIENT
121153
122154 try :
@@ -127,14 +159,37 @@ def wrap_session_request(wrapped, instance, args, kwargs):
127159 except Exception : # pragma: NO COVER
128160 pass
129161
130- # Add the requests url to attributes
131- _tracer .add_attribute_to_current_span (HTTP_URL , url )
162+ # Add the requests host to attributes
163+ _tracer .add_attribute_to_current_span (
164+ HTTP_HOST , dest_url )
132165
133- result = wrapped (* args , ** kwargs )
166+ # Add the requests method to attributes
167+ _tracer .add_attribute_to_current_span (
168+ HTTP_METHOD , method .upper ())
134169
135- # Add the status code to attributes
170+ # Add the requests path to attributes
136171 _tracer .add_attribute_to_current_span (
137- HTTP_STATUS_CODE , str (result .status_code ))
172+ HTTP_PATH , path )
173+
174+ # Add the requests url to attributes
175+ _tracer .add_attribute_to_current_span (HTTP_URL , url )
138176
139- _tracer .end_span ()
140- return result
177+ try :
178+ result = wrapped (* args , ** kwargs )
179+ except requests .Timeout :
180+ _span .set_status (exceptions_status .TIMEOUT )
181+ except requests .URLRequired :
182+ _span .set_status (exceptions_status .INVALID_URL )
183+ except Exception as e :
184+ _span .set_status (exceptions_status .unknown (e ))
185+ else :
186+ # Add the status code to attributes
187+ _tracer .add_attribute_to_current_span (
188+ HTTP_STATUS_CODE , result .status_code
189+ )
190+ _span .set_status (
191+ utils .status_from_http_code (result .status_code )
192+ )
193+ return result
194+ finally :
195+ _tracer .end_span ()
0 commit comments