1010from ddtrace .api import API
1111from ddtrace .span import Span
1212from ddtrace .tracer import Tracer
13- from ddtrace .encoding import JSONEncoder , MsgpackEncoder
13+ from ddtrace .encoding import JSONEncoder , MsgpackEncoder , get_encoder
1414from tests .test_tracer import get_dummy_tracer
1515
1616
@@ -67,7 +67,7 @@ def test_worker_single_trace(self):
6767 # check arguments
6868 endpoint = self .api ._put .call_args [0 ][0 ]
6969 payload = self ._decode (self .api ._put .call_args [0 ][1 ])
70- eq_ (endpoint , '/v0.2 /traces' )
70+ eq_ (endpoint , '/v0.3 /traces' )
7171 eq_ (len (payload ), 1 )
7272 eq_ (len (payload [0 ]), 1 )
7373 eq_ (payload [0 ][0 ]['name' ], 'client.testing' )
@@ -84,7 +84,7 @@ def test_worker_multiple_traces(self):
8484 # check arguments
8585 endpoint = self .api ._put .call_args [0 ][0 ]
8686 payload = self ._decode (self .api ._put .call_args [0 ][1 ])
87- eq_ (endpoint , '/v0.2 /traces' )
87+ eq_ (endpoint , '/v0.3 /traces' )
8888 eq_ (len (payload ), 2 )
8989 eq_ (len (payload [0 ]), 1 )
9090 eq_ (len (payload [1 ]), 1 )
@@ -104,7 +104,7 @@ def test_worker_single_trace_multiple_spans(self):
104104 # check arguments
105105 endpoint = self .api ._put .call_args [0 ][0 ]
106106 payload = self ._decode (self .api ._put .call_args [0 ][1 ])
107- eq_ (endpoint , '/v0.2 /traces' )
107+ eq_ (endpoint , '/v0.3 /traces' )
108108 eq_ (len (payload ), 1 )
109109 eq_ (len (payload [0 ]), 2 )
110110 eq_ (payload [0 ][0 ]['name' ], 'client.testing' )
@@ -122,7 +122,7 @@ def test_worker_single_service(self):
122122 # check arguments
123123 endpoint = self .api ._put .call_args [0 ][0 ]
124124 payload = self ._decode (self .api ._put .call_args [0 ][1 ])
125- eq_ (endpoint , '/v0.2 /services' )
125+ eq_ (endpoint , '/v0.3 /services' )
126126 eq_ (len (payload .keys ()), 1 )
127127 eq_ (payload ['client.service' ], {'app' : 'django' , 'app_type' : 'web' })
128128
@@ -139,7 +139,7 @@ def test_worker_service_called_multiple_times(self):
139139 # check arguments
140140 endpoint = self .api ._put .call_args [0 ][0 ]
141141 payload = self ._decode (self .api ._put .call_args [0 ][1 ])
142- eq_ (endpoint , '/v0.2 /services' )
142+ eq_ (endpoint , '/v0.3 /services' )
143143 eq_ (len (payload .keys ()), 2 )
144144 eq_ (payload ['backend' ], {'app' : 'django' , 'app_type' : 'web' })
145145 eq_ (payload ['database' ], {'app' : 'postgres' , 'app_type' : 'db' })
@@ -161,8 +161,8 @@ def setUp(self):
161161 """
162162 # create a new API object to test the transport using synchronous calls
163163 self .tracer = get_dummy_tracer ()
164- self .api_json = API ('localhost' , 7777 , wait_response = True , encoder = JSONEncoder ())
165- self .api_msgpack = API ('localhost' , 7777 , wait_response = True , encoder = MsgpackEncoder ())
164+ self .api_json = API ('localhost' , 7777 , encoder = JSONEncoder ())
165+ self .api_msgpack = API ('localhost' , 7777 , encoder = MsgpackEncoder ())
166166
167167 def test_send_single_trace (self ):
168168 # register a single trace with a span and send them to the trace agent
@@ -278,3 +278,44 @@ def test_send_service_called_multiple_times(self):
278278 response = self .api_msgpack .send_services (services )
279279 ok_ (response )
280280 eq_ (response .status , 200 )
281+
282+ @skipUnless (
283+ os .environ .get ('TEST_DATADOG_INTEGRATION' , False ),
284+ 'You should have a running trace agent and set TEST_DATADOG_INTEGRATION=1 env variable'
285+ )
286+ class TestAPIDowngrade (TestCase ):
287+ """
288+ Ensures that if the tracing client found an earlier trace agent,
289+ it will downgrade the current connection to a stable API version
290+ """
291+ def test_get_encoder_default (self ):
292+ # get_encoder should return MsgpackEncoder instance if
293+ # msgpack and the CPP implementaiton are available
294+ encoder = get_encoder ()
295+ ok_ (isinstance (encoder , MsgpackEncoder ))
296+
297+ @mock .patch ('ddtrace.encoding.MSGPACK_ENCODING' , False )
298+ def test_get_encoder_fallback (self ):
299+ # get_encoder should return JSONEncoder instance if
300+ # msgpack or the CPP implementaiton, are not available
301+ encoder = get_encoder ()
302+ ok_ (isinstance (encoder , JSONEncoder ))
303+
304+ def test_downgrade_api (self ):
305+ # make a call to a not existing endpoint, downgrades
306+ # the current API to a stable one
307+ tracer = get_dummy_tracer ()
308+ tracer .trace ('client.testing' ).finish ()
309+ trace = tracer .writer .pop ()
310+
311+ # the encoder is right but we're targeting an API
312+ # endpoint that is not available
313+ api = API ('localhost' , 7777 )
314+ api ._traces = '/v0.0/traces'
315+ ok_ (isinstance (api ._encoder , MsgpackEncoder ))
316+
317+ # after the call, we downgrade to a working endpoint
318+ response = api .send_traces ([trace ])
319+ ok_ (response )
320+ eq_ (response .status , 200 )
321+ ok_ (isinstance (api ._encoder , JSONEncoder ))
0 commit comments