@@ -120,20 +120,28 @@ def should_sample(
120120 if not has_tracing_enabled (client .options ):
121121 return dropped_result (parent_span_context , attributes )
122122
123+ has_parent = parent_span_context .is_valid
124+ is_root_span = not has_parent or parent_span_context .is_remote
125+
123126 # Explicit sampled value provided at start_span
124127 if attributes .get (SentrySpanAttribute .CUSTOM_SAMPLED ) is not None :
125- sample_rate = float (attributes [SentrySpanAttribute .CUSTOM_SAMPLED ])
126- if sample_rate > 0 :
127- return sampled_result (parent_span_context , attributes , sample_rate )
128+ if is_root_span :
129+ sample_rate = float (attributes [SentrySpanAttribute .CUSTOM_SAMPLED ])
130+ if sample_rate > 0 :
131+ return sampled_result (parent_span_context , attributes , sample_rate )
132+ else :
133+ return dropped_result (parent_span_context , attributes )
128134 else :
129- return dropped_result (parent_span_context , attributes )
135+ logger .debug (
136+ f"[Tracing] Ignoring sampled param for non-root span { name } "
137+ )
130138
131139 sample_rate = None
132140
133141 # Check if there is a traces_sampler
134142 # Traces_sampler is responsible to check parent sampled to have full transactions.
135143 has_traces_sampler = callable (client .options .get ("traces_sampler" ))
136- if has_traces_sampler :
144+ if is_root_span and has_traces_sampler :
137145 sampling_context = {
138146 "transaction_context" : {
139147 "name" : name ,
@@ -161,8 +169,7 @@ def should_sample(
161169 return dropped_result (parent_span_context , attributes )
162170
163171 # Down-sample in case of back pressure monitor says so
164- # TODO: this should only be done for transactions (aka root spans)
165- if client .monitor :
172+ if is_root_span and client .monitor :
166173 sample_rate /= 2 ** client .monitor .downsample_factor
167174
168175 # Roll the dice on sample rate
0 commit comments