33import time
44
55# project
6- from .encoding import get_encoder
6+ from .encoding import get_encoder , JSONEncoder
77from .compat import httplib
88
99
@@ -14,43 +14,65 @@ class API(object):
1414 """
1515 Send data to the trace agent using the HTTP protocol and JSON format
1616 """
17- def __init__ (self , hostname , port , wait_response = False , headers = None , encoder = None ):
17+ def __init__ (self , hostname , port , headers = None , encoder = None ):
1818 self .hostname = hostname
1919 self .port = port
20+ self ._traces = '/v0.3/traces'
21+ self ._services = '/v0.3/services'
2022 self ._encoder = encoder or get_encoder ()
21- self ._wait_response = wait_response
2223
2324 # overwrite the Content-type with the one chosen in the Encoder
2425 self ._headers = headers or {}
2526 self ._headers .update ({'Content-Type' : self ._encoder .content_type })
2627
28+ def _downgrade (self ):
29+ """
30+ Downgrades the used encoder and API level. This method must
31+ fallback to a safe encoder and API, so that it will success
32+ despite users' configuration
33+ """
34+ self ._traces = '/v0.2/traces'
35+ self ._services = '/v0.2/services'
36+ self ._encoder = JSONEncoder ()
37+ self ._headers .update ({'Content-Type' : self ._encoder .content_type })
38+
2739 def send_traces (self , traces ):
2840 if not traces :
2941 return
3042 start = time .time ()
3143 data = self ._encoder .encode_traces (traces )
32- response = self ._send_span_data (data )
44+ response = self ._put (self ._traces , data )
45+
46+ # the API endpoint is not available so we should
47+ # downgrade the connection and re-try the call
48+ if response .status == 404 :
49+ log .debug ('calling the endpoint "%s" but received 404; downgrading the API' , self ._traces )
50+ self ._downgrade ()
51+ return self .send_traces (traces )
52+
3353 log .debug ("reported %d spans in %.5fs" , len (traces ), time .time () - start )
3454 return response
3555
3656 def send_services (self , services ):
3757 if not services :
3858 return
39- log .debug ("Reporting %d services" , len (services ))
4059 s = {}
4160 for service in services :
4261 s .update (service )
4362 data = self ._encoder .encode_services (s )
44- return self ._put ("/v0.3/services" , data )
63+ response = self ._put (self . _services , data )
4564
46- def _send_span_data (self , data ):
47- return self ._put ("/v0.3/traces" , data )
65+ # the API endpoint is not available so we should
66+ # downgrade the connection and re-try the call
67+ if response .status == 404 :
68+ log .debug ('calling the endpoint "%s" but received 404; downgrading the API' , self ._services )
69+ self ._downgrade ()
70+ return self .send_services (services )
71+
72+ log .debug ("reported %d services" , len (services ))
73+ return response
4874
4975 def _put (self , endpoint , data ):
5076 conn = httplib .HTTPConnection (self .hostname , self .port )
5177 conn .request ("PUT" , endpoint , data , self ._headers )
52-
53- # read the server response only if the
54- # API object is configured to do so
55- if self ._wait_response :
56- return conn .getresponse ()
78+ return conn .getresponse ()
0 commit comments