Skip to content

Commit 0494c7c

Browse files
committed
Add support for partial granularity config
1 parent 0685a9c commit 0494c7c

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

newrelic/config.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,55 @@ def _process_setting(section, option, getter, mapper):
319319
_raise_configuration_error(section, option)
320320

321321

322+
def _process_distributed_tracing_sampler_setting(section, option, getter, mapper):
323+
try:
324+
# The type of a value is dictated by the getter
325+
# function supplied.
326+
value = getattr(_config_object, getter)(section, option)
327+
# The getter parsed the value okay but want to
328+
# pass this through a mapping function to change
329+
# it to internal value suitable for internal
330+
# settings object. This is usually one where the
331+
# value was a string.
332+
if mapper:
333+
value = mapper(value)
334+
# Now need to apply the option from the
335+
# configuration file to the internal settings
336+
# object. Walk the object path and assign it.
337+
target = _settings
338+
fields = option.split(".")
339+
sampler_settings_fields = fields[0:2]
340+
341+
for field in sampler_settings_fields:
342+
target = getattr(target, field)
343+
if len(fields) == 2:
344+
setattr(target, "_sampler", value)
345+
if len(fields) >= 3:
346+
sampler_settings_obj = target
347+
sampler_value = fields[2]
348+
sampler_subsettings = fields[2:]
349+
for field in sampler_subsettings[:-1]:
350+
target = getattr(target, field)
351+
setattr(target, sampler_subsettings[-1], value)
352+
breakpoint()
353+
existing_sampler_value = getattr(sampler_settings_obj, "_sampler", None)
354+
if existing_sampler_value is None:
355+
setattr(sampler_settings_obj, "_sampler", sampler_value)
356+
elif existing_sampler_value != sampler_value:
357+
raise RuntimeError
358+
# Cache the configuration so can be dumped out to
359+
# log file when whole main configuration has been
360+
# processed. This ensures that the log file and log
361+
# level entries have been set.
362+
_cache_object.append((option, value))
363+
except configparser.NoSectionError:
364+
pass
365+
except configparser.NoOptionError:
366+
pass
367+
except Exception:
368+
_raise_configuration_error(section, option)
369+
370+
322371
# Processing of all the settings for specified section except
323372
# for log file and log level which are applied separately to
324373
# ensure they are set as soon as possible.
@@ -407,11 +456,12 @@ def _process_configuration(section):
407456
_process_setting(section, "distributed_tracing.unique_spans.enabled", "getboolean", None)
408457
_process_setting(section, "distributed_tracing.minimize_attributes.enabled", "getboolean", None)
409458
_process_setting(section, "distributed_tracing.exclude_newrelic_header", "getboolean", None)
410-
_process_setting(section, "distributed_tracing.sampler.remote_parent_sampled", "get", None)
411-
_process_setting(section, "distributed_tracing.sampler.remote_parent_not_sampled", "get", None)
412-
_process_setting(section, "distributed_tracing.sampler.partial_granularity.enabled", "getboolean", None)
459+
_process_distributed_tracing_sampler_setting(section, "distributed_tracing.sampler.remote_parent_sampled", "get", None)
460+
_process_distributed_tracing_sampler_setting(section, "distributed_tracing.sampler.remote_parent_not_sampled", "get", None)
461+
_process_distributed_tracing_sampler_setting(section, "distributed_tracing.sampler.partial_granularity.enabled", "getboolean", None)
413462
_process_setting(section, "distributed_tracing.sampler.partial_granularity.type", "get", None)
414463
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled", "get", None)
464+
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target", "get", None)
415465
_process_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled", "get", None)
416466
_process_setting(section, "span_events.enabled", "getboolean", None)
417467
_process_setting(section, "span_events.max_samples_stored", "getint", None)

newrelic/core/config.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,42 @@ class DistributedTracingSamplerSettings(Settings):
338338

339339

340340
class DistributedTracingSamplerPartialGranularitySettings(Settings):
341+
@property
342+
def remote_parent_sampled(self):
343+
return self._remote_parent_sampled
344+
345+
@remote_parent_sampled.setter
346+
def remote_parent_sampled(self, value):
347+
if isinstance(value, str):
348+
self._remote_parent_sampled._sampler = value
349+
else:
350+
self._remote_parent_sampled = value
351+
352+
@property
353+
def remote_parent_not_sampled(self):
354+
return self._remote_parent_not_sampled
355+
356+
@remote_parent_not_sampled.setter
357+
def remote_parent_not_sampled(self, value):
358+
if isinstance(value, str):
359+
self._remote_parent_not_sampled._sampler = value
360+
else:
361+
self._remote_parent_not_sampled = value
362+
363+
364+
class DistributedTracingSamplerPartialGranularityRemoteParentSampledSettings(Settings):
365+
_sampler = "default"
366+
367+
368+
class DistributedTracingSamplerPartialGranularityRemoteParentSampledAdaptiveSettings(Settings):
369+
pass
370+
371+
372+
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledSettings(Settings):
373+
_sampler = "default"
374+
375+
376+
class DistributedTracingSamplerPartialGranularityRemoteParentNotSampledAdaptiveSettings(Settings):
341377
pass
342378

343379

@@ -510,11 +546,12 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
510546
_settings.datastore_tracer.instance_reporting = DatastoreTracerInstanceReportingSettings()
511547
_settings.debug = DebugSettings()
512548
_settings.distributed_tracing = DistributedTracingSettings()
513-
_settings.distributed_tracing.drop_inprocess_spans = DistributedTracingDropInprocessSpansSettings()
514-
_settings.distributed_tracing.unique_spans = DistributedTracingUniqueSpansSettings()
515-
_settings.distributed_tracing.minimize_attributes = DistributedTracingMinimizeAttributesSettings()
516549
_settings.distributed_tracing.sampler = DistributedTracingSamplerSettings()
517550
_settings.distributed_tracing.sampler.partial_granularity = DistributedTracingSamplerPartialGranularitySettings()
551+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled = DistributedTracingSamplerPartialGranularityRemoteParentSampledSettings()
552+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive = DistributedTracingSamplerPartialGranularityRemoteParentSampledAdaptiveSettings()
553+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled = DistributedTracingSamplerPartialGranularityRemoteParentNotSampledSettings()
554+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive = DistributedTracingSamplerPartialGranularityRemoteParentNotSampledAdaptiveSettings()
518555
_settings.error_collector = ErrorCollectorSettings()
519556
_settings.error_collector.attributes = ErrorCollectorAttributesSettings()
520557
_settings.event_harvest_config = EventHarvestConfigSettings()
@@ -855,10 +892,10 @@ def default_otlp_host(host):
855892
_settings.distributed_tracing.sampler.partial_granularity.type = os.environ.get(
856893
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_TYPE", "essential"
857894
)
858-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled = os.environ.get(
895+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_sampled._sampler = os.environ.get(
859896
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_SAMPLED", "default"
860897
)
861-
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled = os.environ.get(
898+
_settings.distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled._sampler = os.environ.get(
862899
"NEW_RELIC_DISTRIBUTED_TRACING_SAMPLER_PARTIAL_GRANULARITY_REMOTE_PARENT_NOT_SAMPLED", "default"
863900
)
864901
_settings.distributed_tracing.exclude_newrelic_header = False

0 commit comments

Comments
 (0)