Skip to content

Commit 882128e

Browse files
committed
Add adaptive.sampling_target config options
1 parent 1c01dc5 commit 882128e

File tree

2 files changed

+206
-8
lines changed

2 files changed

+206
-8
lines changed

newrelic/config.py

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,102 @@ def _process_dt_setting(section, option_p1, option_p2, getter):
337337
while True:
338338
if len(fields) == 1:
339339
value = value1 or value2 or "default"
340-
setattr(target, fields[0], value)
340+
# Store the value at the underscored location so if option_p1 is
341+
# distributed_tracing.sampler.full_granularity.remote_parent_sampled
342+
# store it at location
343+
# distributed_tracing.sampler.full_granularity._remote_parent_sampled
344+
setattr(target, f"_{fields[0]}", value)
345+
break
346+
target = getattr(target, fields[0])
347+
fields = fields[1].split(".", 1)
348+
349+
# Cache the configuration so can be dumped out to
350+
# log file when whole main configuration has been
351+
# processed. This ensures that the log file and log
352+
# level entries have been set.
353+
354+
_cache_object.append((option_p1, value1))
355+
_cache_object.append((option_p2, value2))
356+
357+
except configparser.NoSectionError:
358+
pass
359+
360+
except configparser.NoOptionError:
361+
pass
362+
363+
except Exception:
364+
_raise_configuration_error(section, option_p1)
365+
366+
367+
def _process_dt_hidden_setting(section, option, getter):
368+
try:
369+
# The type of a value is dictated by the getter
370+
# function supplied.
371+
372+
value = getattr(_config_object, getter)(section, option)
373+
374+
# Now need to apply the option from the
375+
# configuration file to the internal settings
376+
# object. Walk the object path and assign it.
377+
378+
target = _settings
379+
fields = option.split(".", 1)
380+
381+
while True:
382+
if len(fields) == 1:
383+
value = value or "default"
384+
# Store the value at the underscored location so if option is
385+
# distributed_tracing.sampler.full_granularity.remote_parent_sampled
386+
# store it at location
387+
# distributed_tracing.sampler.full_granularity._remote_parent_sampled
388+
setattr(target, f"_{fields[0]}", value)
389+
break
390+
target = getattr(target, fields[0])
391+
fields = fields[1].split(".", 1)
392+
393+
# Cache the configuration so can be dumped out to
394+
# log file when whole main configuration has been
395+
# processed. This ensures that the log file and log
396+
# level entries have been set.
397+
398+
_cache_object.append((option, value))
399+
400+
except configparser.NoSectionError:
401+
pass
402+
403+
except configparser.NoOptionError:
404+
pass
405+
406+
except Exception:
407+
_raise_configuration_error(section, option_p1)
408+
409+
def _process_dt_sampler_setting(section, option_p1, option_p2, getter):
410+
try:
411+
# The type of a value is dictated by the getter
412+
# function supplied.
413+
414+
value1 = getattr(_config_object, getter)(section, option_p1)
415+
value2 = getattr(_config_object, getter)(section, option_p2)
416+
417+
# Now need to apply the option from the
418+
# configuration file to the internal settings
419+
# object. Walk the object path and assign it.
420+
421+
target = _settings
422+
fields = option_p1.split(".", 1)
423+
424+
while True:
425+
if len(fields) == 1:
426+
value = value1 or value2 or "default"
427+
setattr(target, f"{fields[0]}", value)
341428
break
429+
elif len(fields) == 3:
430+
sampler = fields[1].split(".", 1)[0]
431+
setattr(target, f"_{fields[0]}", sampler)
342432
target = getattr(target, fields[0])
343433
fields = fields[1].split(".", 1)
344434

435+
345436
# Cache the configuration so can be dumped out to
346437
# log file when whole main configuration has been
347438
# processed. This ensures that the log file and log
@@ -452,17 +543,31 @@ def _process_configuration(section):
452543
"distributed_tracing.sampler.remote_parent_sampled",
453544
"get",
454545
)
546+
_process_dt_sampler_setting(
547+
section,
548+
"distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target",
549+
"distributed_tracing.sampler.remote_parent_sampled.adaptive.sampling_target",
550+
"get",
551+
)
455552
_process_dt_setting(
456553
section,
457554
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled",
458555
"distributed_tracing.sampler.remote_parent_not_sampled",
459556
"get",
460557
)
558+
_process_dt_sampler_setting(
559+
section,
560+
"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",
563+
)
461564
_process_setting(section, "distributed_tracing.sampler.full_granularity.enabled", "getboolean", None)
462565
_process_setting(section, "distributed_tracing.sampler.partial_granularity.enabled", "getboolean", None)
463566
_process_setting(section, "distributed_tracing.sampler.partial_granularity.type", "get", None)
464-
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled", "get", None)
465-
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled", "get", None)
567+
_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)
569+
_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)
466571
_process_setting(section, "span_events.enabled", "getboolean", None)
467572
_process_setting(section, "span_events.max_samples_stored", "getint", None)
468573
_process_setting(section, "span_events.attributes.enabled", "getboolean", None)

newrelic/core/config.py

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,85 @@ class DistributedTracingSettings(Settings):
336336
class DistributedTracingSamplerSettings(Settings):
337337
pass
338338

