Skip to content

Commit ec8bd0c

Browse files
committed
more fixes...
1 parent 9dcc3bb commit ec8bd0c

File tree

2 files changed

+53
-58
lines changed

2 files changed

+53
-58
lines changed

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

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

docs/platforms/python/tracing/configure-sampling/index__3.x.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +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 ctx.get("op") == 'checkout'):
65+
if name and ('/checkout' in name or op == 'checkout'):
6566
return 1.0
6667

6768
# Sample 50% of login transactions
68-
if name and ('/login' in name or ctx.get("op") == 'login'):
69+
if name and ('/login' in name or op == 'login'):
6970
return 0.5
7071

7172
# Sample 10% of everything else
@@ -87,7 +88,7 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
8788
# Use the parent sampling decision if we have an incoming trace.
8889
# Note: we strongly recommend respecting the parent sampling decision,
8990
# as this ensures your traces will be complete!
90-
parent_sampling_decision = sampling_context.get("parent_sampled")
91+
parent_sampling_decision = sampling_context["parent_sampled"]
9192
if parent_sampling_decision is not None:
9293
return float(parent_sampling_decision)
9394

@@ -99,7 +100,7 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
99100

100101
# Sample more transactions if there are recent errors
101102
# Note: hasRecentErrors is a custom attribute that needs to be set
102-
if sampling_context.get("hasRecentErrors") is True:
103+
if sampling_context["hasRecentErrors"] is True:
103104
return 0.8
104105

105106
# Sample based on environment
@@ -137,25 +138,23 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
137138
# Use the parent sampling decision if we have an incoming trace.
138139
# Note: we strongly recommend respecting the parent sampling decision,
139140
# as this ensures your traces will be complete!
140-
parent_sampling_decision = sampling_context.get("parent_sampled")
141+
parent_sampling_decision = sampling_context["parent_sampled"]
141142
if parent_sampling_decision is not None:
142143
return float(parent_sampling_decision)
143144

144-
ctx = sampling_context.get("transaction_context", {})
145-
146145
# Always sample for premium users
147146
# Note: user.tier is a custom attribute that needs to be set
148-
if sampling_context.get("user.tier") == "premium":
147+
if sampling_context["user.tier"] == "premium":
149148
return 1.0
150149

151150
# Sample more transactions for users experiencing errors
152151
# Note: hasRecentErrors is a custom attribute
153-
if sampling_context.get("hasRecentErrors") is True:
152+
if sampling_context["hasRecentErrors"] is True:
154153
return 0.8
155154

156155
# Sample less for high-volume, low-value paths
157-
# Note: name is an SDK-provided attribute
158-
if (ctx.get("name") or "").startswith("/api/metrics"):
156+
transaction_ctx = sampling_context["transaction_context"]
157+
if transaction_ctx["name"].startswith("/api/metrics"):
159158
return 0.01
160159

161160
# Default sampling rate
@@ -186,35 +185,33 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
186185
# Use the parent sampling decision if we have an incoming trace.
187186
# Note: we strongly recommend respecting the parent sampling decision,
188187
# as this ensures your traces will be complete!
189-
parent_sampling_decision = sampling_context.get("parent_sampled")
188+
parent_sampling_decision = sampling_context["parent_sampled"]
190189
if parent_sampling_decision is not None:
191190
return float(parent_sampling_decision)
192191

193-
ctx = sampling_context.get("transaction_context", {})
194-
195192
# Always sample critical business operations
196-
# Note: op is an SDK-provided attribute
197-
if ctx.get("op") in ["payment.process", "order.create", "user.verify"]:
193+
transaction_ctx = sampling_context["transaction_context"]
194+
if transaction_ctx["op"] in ["payment.process", "order.create", "user.verify"]:
198195
return 1.0
199196

200197
# Sample based on user segment
201198
# Note: user.segment is a custom attribute
202-
user_segment = sampling_context.get("user.segment")
199+
user_segment = sampling_context["user.segment"]
203200
if user_segment == "enterprise":
204201
return 0.8
205202
elif user_segment == "premium":
206203
return 0.5
207204

208205
# Sample based on transaction value
209206
# Note: transaction.value is a custom attribute
210-
transaction_value = sampling_context.get("transaction.value")
211-
if transaction_value is not None and transaction_value > 1000: # High-value transactions
207+
transaction_value = sampling_context["transaction.value"]
208+
if transaction_value > 1000:
212209
return 0.7
213210

214211
# Sample based on error rate in the service
215212
# Note: service.error_rate is a custom attribute
216-
error_rate = sampling_context.get("service.error_rate")
217-
if error_rate is not None and error_rate > 0.05: # Error rate above 5%
213+
error_rate = sampling_context["service.error_rate"]
214+
if error_rate > 0.05:
218215
return 0.9
219216

220217
# Default sampling rate
@@ -245,23 +242,23 @@ def traces_sampler(sampling_context: SamplingContext) -> float:
245242
# Use the parent sampling decision if we have an incoming trace.
246243
# Note: we strongly recommend respecting the parent sampling decision,
247244
# as this ensures your traces will be complete!
248-
parent_sampling_decision = sampling_context.get("parent_sampled")
245+
parent_sampling_decision = sampling_context["parent_sampled"]
249246
if parent_sampling_decision is not None:
250247
return float(parent_sampling_decision)
251248

252249
# Sample more transactions with high memory usage
253250
# Note: memory_usage_mb is a custom attribute
254-
if sampling_context.get("memory_usage_mb", 0) > 500: # Over 500MB
251+
if sampling_context["memory_usage_mb"] > 500:
255252
return 0.8
256253

257254
# Sample more transactions with high CPU usage
258255
# Note: cpu_percent is a custom attribute
259-
if sampling_context.get("cpu_percent", 0) > 80: # Over 80% CPU
256+
if sampling_context["cpu_percent"] > 80:
260257
return 0.8
261258

262259
# Sample more transactions with high database load
263260
# Note: db_connections is a custom attribute
264-
if sampling_context.get("db_connections", 0) > 100: # Over 100 connections
261+
if sampling_context["db_connections"] > 100:
265262
return 0.7
266263

267264
# Default sampling rate

0 commit comments

Comments
 (0)