@@ -1490,21 +1490,19 @@ def add_sentry_baggage_to_headers(
14901490 )
14911491
14921492
1493- def make_sampling_decision (
1493+ def _make_sampling_decision (
14941494 name : str ,
14951495 attributes : "Optional[Attributes]" ,
14961496 scope : "sentry_sdk.Scope" ,
14971497) -> "tuple[bool, Optional[float], Optional[float], Optional[str]]" :
14981498 """
14991499 Decide whether a span should be sampled.
15001500
1501- Use traces_sampler if defined, otherwise traces_sample_rate.
1502-
15031501 Returns a tuple with:
1504- - the sampling decision
1505- - the effective sample rate
1506- - the sample rand
1507- - the reason for not sampling the span, if unsampled
1502+ 1. the sampling decision
1503+ 2. the effective sample rate
1504+ 3. the sample rand
1505+ 4. the reason for not sampling the span, if unsampled
15081506 """
15091507 client = sentry_sdk .get_client ()
15101508
@@ -1519,53 +1517,50 @@ def make_sampling_decision(
15191517 if sample_rand is None :
15201518 sample_rand = _generate_sample_rand (propagation_context .trace_id )
15211519
1522- sampling_context = {
1523- "name" : name ,
1524- "trace_id" : propagation_context .trace_id ,
1525- "parent_span_id" : propagation_context .parent_span_id ,
1526- "parent_sampled" : propagation_context .parent_sampled ,
1527- "attributes" : attributes or {},
1528- }
1529- if propagation_context .custom_sampling_context :
1530- sampling_context .update (propagation_context .custom_sampling_context )
1531-
15321520 # If there's a traces_sampler, use that; otherwise use traces_sample_rate
15331521 traces_sampler_defined = callable (client .options .get ("traces_sampler" ))
15341522 if traces_sampler_defined :
1523+ sampling_context = {
1524+ "name" : name ,
1525+ "trace_id" : propagation_context .trace_id ,
1526+ "parent_span_id" : propagation_context .parent_span_id ,
1527+ "parent_sampled" : propagation_context .parent_sampled ,
1528+ "attributes" : dict (attributes ) if attributes else {},
1529+ }
1530+
1531+ if propagation_context .custom_sampling_context :
1532+ sampling_context .update (propagation_context .custom_sampling_context )
1533+
15351534 sample_rate = client .options ["traces_sampler" ](sampling_context )
15361535 else :
1537- if sampling_context [ " parent_sampled" ] is not None :
1538- sample_rate = sampling_context [ " parent_sampled" ]
1536+ if propagation_context . parent_sampled is not None :
1537+ sample_rate = propagation_context . parent_sampled
15391538 else :
15401539 sample_rate = client .options ["traces_sample_rate" ]
15411540
15421541 # Validate whether the sample_rate we got is actually valid. Since
15431542 # traces_sampler is user-provided, it could return anything.
15441543 if not is_valid_sample_rate (sample_rate , source = "Tracing" ):
15451544 logger .warning (f"[Tracing] Discarding { name } because of invalid sample rate." )
1546- return False , None , None , None
1545+ return False , None , None , "sample_rate"
15471546
15481547 sample_rate = float (sample_rate )
1549-
1550- # Adjust sample rate if we're under backpressure
1551- if client .monitor :
1552- sample_rate /= 2 ** client .monitor .downsample_factor
1553-
1554- outcome : "Optional[str]" = None
1555-
15561548 if not sample_rate :
15571549 if traces_sampler_defined :
15581550 reason = "traces_sampler returned 0 or False"
15591551 else :
15601552 reason = "traces_sample_rate is set to 0"
15611553
15621554 logger .debug (f"[Tracing] Discarding { name } because { reason } " )
1563- if client .monitor and client .monitor .downsample_factor > 0 :
1564- outcome = "backpressure"
1565- else :
1566- outcome = "sample_rate"
1555+ return False , 0.0 , None , "sample_rate"
1556+
1557+ # Adjust sample rate if we're under backpressure
1558+ if client .monitor :
1559+ sample_rate /= 2 ** client .monitor .downsample_factor
15671560
1568- return False , 0.0 , None , outcome
1561+ if not sample_rate :
1562+ logger .debug (f"[Tracing] Discarding { name } because backpressure" )
1563+ return False , 0.0 , None , "backpressure"
15691564
15701565 sampled = sample_rand < sample_rate
15711566
0 commit comments