339-
340339
class DistributedTracingSamplerFullGranularitySettings(Settings):
340+
_remote_parent_sampled = "default"
341+
_remote_parent_not_sampled = "default"
342+
#@property
343+
#def remote_parent_sampled(self):
344+
# return self._remote_parent_sampled
345+
346+
#@remote_parent_sampled.setter
347+
#def remote_parent_sampled(self, value):
348+
# if isinstance(value, str):
349+
# self._remote_parent_sampled._sampler = value
350+
# else:
351+
# self._remote_parent_sampled = value
352+
353+
#@property
354+
#def remote_parent_not_sampled(self):
355+
# return self._remote_parent_not_sampled
356+
357+
#@remote_parent_not_sampled.setter
358+
#def remote_parent_not_sampled(self, value):
359+
# if isinstance(value, str):
360+
# self._remote_parent_not_sampled._sampler = value
361+
# else:
362+
# self._remote_parent_not_sampled = value
363+
364+
365+
class DistributedTracingSamplerFullGranularityRemoteParentSampledSettings:
366+
pass
367+
368+
369+
class DistributedTracingSamplerFullGranularityRemoteParentSampledAdaptiveSettings:
370+
pass
371+
372+
373+
class DistributedTracingSamplerFullGranularityRemoteParentNotSampledSettings:
374+
pass
375+
376+
377+
class DistributedTracingSamplerFullGranularityRemoteParentNotSampledAdaptiveSettings:
341378
pass
342379

343380

344381
class DistributedTracingSamplerPartialGranularitySettings(Settings):
382+
@property
383+
def remote_parent_sampled(self):
384+
return self._remote_parent_sampled
385+
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
392+
393+
@property
394+
def remote_parent_not_sampled(self):
395+
return self._remote_parent_not_sampled
396+
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
403+
404+
405+
class DistributedTracingSamplerPartialGranularityRemoteParentSampledSettings:
406+
_sampler = None
407+
408+
409+
class DistributedTracingSamplerPartialGranularityRemoteParentSampledAdaptiveSettings:
410+
pass
411+
412+
413+
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledSettings:
414+
_sampler = None
415+
416+
417+
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledAdaptiveSettings:
345418
pass
346419

347420

@@ -516,7 +589,15 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
516589
_settings.distributed_tracing = DistributedTracingSettings()
517590
_settings.distributed_tracing.sampler = DistributedTracingSamplerSettings()
518591
_settings.distributed_tracing.sampler.full_granularity = DistributedTracingSamplerFullGranularitySettings()
592+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled = DistributedTracingSamplerFullGranularityRemoteParentSampledSettings()
593+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive = DistributedTracingSamplerFullGranularityRemoteParentSampledAdaptiveSettings()
594+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled = DistributedTracingSamplerFullGranularityRemoteParentNotSampledSettings()
595+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.adaptive = DistributedTracingSamplerFullGranularityRemoteParentNotSampledAdaptiveSettings()
519596
_settings.distributed_tracing.sampler.partial_granularity = DistributedTracingSamplerPartialGranularitySettings()
597+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled = DistributedTracingSamplerPartialGranularityRemoteParentSampledSettings()
598+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive = DistributedTracingSamplerPartialGranularityRemoteParentSampledAdaptiveSettings()
599+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled = DistributedTracingSamplerPartialGranularityRemoteParentNotSampledSettings()
600+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive = DistributedTracingSamplerPartialGranularityRemoteParentNotSampledAdaptiveSettings()
520601
_settings.error_collector = ErrorCollectorSettings()
521602
_settings.error_collector.attributes = ErrorCollectorAttributesSettings()
522603
_settings.event_harvest_config = EventHarvestConfigSettings()
@@ -858,10 +939,16 @@ def default_otlp_host(host):
858939
_settings.distributed_tracing.sampler.full_granularity.enabled = _environ_as_bool(
859940
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_ENABLED", default=True
860941
)
861-
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled = os.environ.get(
942+
_settings.distributed_tracing.sampler.full_granularity._remote_parent_sampled = os.environ.get(
862943
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_SAMPLED", None
863944
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_SAMPLED", "default")
864-
_settings.distributed_tracing.sampler.full_granularity.remote_parent_not_sampled = os.environ.get(
945+
_settings.distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target = os.environ.get(
946+
"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(
865952
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_FULL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED", None
866953
) or os.environ.get("NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_REMOTE_PARENT_NOT_SAMPLED", "default")
867954
_settings.distributed_tracing.sampler.partial_granularity.enabled = _environ_as_bool(
@@ -870,12 +957,18 @@ def default_otlp_host(host):
870957
_settings.distributed_tracing.sampler.partial_granularity.type = os.environ.get(
871958
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_TYPE", "essential"
872959
)
873-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled = os.environ.get(
960+
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_sampled = os.environ.get(
874961
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED", "default"
875962
)
876-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled = os.environ.get(
963+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target = os.environ.get(
964+
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
965+
)
966+
_settings.distributed_tracing.sampler.partial_granularity._remote_parent_not_sampled = os.environ.get(
877967
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED", "default"
878968
)
969+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target = os.environ.get(
970+
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED_ADAPTIVE_SAMPLING_TARGET", None
971+
)
879972
_settings.distributed_tracing.exclude_newrelic_header = False
880973
_settings.span_events.enabled = _environ_as_bool("NEW_RELIC_SPAN_EVENTS_ENABLED", default=True)
881974
_settings.event_harvest_config.harvest_limits.span_event_data = _environ_as_int(

0 commit comments

Comments
 (0)