@@ -53,21 +53,20 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
5353 # Use the parent sampling decision if we have an incoming trace.
5454 # Note: we strongly recommend respecting the parent sampling decision,
5555 # as this ensures your traces will be complete!
56- parent_sampling_decision = sampling_context.get( " parent_sampled" )
56+ parent_sampling_decision = sampling_context[ " parent_sampled" ]
5757 if parent_sampling_decision is not None :
5858 return float (parent_sampling_decision)
5959
60- ctx = sampling_context.get(" transaction_context" , {})
61- name = ctx.get(" name" )
60+ transaction_ctx = sampling_context[" transaction_context" ]
61+ name = transaction_ctx[" name" ]
62+ op = transaction_ctx[" op" ]
6263
6364 # Sample all checkout transactions
64- if name and (' /checkout' in name or
65- ctx.get(" op" ) == ' checkout' ):
65+ if name and (' /checkout' in name or op == ' checkout' ):
6666 return 1.0
6767
6868 # Sample 50% of login transactions
69- if name and (' /login' in name or
70- ctx.get(" op" ) == ' login' ):
69+ if name and (' /login' in name or op == ' login' ):
7170 return 0.5
7271
7372 # Sample 10% of everything else
@@ -89,11 +88,11 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
8988 # Use the parent sampling decision if we have an incoming trace.
9089 # Note: we strongly recommend respecting the parent sampling decision,
9190 # as this ensures your traces will be complete!
92- parent_sampling_decision = sampling_context.get( " parent_sampled" )
91+ parent_sampling_decision = sampling_context[ " parent_sampled" ]
9392 if parent_sampling_decision is not None :
9493 return float (parent_sampling_decision)
9594
96- ctx = sampling_context.get( " transaction_context " , {})
95+ custom_sampling_ctx = sampling_context[ " custom_sampling_context " ]
9796 environment = os.environ.get(" ENVIRONMENT" , " development" )
9897
9998 # Sample all transactions in development
@@ -102,7 +101,7 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
102101
103102 # Sample more transactions if there are recent errors
104103 # Note: hasRecentErrors is a custom attribute that needs to be set
105- if ctx.get( " data " , {}).get( " hasRecentErrors" ) is True :
104+ if custom_sampling_ctx[ " hasRecentErrors" ] is True :
106105 return 0.8
107106
108107 # Sample based on environment
@@ -141,26 +140,26 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
141140 # Use the parent sampling decision if we have an incoming trace.
142141 # Note: we strongly recommend respecting the parent sampling decision,
143142 # as this ensures your traces will be complete!
144- parent_sampling_decision = sampling_context.get( " parent_sampled" )
143+ parent_sampling_decision = sampling_context[ " parent_sampled" ]
145144 if parent_sampling_decision is not None :
146145 return float (parent_sampling_decision)
147146
148- ctx = sampling_context.get( " transaction_context " , {})
149- data = ctx.get( " data " , {})
147+ custom_sampling_ctx = sampling_context[ " custom_sampling_context " ]
148+ transaction_ctx = sampling_context[ " transaction_context " ]
150149
151150 # Always sample for premium users
152151 # Note: user.tier is a custom attribute that needs to be set
153- if data.get( " user" , {}).get( " tier" ) == " premium" :
152+ if custom_sampling_ctx[ " user" ][ " tier" ] == " premium" :
154153 return 1.0
155154
156155 # Sample more transactions for users experiencing errors
157156 # Note: hasRecentErrors is a custom attribute
158- if data.get( " hasRecentErrors" ) :
157+ if custom_sampling_ctx[ " hasRecentErrors" ] :
159158 return 0.8
160159
161160 # Sample less for high-volume, low-value paths
162- # Note: name is an SDK-provided attribute
163- if (ctx.get( " name" ) or " " ) .startswith(" /api/metrics" ):
161+ transaction_name = transaction_ctx[ " name" ]
162+ if name and name .startswith(" /api/metrics" ):
164163 return 0.01
165164
166165 # Default sampling rate
@@ -191,35 +190,35 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
191190 # Use the parent sampling decision if we have an incoming trace.
192191 # Note: we strongly recommend respecting the parent sampling decision,
193192 # as this ensures your traces will be complete!
194- parent_sampling_decision = sampling_context.get( " parent_sampled" )
193+ parent_sampling_decision = sampling_context[ " parent_sampled" ]
195194 if parent_sampling_decision is not None :
196195 return float (parent_sampling_decision)
197196
198- ctx = sampling_context.get( " transaction_context" , {})
199- data = ctx.get( " data " , {})
197+ transaction_ctx = sampling_context[ " transaction_context" ]
198+ custom_sampling_context = sampling_context[ " custom_sampling_context " ]
200199
201200 # Always sample critical business operations
202201 # Note: op is an SDK-provided attribute
203- if ctx.get( " op" ) in [" payment.process" , " order.create" , " user.verify" ]:
202+ if transaction_ctx[ " op" ] in [" payment.process" , " order.create" , " user.verify" ]:
204203 return 1.0
205204
206205 # Sample based on user segment
207206 # Note: user.segment is a custom attribute
208- user_segment = data.get( " user" , {}).get( " segment" )
207+ user_segment = custom_sampling_context[ " user" ][ " segment" ]
209208 if user_segment == " enterprise" :
210209 return 0.8
211210 elif user_segment == " premium" :
212211 return 0.5
213212
214213 # Sample based on transaction value
215214 # Note: transaction.value is a custom attribute
216- transaction_value = data.get( " transaction" , {}).get( " value" , 0 )
215+ transaction_value = custom_sampling_context[ " transaction" ][ " value" ]
217216 if transaction_value > 1000 : # High-value transactions
218217 return 0.7
219218
220219 # Sample based on error rate in the service
221220 # Note: service.error_rate is a custom attribute
222- error_rate = data.get( " service" , {}).get( " error_rate" , 0 )
221+ error_rate = custom_sampling_context[ " service" ][ " error_rate" ]
223222 if error_rate > 0.05 : # Error rate above 5%
224223 return 0.9
225224
@@ -252,26 +251,25 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
252251 # Use the parent sampling decision if we have an incoming trace.
253252 # Note: we strongly recommend respecting the parent sampling decision,
254253 # as this ensures your traces will be complete!
255- parent_sampling_decision = sampling_context.get( " parent_sampled" )
254+ parent_sampling_decision = sampling_context[ " parent_sampled" ]
256255 if parent_sampling_decision is not None :
257256 return float (parent_sampling_decision)
258257
259- ctx = sampling_context.get(" transaction_context" , {})
260- data = ctx.get(" data" , {})
258+ custom_sampling_ctx = sampling_context[" custom_sampling_context" ]
261259
262260 # Sample more transactions with high memory usage
263261 # Note: memory_usage_mb is a custom attribute
264- if data.get( " memory_usage_mb" , 0 ) > 500 : # Over 500MB
262+ if data[ " memory_usage_mb" ] > 500 :
265263 return 0.8
266264
267265 # Sample more transactions with high CPU usage
268266 # Note: cpu_percent is a custom attribute
269- if data.get( " cpu_percent" , 0 ) > 80 : # Over 80% CPU
267+ if data[ " cpu_percent" ] > 80 :
270268 return 0.8
271269
272270 # Sample more transactions with high database load
273271 # Note: db_connections is a custom attribute
274- if data.get( " db_connections" , 0 ) > 100 : # Over 100 connections
272+ if data[ " db_connections" ] > 100 :
275273 return 0.7
276274
277275 # Default sampling rate
0 commit comments