Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions newrelic/api/opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __init__(

def _sampled(self):
# Uses NR to determine if the trace is sampled
#
#
# transaction.sampled can be `None`, `True`, `False`.
# If `None`, this has not been computed by NR which
# can also mean the following:
Expand Down Expand Up @@ -272,15 +272,14 @@ def start_span(
*args,
**kwargs,
):

nr_trace_type = FunctionTrace
transaction = current_transaction()
self.nr_application = application_instance()
self.attributes = attributes or {}

if not self.nr_application.settings.otel_bridge.enabled:
return otel_api_trace.INVALID_SPAN

# Retrieve parent span
parent_span_context = otel_api_trace.get_current_span(context).get_span_context()

Expand Down Expand Up @@ -436,4 +435,4 @@ def get_tracer(
*args,
**kwargs,
):
return Tracer(resource=self._resource, instrumentation_library=instrumenting_module_name, *args, **kwargs)
return Tracer(*args, resource=self._resource, instrumentation_library=instrumenting_module_name, **kwargs)
11 changes: 11 additions & 0 deletions newrelic/api/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,17 @@ def _compute_sampled_and_priority(
elif config == "always_off":
sampled = False
priority = 0
elif config == "trace_id_ratio_based":
_logger.debug("Let trace id ratio based sampler algorithm decide based on trace_id = %s.", self._trace_id)
priority, sampled = self.sampling_algo_compute_sampled_and_priority(
priority,
sampled,
{
"full_granularity": full_granularity,
"section": section,
"trace_id": int(self._trace_id.lower().zfill(32), 16),
},
)
else:
if config not in ("default", "adaptive"):
_logger.warning("%s=%s is not a recognized value. Using 'default' instead.", setting_path, config)
Expand Down
42 changes: 40 additions & 2 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ def _map_aws_account_id(s):
# modules to look up customised settings defined in the loaded
# configuration file.

_config_object = configparser.RawConfigParser()

def ratio(value):
try:
val = float(value)
if 0 < val <= 1:
return val
except ValueError:
pass


_config_object = configparser.RawConfigParser(converters={"ratio": ratio})

# Cache of the parsed global settings found in the configuration
# file. We cache these so can dump them out to the log file once
Expand All @@ -108,7 +118,7 @@ def _map_aws_account_id(s):
def _reset_config_parser():
global _config_object
global _cache_object
_config_object = configparser.RawConfigParser()
_config_object = configparser.RawConfigParser(converters={"ratio": ratio})
_cache_object = []


Expand Down Expand Up @@ -378,6 +388,8 @@ def _process_dt_hidden_setting(section, option, getter):
target = _settings
fields = option.split(".", 1)

if value == "trace_id_ratio_based":
raise configparser.NoOptionError("trace_id_ratio_sampler option can only be set by configuring the ratio")
while True:
if len(fields) == 1:
value = value or "default"
Expand Down Expand Up @@ -538,6 +550,9 @@ def _process_configuration(section):
_process_dt_sampler_setting(
section, "distributed_tracing.sampler.full_granularity.root.adaptive.sampling_target", "getint"
)
_process_dt_sampler_setting(
section, "distributed_tracing.sampler.full_granularity.root.trace_id_ratio_based.ratio", "getratio"
)
_process_dt_setting(
section,
"distributed_tracing.sampler.full_granularity.remote_parent_sampled",
Expand All @@ -547,6 +562,11 @@ def _process_configuration(section):
_process_dt_sampler_setting(
section, "distributed_tracing.sampler.full_granularity.remote_parent_sampled.adaptive.sampling_target", "getint"
)
_process_dt_sampler_setting(
section,
"distributed_tracing.sampler.full_granularity.remote_parent_sampled.trace_id_ratio_based.ratio",
"getratio",
)
_process_dt_setting(
section,
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled",
Expand All @@ -558,19 +578,32 @@ def _process_configuration(section):
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.adaptive.sampling_target",
"getint",
)
_process_dt_sampler_setting(
section,
"distributed_tracing.sampler.full_granularity.remote_parent_not_sampled.trace_id_ratio_based.ratio",
"getratio",
)
_process_setting(section, "distributed_tracing.sampler.full_granularity.enabled", "getboolean", None)
_process_setting(section, "distributed_tracing.sampler.partial_granularity.enabled", "getboolean", None)
_process_setting(section, "distributed_tracing.sampler.partial_granularity.type", "get", None)
_process_dt_hidden_setting(section, "distributed_tracing.sampler.partial_granularity.root", "get")
_process_dt_sampler_setting(
section, "distributed_tracing.sampler.partial_granularity.root.adaptive.sampling_target", "getint"
)
_process_dt_sampler_setting(
section, "distributed_tracing.sampler.partial_granularity.root.trace_id_ratio_based.ratio", "getratio"
)
_process_dt_hidden_setting(section, "distributed_tracing.sampler.partial_granularity.remote_parent_sampled", "get")
_process_dt_sampler_setting(
section,
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled.adaptive.sampling_target",
"getint",
)
_process_dt_sampler_setting(
section,
"distributed_tracing.sampler.partial_granularity.remote_parent_sampled.trace_id_ratio_based.ratio",
"getratio",
)
_process_dt_hidden_setting(
section, "distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled", "get"
)
Expand All @@ -579,6 +612,11 @@ def _process_configuration(section):
"distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.adaptive.sampling_target",
"getint",
)
_process_dt_sampler_setting(
section,
"distributed_tracing.sampler.partial_granularity.remote_parent_not_sampled.trace_id_ratio_based.ratio",
"getratio",
)
_process_setting(section, "span_events.enabled", "getboolean", None)
_process_setting(section, "span_events.max_samples_stored", "getint", None)
_process_setting(section, "span_events.attributes.enabled", "getboolean", None)
Expand Down
Loading