@@ -101,7 +101,7 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
101101
102102 # Sample more transactions if there are recent errors
103103 # Note: hasRecentErrors is a custom attribute that needs to be set
104- if custom_sampling_ctx[ " hasRecentErrors" ] is True :
104+ if custom_sampling_ctx.get( " hasRecentErrors" ) is True :
105105 return 0.8
106106
107107 # Sample based on environment
@@ -145,20 +145,19 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
145145 return float (parent_sampling_decision)
146146
147147 custom_sampling_ctx = sampling_context[" custom_sampling_context" ]
148- transaction_ctx = sampling_context[" transaction_context" ]
149148
150149 # Always sample for premium users
151150 # Note: user.tier is a custom attribute that needs to be set
152- if custom_sampling_ctx[ " user" ][ " tier" ] == " premium" :
151+ if custom_sampling_ctx.get( " user" , {}).get( " tier" ) == " premium" :
153152 return 1.0
154153
155154 # Sample more transactions for users experiencing errors
156155 # Note: hasRecentErrors is a custom attribute
157- if custom_sampling_ctx[ " hasRecentErrors" ] :
156+ if custom_sampling_ctx.get( " hasRecentErrors" ) is True :
158157 return 0.8
159158
160159 # Sample less for high-volume, low-value paths
161- name = transaction_ctx [" name" ]
160+ name = sampling_context[ " transaction_context " ] [" name" ]
162161 if name and name.startswith(" /api/metrics" ):
163162 return 0.01
164163
@@ -194,32 +193,32 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
194193 if parent_sampling_decision is not None :
195194 return float (parent_sampling_decision)
196195
197- transaction_ctx = sampling_context[" transaction_context" ]
198- custom_sampling_context = sampling_context[" custom_sampling_context" ]
199-
200196 # Always sample critical business operations
201197 # Note: op is an SDK-provided attribute
198+ transaction_ctx = sampling_context[" transaction_context" ]
202199 if transaction_ctx[" op" ] in [" payment.process" , " order.create" , " user.verify" ]:
203200 return 1.0
204201
202+ custom_sampling_context = sampling_context[" custom_sampling_context" ]
203+
205204 # Sample based on user segment
206205 # Note: user.segment is a custom attribute
207- user_segment = custom_sampling_context[ " user" ][ " segment" ]
206+ user_segment = custom_sampling_context.get( " user" , {}).get( " segment" )
208207 if user_segment == " enterprise" :
209208 return 0.8
210209 elif user_segment == " premium" :
211210 return 0.5
212211
213212 # Sample based on transaction value
214213 # Note: transaction.value is a custom attribute
215- transaction_value = custom_sampling_context[ " transaction" ][ " value" ]
216- if transaction_value > 1000 : # High-value transactions
214+ transaction_value = custom_sampling_context.get( " transaction" , {}).get( " value" )
215+ if transaction_value is not None and transaction_value > 1000 : # High-value transactions
217216 return 0.7
218217
219218 # Sample based on error rate in the service
220219 # Note: service.error_rate is a custom attribute
221- error_rate = custom_sampling_context[ " service" ][ " error_rate" ]
222- if error_rate > 0.05 : # Error rate above 5%
220+ error_rate = custom_sampling_context.get( " service" , {}).get( " error_rate" )
221+ if error_rate is not None and error_rate > 0.05 : # Error rate above 5%
223222 return 0.9
224223
225224 # Default sampling rate
@@ -259,17 +258,20 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
259258
260259 # Sample more transactions with high memory usage
261260 # Note: memory_usage_mb is a custom attribute
262- if custom_sampling_ctx[" memory_usage_mb" ] > 500 :
261+ memory_usage = custom_sampling_ctx.get(" memory_usage_mb" )
262+ if memory_usage is not None and memory_usage > 500 :
263263 return 0.8
264264
265265 # Sample more transactions with high CPU usage
266266 # Note: cpu_percent is a custom attribute
267- if custom_sampling_ctx[" cpu_percent" ] > 80 :
267+ cpu_percent = custom_sampling_ctx.get(" cpu_percent" )
268+ if cpu_percent is not None and cpu_percent > 80 :
268269 return 0.8
269270
270271 # Sample more transactions with high database load
271272 # Note: db_connections is a custom attribute
272- if custom_sampling_ctx[" db_connections" ] > 100 :
273+ db_connections = custom_sampling_ctx.get(" db_connections" )
274+ if db_connections is not None and db_connections > 100 :
273275 return 0.7
274276
275277 # Default sampling rate
0 commit comments