|
17 | 17 |
|
18 | 18 | import pytest |
19 | 19 | import webtest |
20 | | -from testing_support.fixtures import override_application_settings, validate_attributes |
| 20 | +from testing_support.fixtures import override_application_settings, validate_attributes, validate_attributes_complete |
21 | 21 | from testing_support.validators.validate_error_event_attributes import validate_error_event_attributes |
| 22 | +from testing_support.validators.validate_function_called import validate_function_called |
| 23 | +from testing_support.validators.validate_function_not_called import validate_function_not_called |
22 | 24 | from testing_support.validators.validate_transaction_event_attributes import validate_transaction_event_attributes |
23 | 25 | from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics |
24 | 26 |
|
|
36 | 38 | ) |
37 | 39 | from newrelic.api.web_transaction import WSGIWebTransaction |
38 | 40 | from newrelic.api.wsgi_application import wsgi_application |
| 41 | +from newrelic.core.attribute import Attribute |
39 | 42 |
|
40 | 43 | distributed_trace_intrinsics = ["guid", "traceId", "priority", "sampled"] |
41 | 44 | inbound_payload_intrinsics = [ |
@@ -410,3 +413,65 @@ def _test_inbound_dt_payload_acceptance(): |
410 | 413 | assert not result |
411 | 414 |
|
412 | 415 | _test_inbound_dt_payload_acceptance() |
| 416 | + |
| 417 | + |
| 418 | +@pytest.mark.parametrize( |
| 419 | + "sampled,remote_parent_sampled,remote_parent_not_sampled,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called", |
| 420 | + ( |
| 421 | + (True, "default", "default", None, None, True), # Uses sampling algo. |
| 422 | + (True, "always_on", "default", True, 2, False), # Always sampled. |
| 423 | + (True, "always_off", "default", False, 0, False), # Never sampled. |
| 424 | + (False, "default", "default", None, None, True), # Uses sampling algo. |
| 425 | + (False, "always_on", "default", None, None, True), # Uses sampling alog. |
| 426 | + (False, "always_off", "default", None, None, True), # Uses sampling algo. |
| 427 | + (True, "default", "always_on", None, None, True), # Uses sampling algo. |
| 428 | + (True, "default", "always_off", None, None, True), # Uses sampling algo. |
| 429 | + (False, "default", "always_on", True, 2, False), # Always sampled. |
| 430 | + (False, "default", "always_off", False, 0, False), # Never sampled. |
| 431 | + ), |
| 432 | +) |
| 433 | +def test_distributed_trace_w3cparent_sampling_decision( |
| 434 | + sampled, |
| 435 | + remote_parent_sampled, |
| 436 | + remote_parent_not_sampled, |
| 437 | + expected_sampled, |
| 438 | + expected_priority, |
| 439 | + expected_adaptive_sampling_algo_called, |
| 440 | +): |
| 441 | + required_intrinsics = [] |
| 442 | + if expected_sampled is not None: |
| 443 | + required_intrinsics.append(Attribute(name="sampled", value=expected_sampled, destinations=0b110)) |
| 444 | + if expected_priority is not None: |
| 445 | + required_intrinsics.append(Attribute(name="priority", value=expected_priority, destinations=0b110)) |
| 446 | + |
| 447 | + test_settings = _override_settings.copy() |
| 448 | + test_settings.update( |
| 449 | + { |
| 450 | + "distributed_tracing.sampler.remote_parent_sampled": remote_parent_sampled, |
| 451 | + "distributed_tracing.sampler.remote_parent_not_sampled": remote_parent_not_sampled, |
| 452 | + "span_events.enabled": True, |
| 453 | + } |
| 454 | + ) |
| 455 | + if expected_adaptive_sampling_algo_called: |
| 456 | + function_called_decorator = validate_function_called( |
| 457 | + "newrelic.api.transaction", "Transaction.sampling_algo_compute_sampled_and_priority" |
| 458 | + ) |
| 459 | + else: |
| 460 | + function_called_decorator = validate_function_not_called( |
| 461 | + "newrelic.api.transaction", "Transaction.sampling_algo_compute_sampled_and_priority" |
| 462 | + ) |
| 463 | + |
| 464 | + @function_called_decorator |
| 465 | + @override_application_settings(test_settings) |
| 466 | + @validate_attributes_complete("intrinsic", required_intrinsics) |
| 467 | + @background_task(name="test_distributed_trace_attributes") |
| 468 | + def _test(): |
| 469 | + txn = current_transaction() |
| 470 | + |
| 471 | + headers = { |
| 472 | + "traceparent": f"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-{int(sampled):02x}", |
| 473 | + "tracestate": "rojo=f06a0ba902b7,congo=t61rcWkgMzE", |
| 474 | + } |
| 475 | + accept_distributed_trace_headers(headers) |
| 476 | + |
| 477 | + _test() |
0 commit comments