@@ -285,7 +285,7 @@ def __init__(self, application, enabled=None, source=None):
285285 self .tracestate = ""
286286 self ._priority = None
287287 self ._sampled = None
288- self ._traceparent_sampled = None
288+ self ._remote_parent_sampled = None
289289
290290 self ._distributed_trace_state = 0
291291
@@ -1008,6 +1008,10 @@ def user_attributes(self):
10081008 return create_attributes (self ._custom_params , DST_ALL , self .attribute_filter )
10091009
10101010 def sampling_algo_compute_sampled_and_priority (self ):
1011+ # self._priority and self._sampled are set when parsing the W3C tracestate
1012+ # or newrelic DT headers and may be overridden in _compute_sampled_and_priority
1013+ # based on the configuration. The only time they are set in here is when the
1014+ # sampling decision must be made by the adaptive sampling algorithm.
10111015 if self ._priority is None :
10121016 # Truncate priority field to 6 digits past the decimal.
10131017 self ._priority = float (f"{ random .random ():.6f} " ) # noqa: S311
@@ -1017,12 +1021,12 @@ def sampling_algo_compute_sampled_and_priority(self):
10171021 self ._priority += 1
10181022
10191023 def _compute_sampled_and_priority (self ):
1020- if self ._traceparent_sampled is None :
1024+ if self ._remote_parent_sampled is None :
10211025 config = "default" # Use sampling algo.
1022- elif self ._traceparent_sampled :
1026+ elif self ._remote_parent_sampled :
10231027 setting_path = "distributed_tracing.sampler.remote_parent_sampled"
10241028 config = self .settings .distributed_tracing .sampler .remote_parent_sampled
1025- else : # self._traceparent_sampled is False.
1029+ else : # self._remote_parent_sampled is False.
10261030 setting_path = "distributed_tracing.sampler.remote_parent_not_sampled"
10271031 config = self .settings .distributed_tracing .sampler .remote_parent_not_sampled
10281032
@@ -1033,7 +1037,7 @@ def _compute_sampled_and_priority(self):
10331037 self ._sampled = False
10341038 self ._priority = 0
10351039 else :
1036- if config != "default" :
1040+ if config not in ( "default" , "adaptive" ) :
10371041 _logger .warning ("%s=%s is not a recognized value. Using 'default' instead." , setting_path , config )
10381042 self .sampling_algo_compute_sampled_and_priority ()
10391043
@@ -1258,10 +1262,9 @@ def _accept_distributed_trace_data(self, data, transport_type):
12581262
12591263 self ._trace_id = data .get ("tr" )
12601264
1261- priority = data .get ("pr" )
1262- if priority is not None :
1263- self ._priority = priority
1264- self ._sampled = data .get ("sa" )
1265+ self ._priority = data .get ("pr" )
1266+ self ._sampled = data .get ("sa" )
1267+ self ._remote_parent_sampled = data .get ("sa" )
12651268
12661269 if "ti" in data :
12671270 transport_start = data ["ti" ] / 1000.0
@@ -1330,13 +1333,13 @@ def accept_distributed_trace_headers(self, headers, transport_type="HTTP"):
13301333 tracestate_data = None
13311334 if tracestate_data :
13321335 self .trusted_parent_span = tracestate_data .pop ("id" , None )
1336+ # TODO: Make sure this doesn't override the sampled flag.
13331337 data .update (tracestate_data )
13341338 else :
13351339 self ._record_supportability ("Supportability/TraceContext/TraceState/InvalidNrEntry" )
13361340 else :
13371341 self ._record_supportability ("Supportability/TraceContext/TraceState/NoNrEntry" )
13381342
1339- self ._traceparent_sampled = data .get ("sa" )
13401343 self ._accept_distributed_trace_data (data , transport_type )
13411344 self ._record_supportability ("Supportability/TraceContext/Accept/Success" )
13421345 return True
0 commit comments