Skip to content

Commit 1d455c9

Browse files
committed
Increment priority when only sampled is sent in headers
1 parent 5623745 commit 1d455c9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

newrelic/api/transaction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,15 +1017,16 @@ def sampling_algo_compute_sampled_and_priority(self, priority, sampled, sampler_
10171017
# or newrelic DT headers and may be overridden in _make_sampling_decision
10181018
# based on the configuration. The only time they are set in here is when the
10191019
# sampling decision must be made by the adaptive sampling algorithm.
1020+
adjust_priority = priority is None or sampled is None
10201021
if priority is None:
10211022
# Truncate priority field to 6 digits past the decimal.
10221023
priority = float(f"{random.random():.6f}") # noqa: S311
10231024
if sampled is None:
10241025
_logger.debug("No trusted account id found. Sampling decision will be made by adaptive sampling algorithm.")
10251026
sampled = self._application.compute_sampled(**sampler_kwargs)
1027+
if adjust_priority and sampled:
10261028
# Increment the priority + 2 for full and + 1 for partial granularity.
1027-
if sampled:
1028-
priority += 1 + int(sampler_kwargs.get("full_granularity"))
1029+
priority += 1 + int(sampler_kwargs.get("full_granularity"))
10291030
return priority, sampled
10301031

10311032
def _compute_sampled_and_priority(

tests/agent_features/test_distributed_tracing.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,21 @@ def _test():
14511451
pass
14521452

14531453
_test()
1454+
1455+
1456+
def test_distributed_trace_priority_set_when_only_sampled_set_in_headers(
1457+
monkeypatch,
1458+
):
1459+
monkeypatch.setattr(random, 'random', lambda *args, **kwargs: 0.123)
1460+
1461+
@override_application_settings(_override_settings)
1462+
@validate_transaction_object_attributes({"sampled": True, "priority": 2.123})
1463+
@background_task()
1464+
def _test():
1465+
headers = {
1466+
"traceparent": f"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01",
1467+
"tracestate": f"1@nr=0-0-1-2827902-0af7651916cd43dd-00f067aa0ba902b7-1--1518469636035"
1468+
}
1469+
accept_distributed_trace_headers(headers)
1470+
1471+
_test()

0 commit comments

Comments
 (0)