Skip to content

Commit ec3f7fe

Browse files
committed
Fixup tests
1 parent 670d454 commit ec3f7fe

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

newrelic/api/transaction.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,14 +1065,14 @@ def _make_sampling_decision(self):
10651065
remote_parent_not_sampled_setting = self.settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled,
10661066
)
10671067
_logger.debug("Full granularity sampling decision was %s with priority=%s.", sampled, priority)
1068-
if computed_sampled:
1068+
if computed_sampled or not self.settings.distributed_tracing.sampler.partial_granularity.enabled:
10691069
self._priority = computed_priority
10701070
self._sampled = computed_sampled
10711071
self._sampling_decision_made = True
10721072
return
10731073

10741074
# If full granularity is not going to sample, let partial granularity decide.
1075-
if self.settings.distributed_tracing.sampler.partial_granularity.enabled:
1075+
if not self.settings.distributed_tracing.sampler.partial_granularity.enabled:
10761076
_logger.debug("Partial granularity tracing is enabled. Asking if partial granularity wants to sample.")
10771077
self._priority, self._sampled = self._compute_sampled_and_priority(
10781078
priority,
@@ -1237,7 +1237,6 @@ def _accept_distributed_trace_payload(self, payload, transport_type="HTTP"):
12371237
return False
12381238

12391239
try:
1240-
self._remote_parent_sampled = payload.get("sa")
12411240
version = payload.get("v")
12421241
major_version = version and int(version[0])
12431242

@@ -1258,7 +1257,7 @@ def _accept_distributed_trace_payload(self, payload, transport_type="HTTP"):
12581257
if not any(k in data for k in ("id", "tx")):
12591258
self._record_supportability("Supportability/DistributedTrace/AcceptPayload/ParseException")
12601259
return False
1261-
1260+
self._remote_parent_sampled = data.get("sa")
12621261
settings = self._settings
12631262
account_id = data.get("ac")
12641263
trusted_account_key = settings.trusted_account_key or (
@@ -1349,7 +1348,7 @@ def accept_distributed_trace_headers(self, headers, transport_type="HTTP"):
13491348
try:
13501349
traceparent = ensure_str(traceparent).strip()
13511350
data = W3CTraceParent.decode(traceparent)
1352-
self._remote_parent_sampled = data.get("sa")
1351+
self._remote_parent_sampled = data.pop("sa", None)
13531352
except:
13541353
data = None
13551354

tests/agent_features/test_distributed_tracing.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -419,34 +419,43 @@ def _test_inbound_dt_payload_acceptance():
419419

420420

421421
@pytest.mark.parametrize(
422-
"traceparent_sampled,newrelic_sampled,remote_parent_sampled,remote_parent_not_sampled,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called",
422+
"traceparent_sampled,newrelic_sampled,remote_parent_sampled_setting,remote_parent_not_sampled_setting,expected_sampled,expected_priority,expected_adaptive_sampling_algo_called",
423423
(
424-
(True, None, "default", "default", None, None, True), # Uses sampling algo.
425-
(True, None, "always_on", "default", True, 2, False), # Always sampled.
426-
(True, None, "always_off", "default", False, 0, False), # Never sampled.
427-
(False, None, "default", "default", None, None, True), # Uses sampling algo.
428-
(False, None, "always_on", "default", None, None, True), # Uses sampling alog.
429-
(False, None, "always_off", "default", None, None, True), # Uses sampling algo.
430-
(True, None, "default", "always_on", None, None, True), # Uses sampling algo.
431-
(True, None, "default", "always_off", None, None, True), # Uses sampling algo.
432-
(False, None, "default", "always_on", True, 2, False), # Always sampled.
433-
(False, None, "default", "always_off", False, 0, False), # Never sampled.
434-
(None, True, "default", "default", None, None, True), # Uses sampling algo.
424+
#(True, None, "default", "default", None, None, True), # Uses adaptive sampling algo.
425+
#(True, None, "always_on", "default", True, 2, False), # Always sampled.
426+
#(True, None, "always_off", "default", False, 0, False), # Never sampled.
427+
#(False, None, "default", "default", None, None, True), # Uses adaptive sampling algo.
428+
#(False, None, "always_on", "default", None, None, True), # Uses adaptive sampling alog.
429+
#(False, None, "always_off", "default", None, None, True), # Uses adaptive sampling algo.
430+
#(True, None, "default", "always_on", None, None, True), # Uses adaptive sampling algo.
431+
#(True, None, "default", "always_off", None, None, True), # Uses adaptive sampling algo.
432+
#(False, None, "default", "always_on", True, 2, False), # Always sampled.
433+
#(False, None, "default", "always_off", False, 0, False), # Never sampled.
434+
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+
443+
#(None, True, "default", "default", True, .1234, False), # Uses sampling and priority from newrelic header.
435444
(None, True, "always_on", "default", True, 2, False), # Always sampled.
436445
(None, True, "always_off", "default", False, 0, False), # Never sampled.
437-
(None, False, "default", "default", None, None, True), # Uses sampling algo.
438-
(None, False, "always_on", "default", None, None, True), # Uses sampling alog.
439-
(None, False, "always_off", "default", None, None, True), # Uses sampling algo.
440-
(None, True, "default", "always_on", None, None, True), # Uses sampling algo.
441-
(None, True, "default", "always_off", None, None, True), # Uses sampling algo.
446+
(None, False, "default", "default", False, .1234, False), # Uses sampling and priority from newrelic header.
447+
(None, False, "always_on", "default", False, .1234, False), # Uses sampling and priority from newrelic header.
448+
(None, True, "default", "always_on", True, .1234, False), # Uses sampling and priority from newrelic header.
442449
(None, False, "default", "always_on", True, 2, False), # Always sampled.
443450
(None, False, "default", "always_off", False, 0, False), # Never sampled.
451+
(None, None, "default", "default", None, None, True), # Uses adaptive sampling algo.
444452
),
445453
)
446454
def test_distributed_trace_remote_parent_sampling_decision(
447-
sampled,
448-
remote_parent_sampled,
449-
remote_parent_not_sampled,
455+
traceparent_sampled,
456+
newrelic_sampled,
457+
remote_parent_sampled_setting,
458+
remote_parent_not_sampled_setting,
450459
expected_sampled,
451460
expected_priority,
452461
expected_adaptive_sampling_algo_called,
@@ -460,18 +469,18 @@ def test_distributed_trace_remote_parent_sampling_decision(
460469
test_settings = _override_settings.copy()
461470
test_settings.update(
462471
{
463-
"distributed_tracing.sampler.remote_parent_sampled": remote_parent_sampled,
464-
"distributed_tracing.sampler.remote_parent_not_sampled": remote_parent_not_sampled,
472+
"distributed_tracing.sampler.full_granularity.remote_parent_sampled": remote_parent_sampled_setting,
473+
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled": remote_parent_not_sampled_setting,
465474
"span_events.enabled": True,
466475
}
467476
)
468477
if expected_adaptive_sampling_algo_called:
469478
function_called_decorator = validate_function_called(
470-
"newrelic.api.transaction", "Transaction.sampling_algo_compute_sampled_and_priority"
479+
"newrelic.core.adaptive_sampler", "AdaptiveSampler.compute_sampled"
471480
)
472481
else:
473482
function_called_decorator = validate_function_not_called(
474-
"newrelic.api.transaction", "Transaction.sampling_algo_compute_sampled_and_priority"
483+
"newrelic.core.adaptive_sampler", "AdaptiveSampler.compute_sampled"
475484
)
476485

477486
@function_called_decorator
@@ -484,12 +493,13 @@ def _test():
484493
if traceparent_sampled is not None:
485494
headers = {
486495
"traceparent": f"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-{int(traceparent_sampled):02x}",
487-
"tracestate": "33@nr=0-0-33-2827902-7d3efb1b173fecfa-e8b91a159289ff74-1-1.23456-1518469636035"
488-
"newrelic": "{\"v\":[0,1],\"d\":{\"ty\":\"Mobile\",\"ac\":\"123\",\"ap\":\"51424\",\"id\":\"5f474d64b9cc9b2a\",\"tr\":\"6e2fea0b173fdad0\",\"pr\":0.1234,\"sa\":true,\"ti\":1482959525577,\"tx\":\"27856f70d3d314b7\"}}"
496+
"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.
489497
}
498+
if newrelic_sampled is not None:
499+
headers["tracestate"] = f"1@nr=0-0-1-2827902-0af7651916cd43dd-00f067aa0ba902b7-{int(newrelic_sampled)}-1.23456-1518469636035"
490500
else:
491501
headers = {
492-
"newrelic": "{\"v\":[0,1],\"d\":{\"ty\":\"Mobile\",\"ac\":\"123\",\"ap\":\"51424\",\"id\":\"5f474d64b9cc9b2a\",\"tr\":\"6e2fea0b173fdad0\",\"pr\":0.1234,\"sa\":%s,\"ti\":1482959525577,\"tx\":\"27856f70d3d314b7\"}}"%(str(newrelic_sampled).lower())
502+
"newrelic": "{\"v\":[0,1],\"d\":{\"ty\":\"Mobile\",\"ac\":\"1\",\"ap\":\"51424\",\"id\":\"00f067aa0ba902b7\",\"tr\":\"0af7651916cd43dd8448eb211c80319c\",\"pr\":0.1234,\"sa\":%s,\"ti\":1482959525577,\"tx\":\"0af7651916cd43dd\"}}"%(str(newrelic_sampled).lower())
493503
}
494504
accept_distributed_trace_headers(headers)
495505

0 commit comments

Comments
 (0)