3939from newrelic .api .wsgi_application import wsgi_application
4040from newrelic .core .attribute import Attribute
4141
42+ # ruff: noqa: UP031
4243distributed_trace_intrinsics = ["guid" , "traceId" , "priority" , "sampled" ]
4344inbound_payload_intrinsics = [
4445 "parent.type" ,
@@ -419,24 +420,41 @@ def _test_inbound_dt_payload_acceptance():
419420
420421
421422@pytest .mark .parametrize (
422- "sampled,remote_parent_sampled,remote_parent_not_sampled ,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called" ,
423+ "traceparent_sampled,newrelic_sampled,remote_parent_sampled_setting,remote_parent_not_sampled_setting ,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called" ,
423424 (
424- (True , "default" , "default" , None , None , True ), # Uses sampling algo.
425- (True , "always_on" , "default" , True , 2 , False ), # Always sampled.
426- (True , "always_off" , "default" , False , 0 , False ), # Never sampled.
427- (False , "default" , "default" , None , None , True ), # Uses sampling algo.
428- (False , "always_on" , "default" , None , None , True ), # Uses sampling alog.
429- (False , "always_off" , "default" , None , None , True ), # Uses sampling algo.
430- (True , "default" , "always_on" , None , None , True ), # Uses sampling algo.
431- (True , "default" , "always_off" , None , None , True ), # Uses sampling algo.
432- (False , "default" , "always_on" , True , 2 , False ), # Always sampled.
433- (False , "default" , "always_off" , False , 0 , False ), # Never sampled.
425+ (True , None , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
426+ (True , None , "always_on" , "default" , True , 2 , False ), # Always sampled.
427+ (True , None , "always_off" , "default" , False , 0 , False ), # Never sampled.
428+ (False , None , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
429+ (False , None , "always_on" , "default" , None , None , True ), # Uses adaptive sampling alog.
430+ (False , None , "always_off" , "default" , None , None , True ), # Uses adaptive sampling algo.
431+ (True , None , "default" , "always_on" , None , None , True ), # Uses adaptive sampling algo.
432+ (True , None , "default" , "always_off" , None , None , True ), # Uses adaptive sampling algo.
433+ (False , None , "default" , "always_on" , True , 2 , False ), # Always sampled.
434+ (False , None , "default" , "always_off" , False , 0 , False ), # Never sampled.
435+ (True , True , "default" , "default" , True , 1.23456 , False ), # Uses sampling decision in W3C TraceState header.
436+ (True , False , "default" , "default" , False , 1.23456 , False ), # Uses sampling decision in W3C TraceState header.
437+ (False , False , "default" , "default" , False , 1.23456 , False ), # Uses sampling decision in W3C TraceState header.
438+ (True , False , "always_on" , "default" , True , 2 , False ), # Always sampled.
439+ (True , True , "always_off" , "default" , False , 0 , False ), # Never sampled.
440+ (False , False , "default" , "always_on" , True , 2 , False ), # Always sampled.
441+ (False , True , "default" , "always_off" , False , 0 , False ), # Never sampled.
442+ (None , True , "default" , "default" , True , 0.1234 , False ), # Uses sampling and priority from newrelic header.
443+ (None , True , "always_on" , "default" , True , 2 , False ), # Always sampled.
444+ (None , True , "always_off" , "default" , False , 0 , False ), # Never sampled.
445+ (None , False , "default" , "default" , False , 0.1234 , False ), # Uses sampling and priority from newrelic header.
446+ (None , False , "always_on" , "default" , False , 0.1234 , False ), # Uses sampling and priority from newrelic header.
447+ (None , True , "default" , "always_on" , True , 0.1234 , False ), # Uses sampling and priority from newrelic header.
448+ (None , False , "default" , "always_on" , True , 2 , False ), # Always sampled.
449+ (None , False , "default" , "always_off" , False , 0 , False ), # Never sampled.
450+ (None , None , "default" , "default" , None , None , True ), # Uses adaptive sampling algo.
434451 ),
435452)
436- def test_distributed_trace_w3cparent_sampling_decision (
437- sampled ,
438- remote_parent_sampled ,
439- remote_parent_not_sampled ,
453+ def test_distributed_trace_remote_parent_sampling_decision_full_granularity (
454+ traceparent_sampled ,
455+ newrelic_sampled ,
456+ remote_parent_sampled_setting ,
457+ remote_parent_not_sampled_setting ,
440458 expected_sampled ,
441459 expected_priority ,
442460 expected_adaptive_sampling_algo_called ,
@@ -450,18 +468,18 @@ def test_distributed_trace_w3cparent_sampling_decision(
450468 test_settings = _override_settings .copy ()
451469 test_settings .update (
452470 {
453- "distributed_tracing.sampler.remote_parent_sampled" : remote_parent_sampled ,
454- "distributed_tracing.sampler.remote_parent_not_sampled" : remote_parent_not_sampled ,
471+ "distributed_tracing.sampler.remote_parent_sampled" : remote_parent_sampled_setting ,
472+ "distributed_tracing.sampler.remote_parent_not_sampled" : remote_parent_not_sampled_setting ,
455473 "span_events.enabled" : True ,
456474 }
457475 )
458476 if expected_adaptive_sampling_algo_called :
459477 function_called_decorator = validate_function_called (
460- "newrelic.api.transaction " , "Transaction.sampling_algo_compute_sampled_and_priority "
478+ "newrelic.core.adaptive_sampler " , "AdaptiveSampler.compute_sampled "
461479 )
462480 else :
463481 function_called_decorator = validate_function_not_called (
464- "newrelic.api.transaction " , "Transaction.sampling_algo_compute_sampled_and_priority "
482+ "newrelic.core.adaptive_sampler " , "AdaptiveSampler.compute_sampled "
465483 )
466484
467485 @function_called_decorator
@@ -471,10 +489,20 @@ def test_distributed_trace_w3cparent_sampling_decision(
471489 def _test ():
472490 txn = current_transaction ()
473491
474- headers = {
475- "traceparent" : f"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-{ int (sampled ):02x} " ,
476- "tracestate" : "rojo=f06a0ba902b7,congo=t61rcWkgMzE" ,
477- }
492+ if traceparent_sampled is not None :
493+ headers = {
494+ "traceparent" : f"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-{ int (traceparent_sampled ):02x} " ,
495+ "newrelic" : '{"v":[0,1],"d":{"ty":"Mobile","ac":"123","ap":"51424","id":"5f474d64b9cc9b2a","tr":"6e2fea0b173fdad0","pr":0.1234,"sa":true,"ti":1482959525577,"tx":"27856f70d3d314b7"}}' , # This header should be ignored.
496+ }
497+ if newrelic_sampled is not None :
498+ headers ["tracestate" ] = (
499+ f"1@nr=0-0-1-2827902-0af7651916cd43dd-00f067aa0ba902b7-{ int (newrelic_sampled )} -1.23456-1518469636035"
500+ )
501+ else :
502+ headers = {
503+ "newrelic" : '{"v":[0,1],"d":{"ty":"Mobile","ac":"1","ap":"51424","id":"00f067aa0ba902b7","tr":"0af7651916cd43dd8448eb211c80319c","pr":0.1234,"sa":%s,"ti":1482959525577,"tx":"0af7651916cd43dd"}}'
504+ % (str (newrelic_sampled ).lower ())
505+ }
478506 accept_distributed_trace_headers (headers )
479507
480508 _test ()
0 commit comments