1+ from typing import Optional
2+
13import ddtrace
24from ddtrace import config
35
46from .. import trace_utils
57from ...constants import ANALYTICS_SAMPLE_RATE_KEY
68from ...constants import SPAN_MEASURED_KEY
79from ...ext import SpanTypes
8- from ...internal .compat import parse
910from ...internal .logger import get_logger
1011from ...propagation .http import HTTPPropagator
1112
1213
1314log = get_logger (__name__ )
1415
1516
17+ def _extract_hostname (uri ):
18+ # type: (str) -> str
19+ end = len (uri )
20+ j = uri .rfind ("#" , 0 , end )
21+ if j != - 1 :
22+ end = j
23+ j = uri .rfind ("&" , 0 , end )
24+ if j != - 1 :
25+ end = j
26+
27+ start = uri .find ("://" , 0 , end ) + 3
28+ i = uri .find ("@" , start , end ) + 1
29+ if i != 0 :
30+ start = i
31+ j = uri .find ("/" , start , end )
32+ if j != - 1 :
33+ end = j
34+
35+ return uri [start :end ]
36+
37+
38+ def _extract_query_string (uri ):
39+ # type: (str) -> Optional[str]
40+ start = uri .find ("?" ) + 1
41+ if start == 0 :
42+ return None
43+
44+ end = len (uri )
45+ j = uri .rfind ("#" , 0 , end )
46+ if j != - 1 :
47+ end = j
48+
49+ if end <= start :
50+ return None
51+
52+ return uri [start :end ]
53+
54+
1655def _wrap_send (func , instance , args , kwargs ):
1756 """Trace the `Session.send` instance method"""
1857 # TODO[manu]: we already offer a way to provide the Global Tracer
@@ -29,10 +68,8 @@ def _wrap_send(func, instance, args, kwargs):
2968 if not request :
3069 return func (* args , ** kwargs )
3170
32- parsed_uri = parse .urlparse (request .url )
33- hostname = parsed_uri .hostname
34- if parsed_uri .port :
35- hostname = "{}:{}" .format (hostname , parsed_uri .port )
71+ url = request .url
72+ hostname = _extract_hostname (url )
3673
3774 cfg = config .get_from (instance )
3875 service = None
@@ -59,19 +96,9 @@ def _wrap_send(func, instance, args, kwargs):
5996 if cfg .get ("distributed_tracing" ):
6097 HTTPPropagator .inject (span .context , request .headers )
6198
62- response = None
99+ response = response_headers = None
63100 try :
64101 response = func (* args , ** kwargs )
65-
66- # Storing response headers in the span. Note that response.headers is not a dict, but an iterable
67- # requests custom structure, that we convert to a dict
68- if hasattr (response , "headers" ):
69- response_headers = response .headers
70- else :
71- response_headers = None
72- trace_utils .set_http_meta (
73- span , config .requests , request_headers = request .headers , response_headers = response_headers
74- )
75102 return response
76103 finally :
77104 try :
@@ -82,13 +109,16 @@ def _wrap_send(func, instance, args, kwargs):
82109 # Note that response.headers is not a dict, but an iterable
83110 # requests custom structure, that we convert to a dict
84111 response_headers = dict (getattr (response , "headers" , {}))
112+
85113 trace_utils .set_http_meta (
86114 span ,
87115 config .requests ,
116+ request_headers = request .headers ,
117+ response_headers = response_headers ,
88118 method = request .method .upper (),
89119 url = request .url ,
90120 status_code = status ,
91- query = parsed_uri . query ,
121+ query = _extract_query_string ( url ) ,
92122 )
93123 except Exception :
94124 log .debug ("requests: error adding tags" , exc_info = True )
0 commit comments