Skip to content

Commit 006cd39

Browse files
committed
restore some .get()s where it makes sense
1 parent ea36783 commit 006cd39

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

docs/platforms/python/tracing/configure-sampling/index.mdx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

docs/platforms/python/tracing/configure-sampling/index__v3.x.mdx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
144144

145145
# Always sample for premium users
146146
# Note: user.tier is a custom attribute that needs to be set
147-
if sampling_context["user.tier"] == "premium":
147+
if sampling_context.get("user.tier") == "premium":
148148
return 1.0
149149

150150
# Sample more transactions for users experiencing errors
151151
# Note: hasRecentErrors is a custom attribute
152-
if sampling_context["hasRecentErrors"] is True:
152+
if sampling_context.get("hasRecentErrors") is True:
153153
return 0.8
154154

155155
# Sample less for high-volume, low-value paths
156-
transaction_ctx = sampling_context["transaction_context"]
157-
if transaction_ctx["name"].startswith("/api/metrics"):
156+
transaction_ctx = sampling_context.get("transaction_context")
157+
if transaction_ctx and transaction_ctx["name"].startswith("/api/metrics"):
158158
return 0.01
159159

160160
# Default sampling rate
@@ -196,22 +196,22 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
196196

197197
# Sample based on user segment
198198
# Note: user.segment is a custom attribute
199-
user_segment = sampling_context["user.segment"]
199+
user_segment = sampling_context.get("user.segment")
200200
if user_segment == "enterprise":
201201
return 0.8
202202
elif user_segment == "premium":
203203
return 0.5
204204

205205
# Sample based on transaction value
206206
# Note: transaction.value is a custom attribute
207-
transaction_value = sampling_context["transaction.value"]
208-
if transaction_value > 1000:
207+
transaction_value = sampling_context.get("transaction.value")
208+
if transaction_value is not None and transaction_value > 1000:
209209
return 0.7
210210

211211
# Sample based on error rate in the service
212212
# Note: service.error_rate is a custom attribute
213-
error_rate = sampling_context["service.error_rate"]
214-
if error_rate > 0.05:
213+
error_rate = sampling_context.get("service.error_rate")
214+
if error_rate is not None and error_rate > 0.05:
215215
return 0.9
216216

217217
# Default sampling rate
@@ -248,17 +248,20 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
248248

249249
# Sample more transactions with high memory usage
250250
# Note: memory_usage_mb is a custom attribute
251-
if sampling_context["memory_usage_mb"] > 500:
251+
memory_usage = sampling_context.get("memory_usage_mb")
252+
if memory_usage is not None and memory_usage > 500:
252253
return 0.8
253254

254255
# Sample more transactions with high CPU usage
255256
# Note: cpu_percent is a custom attribute
256-
if sampling_context["cpu_percent"] > 80:
257+
cpu_percent = sampling_context.get("cpu_percent")
258+
if cpu_percent is not None and cpu_percent > 80:
257259
return 0.8
258260

259261
# Sample more transactions with high database load
260262
# Note: db_connections is a custom attribute
261-
if sampling_context["db_connections"] > 100:
263+
db_connections = sampling_context.get("db_connections")
264+
if db_connections is not None and db_connections > 100:
262265
return 0.7
263266

264267
# Default sampling rate

0 commit comments

Comments
 (0)