1616import copy
1717import json
1818import time
19+ import random
1920
2021import pytest
2122import webtest
2526from testing_support .validators .validate_function_not_called import validate_function_not_called
2627from testing_support .validators .validate_transaction_event_attributes import validate_transaction_event_attributes
2728from testing_support .validators .validate_transaction_metrics import validate_transaction_metrics
29+ from testing_support .validators .validate_transaction_object_attributes import validate_transaction_object_attributes
2830
2931from newrelic .api .application import application_instance
3032from newrelic .api .function_trace import function_trace
@@ -542,17 +544,17 @@ def _test_inbound_dt_payload_acceptance():
542544 "newrelic_header,traceparent_sampled,newrelic_sampled,root_setting,remote_parent_sampled_setting,remote_parent_not_sampled_setting,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called" ,
543545 (
544546 (False , None , None , "default" , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
545- (False , None , None , "always_on" , "default" , "default" , True , 2 , False ), # Always sampled.
547+ (False , None , None , "always_on" , "default" , "default" , True , 3 , False ), # Always sampled.
546548 (False , None , None , "always_off" , "default" , "default" , False , 0 , False ), # Never sampled.
547549 (True , True , None , "default" , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
548- (True , True , None , "default" , "always_on" , "default" , True , 2 , False ), # Always sampled.
550+ (True , True , None , "default" , "always_on" , "default" , True , 3 , False ), # Always sampled.
549551 (True , True , None , "default" , "always_off" , "default" , False , 0 , False ), # Never sampled.
550552 (True , False , None , "default" , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
551553 (True , False , None , "default" , "always_on" , "default" , None , None , True ), # Uses adaptive sampling alog.
552554 (True , False , None , "default" , "always_off" , "default" , None , None , True ), # Uses adaptive sampling algo.
553555 (True , True , None , "default" , "default" , "always_on" , None , None , True ), # Uses adaptive sampling algo.
554556 (True , True , None , "default" , "default" , "always_off" , None , None , True ), # Uses adaptive sampling algo.
555- (True , False , None , "default" , "default" , "always_on" , True , 2 , False ), # Always sampled.
557+ (True , False , None , "default" , "default" , "always_on" , True , 3 , False ), # Always sampled.
556558 (True , False , None , "default" , "default" , "always_off" , False , 0 , False ), # Never sampled.
557559 (
558560 True ,
@@ -587,9 +589,9 @@ def _test_inbound_dt_payload_acceptance():
587589 1.23456 ,
588590 False ,
589591 ), # Uses sampling decision in W3C TraceState header.
590- (True , True , False , "default" , "always_on" , "default" , True , 2 , False ), # Always sampled.
592+ (True , True , False , "default" , "always_on" , "default" , True , 3 , False ), # Always sampled.
591593 (True , True , True , "default" , "always_off" , "default" , False , 0 , False ), # Never sampled.
592- (True , False , False , "default" , "default" , "always_on" , True , 2 , False ), # Always sampled.
594+ (True , False , False , "default" , "default" , "always_on" , True , 3 , False ), # Always sampled.
593595 (True , False , True , "default" , "default" , "always_off" , False , 0 , False ), # Never sampled.
594596 (
595597 True ,
@@ -602,7 +604,7 @@ def _test_inbound_dt_payload_acceptance():
602604 0.1234 ,
603605 False ,
604606 ), # Uses sampling and priority from newrelic header.
605- (True , None , True , "default" , "always_on" , "default" , True , 2 , False ), # Always sampled.
607+ (True , None , True , "default" , "always_on" , "default" , True , 3 , False ), # Always sampled.
606608 (True , None , True , "default" , "always_off" , "default" , False , 0 , False ), # Never sampled.
607609 (
608610 True ,
@@ -637,7 +639,7 @@ def _test_inbound_dt_payload_acceptance():
637639 0.1234 ,
638640 False ,
639641 ), # Uses sampling and priority from newrelic header.
640- (True , None , False , "default" , "default" , "always_on" , True , 2 , False ), # Always sampled.
642+ (True , None , False , "default" , "default" , "always_on" , True , 3 , False ), # Always sampled.
641643 (True , None , False , "default" , "default" , "always_off" , False , 0 , False ), # Never sampled.
642644 (True , None , None , "default" , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
643645 ),
@@ -878,8 +880,7 @@ def _test():
878880 "full_granularity_enabled,full_granularity_remote_parent_sampled_setting,partial_granularity_enabled,partial_granularity_remote_parent_sampled_setting,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called" ,
879881 (
880882 (True , "always_off" , True , "adaptive" , None , None , True ), # Uses adaptive sampling algo.
881- (True , "always_on" , True , "adaptive" , True , 2 , False ), # Uses adaptive sampling algo.
882- (False , "always_on" , False , "adaptive" , False , 0 , False ), # Uses adaptive sampling algo.
883+ (True , "always_on" , True , "adaptive" , True , 3 , False ), # Always samples.
883884 ),
884885)
885886def test_distributed_trace_remote_parent_sampling_decision_between_full_and_partial_granularity (
@@ -1394,3 +1395,59 @@ def _test():
13941395 assert application .sampler ._samplers [expected_sampling_instance_called ].ratio == expected_ratio
13951396
13961397 _test ()
1398+
1399+
1400+ @pytest .mark .parametrize (
1401+ "dt_settings,expected_priority,expected_sampled" ,
1402+ (
1403+ ( # When dt is enabled but full and partial are disabled.
1404+ {
1405+ "distributed_tracing.sampler.full_granularity.enabled" : False ,
1406+ "distributed_tracing.sampler.partial_granularity.enabled" : False ,
1407+ },
1408+ 0.123 , # random
1409+ False ,
1410+ ),
1411+ ( # When dt is disabled.
1412+ {
1413+ "distributed_tracing.enabled" : False ,
1414+ },
1415+ 0.123 , # random
1416+ False ,
1417+ ),
1418+ ( # Verify when full granularity sampled +2 is added to the priority.
1419+ {
1420+ "distributed_tracing.sampler.full_granularity.root.trace_id_ratio_based.ratio" : 1 ,
1421+ },
1422+ 2.123 , # random + 2
1423+ True ,
1424+ ),
1425+ ( # Verify when partial granularity sampled +1 is added to the priority.
1426+ {
1427+ "distributed_tracing.sampler.full_granularity.enabled" : False ,
1428+ "distributed_tracing.sampler.partial_granularity.enabled" : True ,
1429+ "distributed_tracing.sampler.partial_granularity.root.trace_id_ratio_based.ratio" : 1 ,
1430+ },
1431+ 1.123 , # random + 1
1432+ True ,
1433+ ),
1434+ ),
1435+ )
1436+ def test_distributed_trace_enabled_settings_set_correct_sampled_priority (
1437+ dt_settings ,
1438+ expected_priority ,
1439+ expected_sampled ,
1440+ monkeypatch ,
1441+ ):
1442+ monkeypatch .setattr (random , 'random' , lambda * args , ** kwargs : 0.123 )
1443+
1444+ test_settings = _override_settings .copy ()
1445+ test_settings .update (dt_settings )
1446+
1447+ @override_application_settings (test_settings )
1448+ @validate_transaction_object_attributes ({"sampled" : expected_sampled , "priority" : expected_priority })
1449+ @background_task (name = "test_distributed_trace_attributes" )
1450+ def _test ():
1451+ pass
1452+
1453+ _test ()
0 commit comments