Skip to content

Commit 1c11ccf

Browse files
committed
Add support for *.adaptive.sampling_target
1 parent 882128e commit 1c11ccf

File tree

5 files changed

+204
-65
lines changed

5 files changed

+204
-65
lines changed

newrelic/api/transaction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,8 @@ def _make_sampling_decision(self):
10841084
priority,
10851085
sampled,
10861086
full_granularity=True,
1087-
remote_parent_sampled_setting=self.settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled,
1088-
remote_parent_not_sampled_setting=self.settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled,
1087+
remote_parent_sampled_setting=self.settings.distributed_tracing.sampler.full_granularity._remote_parent_sampled,
1088+
remote_parent_not_sampled_setting=self.settings.distributed_tracing.sampler.full_granularity._remote_parent_not_sampled,
10891089
)
10901090
_logger.debug("Full granularity sampling decision was %s with priority=%s.", sampled, priority)
10911091
if computed_sampled or not self.settings.distributed_tracing.sampler.partial_granularity.enabled:
@@ -1101,8 +1101,8 @@ def _make_sampling_decision(self):
11011101
priority,
11021102
sampled,
11031103
full_granularity=False,
1104-
remote_parent_sampled_setting=self.settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled,
1105-
remote_parent_not_sampled_setting=self.settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled,
1104+
remote_parent_sampled_setting=self.settings.distributed_tracing.sampler.partial_granularity._remote_parent_sampled,
1105+
remote_parent_not_sampled_setting=self.settings.distributed_tracing.sampler.partial_granularity._remote_parent_not_sampled,
11061106
)
11071107
_logger.debug(
11081108
"Partial granularity sampling decision was %s with priority=%s.", self._sampled, self._priority

newrelic/config.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,27 +406,25 @@ def _process_dt_hidden_setting(section, option, getter):
406406
except Exception:
407407
_raise_configuration_error(section, option_p1)
408408

409-
def _process_dt_sampler_setting(section, option_p1, option_p2, getter):
409+
def _process_dt_sampler_setting(section, option, getter):
410410
try:
411411
# The type of a value is dictated by the getter
412412
# function supplied.
413413

414-
value1 = getattr(_config_object, getter)(section, option_p1)
415-
value2 = getattr(_config_object, getter)(section, option_p2)
414+
value = getattr(_config_object, getter)(section, option)
416415

417416
# Now need to apply the option from the
418417
# configuration file to the internal settings
419418
# object. Walk the object path and assign it.
420419

421420
target = _settings
422-
fields = option_p1.split(".", 1)
421+
fields = option.split(".", 1)
423422

424423
while True:
425424
if len(fields) == 1:
426-
value = value1 or value2 or "default"
427425
setattr(target, f"{fields[0]}", value)
428426
break
429-
elif len(fields) == 3:
427+
elif fields[0] in ("root", "remote_parent_sampled", "remote_parent_not_sampled"):
430428
sampler = fields[1].split(".", 1)[0]
431429
setattr(target, f"_{fields[0]}", sampler)
432430
target = getattr(target, fields[0])
@@ -438,8 +436,7 @@ def _process_dt_sampler_setting(section, option_p1, option_p2, getter):
438436
# processed. This ensures that the log file and log
439437
# level entries have been set.
440438

441-
_cache_object.append((option_p1, value1))
442-
_cache_object.append((option_p2, value2))
439+
_cache_object.append((option, value))
443440

444441
except configparser.NoSectionError:
445442
pass
@@ -448,7 +445,7 @@ def _process_dt_sampler_setting(section, option_p1, option_p2, getter):
448445
pass
449446

450447
except Exception:
451-
_raise_configuration_error(section, option_p1)
448+
_raise_configuration_error(section, option)
452449

453450

454451
# Processing of all the settings for specified section except
@@ -546,8 +543,7 @@ def _process_configuration(section):
546543
_process_dt_sampler_setting(
547544
section,
548545
"distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target",
549-
"distributed_tracing.sampler.remote_parent_sampled.adaptive.sampling_target",
550-
"get",
546+
"getint",
551547
)
552548
_process_dt_setting(
553549
section,
@@ -558,16 +554,23 @@ def _process_configuration(section):
558554
_process_dt_sampler_setting(
559555
section,
560556
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.adaptive.sampling_target",
561-
"distributed_tracing.sampler.remote_parent_not_sampled.adaptive.sampling_target",
562-
"get",
557+
"getint",
563558
)
564559
_process_setting(section, "distributed_tracing.sampler.full_granularity.enabled", "getboolean", None)
565560
_process_setting(section, "distributed_tracing.sampler.partial_granularity.enabled", "getboolean", None)
566561
_process_setting(section, "distributed_tracing.sampler.partial_granularity.type", "get", None)
567562
_process_dt_hidden_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled", "get")
568-
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target", "get", None)
563+
_process_dt_sampler_setting(
564+
section,
565+
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target",
566+
"getint",
567+
)
569568
_process_dt_hidden_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled", "get")
570-
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target", "get", None)
569+
_process_dt_sampler_setting(
570+
section,
571+
"distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target",
572+
"getint",
573+
)
571574
_process_setting(section, "span_events.enabled", "getboolean", None)
572575
_process_setting(section, "span_events.max_samples_stored", "getint", None)
573576
_process_setting(section, "span_events.attributes.enabled", "getboolean", None)

newrelic/core/config.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -379,39 +379,41 @@ class DistributedTracingSamplerFullGranularityRemoteParentNotSampledAdaptiveSett
379379

380380

381381
class DistributedTracingSamplerPartialGranularitySettings(Settings):
382-
@property
383-
def remote_parent_sampled(self):
384-
return self._remote_parent_sampled
382+
_remote_parent_sampled = "default"
383+
_remote_parent_not_sampled = "default"
384+
#@property
385+
#def remote_parent_sampled(self):
386+
# return self._remote_parent_sampled
385387

386-
@remote_parent_sampled.setter
387-
def remote_parent_sampled(self, value):
388-
if isinstance(value, str):
389-
self._remote_parent_sampled._sampler = value
390-
else:
391-
self._remote_parent_sampled = value
388+
#@remote_parent_sampled.setter
389+
#def remote_parent_sampled(self, value):
390+
# if isinstance(value, str):
391+
# self._remote_parent_sampled._sampler = value
392+
# else:
393+
# self._remote_parent_sampled = value
392394

393-
@property
394-
def remote_parent_not_sampled(self):
395-
return self._remote_parent_not_sampled
395+
#@property
396+
#def remote_parent_not_sampled(self):
397+
# return self._remote_parent_not_sampled
396398

397-
@remote_parent_not_sampled.setter
398-
def remote_parent_not_sampled(self, value):
399-
if isinstance(value, str):
400-
self._remote_parent_not_sampled._sampler = value
401-
else:
402-
self._remote_parent_not_sampled = value
399+
#@remote_parent_not_sampled.setter
400+
#def remote_parent_not_sampled(self, value):
401+
# if isinstance(value, str):
402+
# self._remote_parent_not_sampled._sampler = value
403+
# else:
404+
# self._remote_parent_not_sampled = value
403405

404406

405407
class DistributedTracingSamplerPartialGranularityRemoteParentSampledSettings:
406-
_sampler = None
408+
pass #_sampler = None
407409

408410

409411
class DistributedTracingSamplerPartialGranularityRemoteParentSampledAdaptiveSettings:
410412
pass
411413

412414

413415
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledSettings:
414-
_sampler = None
416+
pass #_sampler = None
415417

416418

417419
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledAdaptiveSettings:
@@ -642,6 +644,8 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
642644
def _environ_as_int(name, default=0):
643645
val = os.environ.get(name, default)
644646
try:
647+
if default is None and val is None:
648+
return None
645649
return int(val)
646650
except ValueError:
647651
return default
@@ -939,34 +943,34 @@ def default_otlp_host(host):
939943
_settings.distributed_tracing.sampler.full_granularity.enabled = _environ_as_bool(
940944
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_ENABLED", default=True
941945
)
942-
_settings.distributed_tracing.sampler.full_granularity._remote_parent_sampled = os.environ.get(
946+
_settings.distributed_tracing.sampler.full_granularity._remote_parent_sampled = ("adaptive" if os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None) else None) or os.environ.get(
943947
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_SAMPLED", None
944948
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_SAMPLED", "default")
945-
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target = os.environ.get(
949+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target = _environ_as_int(
946950
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
947-
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", "default")
948-
_settings.distributed_tracing.sampler.full_granularity._remote_parent_not_sampled = os.environ.get(
949-
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
950-
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", "default")
951-
_settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.adaptive.sampling_target = os.environ.get(
951+
)
952+
_settings.distributed_tracing.sampler.full_granularity._remote_parent_not_sampled = ("adaptive" if os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None) else None) or os.environ.get(
952953
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED", None
953954
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_NOT_SAMPLED", "default")
955+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.adaptive.sampling_target = _environ_as_int(
956+
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
957+
)
954958
_settings.distributed_tracing.sampler.partial_granularity.enabled = _environ_as_bool(
955959
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_ENABLED", default=False
956960
)
957961
_settings.distributed_tracing.sampler.partial_granularity.type = os.environ.get(
958962
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_TYPE", "essential"
959963
)
960-
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_sampled = os.environ.get(
964+
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_sampled = ("adaptive" if os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None) else None) or os.environ.get(
961965
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED", "default"
962966
)
963-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target = os.environ.get(
967+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target = _environ_as_int(
964968
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
965969
)
966-
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_not_sampled = os.environ.get(
970+
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_not_sampled = ("adaptive" if os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None) else None) or os.environ.get(
967971
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED", "default"
968972
)
969-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target = os.environ.get(
973+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target = _environ_as_int(
970974
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
971975
)
972976
_settings.distributed_tracing.exclude_newrelic_header = False

tests/agent_features/test_distributed_tracing.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ def test_distributed_trace_remote_parent_sampling_decision_full_granularity(
586586
test_settings = _override_settings.copy()
587587
test_settings.update(
588588
{
589-
"distributed_tracing.sampler.full_granularity.remote_parent_sampled": remote_parent_sampled_setting,
590-
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled": remote_parent_not_sampled_setting,
589+
"distributed_tracing.sampler.full_granularity._remote_parent_sampled": remote_parent_sampled_setting,
590+
"distributed_tracing.sampler.full_granularity._remote_parent_not_sampled": remote_parent_not_sampled_setting,
591591
"span_events.enabled": True,
592592
}
593593
)
@@ -677,8 +677,8 @@ def test_distributed_trace_remote_parent_sampling_decision_partial_granularity(
677677
{
678678
"distributed_tracing.sampler.full_granularity.enabled": False,
679679
"distributed_tracing.sampler.partial_granularity.enabled": True,
680-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": remote_parent_sampled_setting,
681-
"distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled": remote_parent_not_sampled_setting,
680+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": remote_parent_sampled_setting,
681+
"distributed_tracing.sampler.partial_granularity._remote_parent_not_sampled": remote_parent_not_sampled_setting,
682682
"span_events.enabled": True,
683683
}
684684
)
@@ -745,8 +745,8 @@ def test_distributed_trace_remote_parent_sampling_decision_between_full_and_part
745745
{
746746
"distributed_tracing.sampler.full_granularity.enabled": full_granularity_enabled,
747747
"distributed_tracing.sampler.partial_granularity.enabled": partial_granularity_enabled,
748-
"distributed_tracing.sampler.full_granularity.remote_parent_sampled": full_granularity_remote_parent_sampled_setting,
749-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": partial_granularity_remote_parent_sampled_setting,
748+
"distributed_tracing.sampler.full_granularity._remote_parent_sampled": full_granularity_remote_parent_sampled_setting,
749+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": partial_granularity_remote_parent_sampled_setting,
750750
"span_events.enabled": True,
751751
}
752752
)
@@ -815,7 +815,7 @@ def _test():
815815
"distributed_tracing.sampler.full_granularity.enabled": False,
816816
"distributed_tracing.sampler.partial_granularity.enabled": True,
817817
"distributed_tracing.sampler.partial_granularity.type": "compact",
818-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": "always_on",
818+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": "always_on",
819819
"span_events.enabled": True,
820820
}
821821
)(_test)
@@ -868,7 +868,7 @@ def _test():
868868
"distributed_tracing.sampler.full_granularity.enabled": False,
869869
"distributed_tracing.sampler.partial_granularity.enabled": True,
870870
"distributed_tracing.sampler.partial_granularity.type": "compact",
871-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": "always_on",
871+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": "always_on",
872872
"span_events.enabled": True,
873873
}
874874
)(_test)
@@ -918,7 +918,7 @@ def _test():
918918
"distributed_tracing.sampler.full_granularity.enabled": False,
919919
"distributed_tracing.sampler.partial_granularity.enabled": True,
920920
"distributed_tracing.sampler.partial_granularity.type": "compact",
921-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": "always_on",
921+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": "always_on",
922922
"span_events.enabled": True,
923923
}
924924
)(_test)
@@ -972,7 +972,7 @@ def _test():
972972
"distributed_tracing.sampler.full_granularity.enabled": False,
973973
"distributed_tracing.sampler.partial_granularity.enabled": True,
974974
"distributed_tracing.sampler.partial_granularity.type": "reduced",
975-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": "always_on",
975+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": "always_on",
976976
"span_events.enabled": True,
977977
}
978978
)(_test)
@@ -1026,7 +1026,7 @@ def _test():
10261026
"distributed_tracing.sampler.full_granularity.enabled": False,
10271027
"distributed_tracing.sampler.partial_granularity.enabled": True,
10281028
"distributed_tracing.sampler.partial_granularity.type": "essential",
1029-
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled": "always_on",
1029+
"distributed_tracing.sampler.partial_granularity._remote_parent_sampled": "always_on",
10301030
"span_events.enabled": True,
10311031
}
10321032
)(_test)

0 commit comments

Comments
 (0)