|
3 | 3 | from .web.app import CustomDefaultHandler |
4 | 4 | from .utils import TornadoTestCase |
5 | 5 |
|
| 6 | +from ddtrace.constants import SAMPLING_PRIORITY_KEY |
| 7 | + |
6 | 8 |
|
7 | 9 | class TestTornadoWeb(TornadoTestCase): |
8 | 10 | """ |
9 | 11 | Ensure that Tornado web handlers are properly traced. |
10 | 12 | """ |
| 13 | + def get_settings(self): |
| 14 | + return { |
| 15 | + 'datadog_trace': { |
| 16 | + 'distributed_tracing': True, |
| 17 | + } |
| 18 | + } |
| 19 | + |
11 | 20 | def test_success_handler(self): |
12 | 21 | # it should trace a handler that returns 200 |
13 | 22 | response = self.fetch('/success/') |
@@ -227,6 +236,69 @@ def test_static_handler(self): |
227 | 236 | eq_('/statics/empty.txt', request_span.get_tag('http.url')) |
228 | 237 | eq_(0, request_span.error) |
229 | 238 |
|
| 239 | + def test_propagation(self): |
| 240 | + # it should trace a handler that returns 200 with a propagated context |
| 241 | + headers = { |
| 242 | + 'x-datadog-trace-id': '1234', |
| 243 | + 'x-datadog-parent-id': '4567', |
| 244 | + 'x-datadog-sampling-priority': '2' |
| 245 | + } |
| 246 | + response = self.fetch('/success/', headers=headers) |
| 247 | + eq_(200, response.code) |
| 248 | + |
| 249 | + traces = self.tracer.writer.pop_traces() |
| 250 | + eq_(1, len(traces)) |
| 251 | + eq_(1, len(traces[0])) |
| 252 | + |
| 253 | + request_span = traces[0][0] |
| 254 | + |
| 255 | + # simple sanity check on the span |
| 256 | + eq_('tornado.request', request_span.name) |
| 257 | + eq_('200', request_span.get_tag('http.status_code')) |
| 258 | + eq_('/success/', request_span.get_tag('http.url')) |
| 259 | + eq_(0, request_span.error) |
| 260 | + |
| 261 | + # check propagation |
| 262 | + eq_(1234, request_span.trace_id) |
| 263 | + eq_(4567, request_span.parent_id) |
| 264 | + eq_(2, request_span.get_metric(SAMPLING_PRIORITY_KEY)) |
| 265 | + |
| 266 | + |
| 267 | +class TestNoPropagationTornadoWeb(TornadoTestCase): |
| 268 | + """ |
| 269 | + Ensure that Tornado web handlers are properly traced and are ignoring propagated HTTP headers when disabled. |
| 270 | + """ |
| 271 | + def get_settings(self): |
| 272 | + # distributed_tracing should be disabled by default |
| 273 | + return {} |
| 274 | + |
| 275 | + def test_no_propagation(self): |
| 276 | + # it should not propagate the HTTP context |
| 277 | + headers = { |
| 278 | + 'x-datadog-trace-id': '1234', |
| 279 | + 'x-datadog-parent-id': '4567', |
| 280 | + 'x-datadog-sampling-priority': '2' |
| 281 | + } |
| 282 | + response = self.fetch('/success/', headers=headers) |
| 283 | + eq_(200, response.code) |
| 284 | + |
| 285 | + traces = self.tracer.writer.pop_traces() |
| 286 | + eq_(1, len(traces)) |
| 287 | + eq_(1, len(traces[0])) |
| 288 | + |
| 289 | + request_span = traces[0][0] |
| 290 | + |
| 291 | + # simple sanity check on the span |
| 292 | + eq_('tornado.request', request_span.name) |
| 293 | + eq_('200', request_span.get_tag('http.status_code')) |
| 294 | + eq_('/success/', request_span.get_tag('http.url')) |
| 295 | + eq_(0, request_span.error) |
| 296 | + |
| 297 | + # check non-propagation |
| 298 | + assert request_span.trace_id != 1234 |
| 299 | + assert request_span.parent_id != 4567 |
| 300 | + assert request_span.get_metric(SAMPLING_PRIORITY_KEY) != 2 |
| 301 | + |
230 | 302 |
|
231 | 303 | class TestCustomTornadoWeb(TornadoTestCase): |
232 | 304 | """ |
|
0 commit comments