You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(tracing): Add hook for trace sampling function to SDK options (#2820)
Currently, tracing is either on, with a static sample rate for all transactions, or off. This doesn't let users either
a) sample different transactions at different rates or
b) filter out certain transactions all together.
This PR introduces a new SDK option, `tracesSampler`, whose value is a callback with the signature
`tracesSampler(samplingContext: SamplingContext): number | boolean`,
where `samplingContext` is a combination of automatically-provided data (varying by SDK/integration) and data passed by the user to `startTransaction()` as an optional third parameter. Given that data, the `tracesSampler` function can then compute the appropriate sample rate and return it to the sampling function.
Example use cases for variable sample rate:
- Collect traces for all enterprise customers but only a fraction of regular customers
- Collect a higher percentage of traces on rarely-visited pages, to ensure a decent sample size
- Collect more traces on a page you recently revamped compared to one whose code you already have well-profiled
Example use cases for filtering:
- Don't trace pre-flight CORS requests
- Don't trace health check requests
- Don't collect traces from users with a certain browser extension enabled, because it's known to break things
- Don't trace navigation to deprecated parts of your web app
(A user can, of course, combine these ideas as well, returning a variable sample rate for some traces and 0 to filter out others.)
In order to support this addition, a few other changes needed to be made:
- Move method for extracting node request data to `@sentry/utils`
- Break up `misc.ts` to eliminate circular dependencies within `@sentry/utils`
- Move method for extracting `sentry-trace` data into `@sentry/tracing` utils
- Tweak some types related to domains and request data, and polyfill `WorkerLocation` type
Copy file name to clipboardExpand all lines: packages/gatsby/README.md
+27-4Lines changed: 27 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ To automatically capture the `release` value on Vercel you will need to register
40
40
41
41
## Sentry Performance
42
42
43
-
To enable Tracing support, supply the`tracesSampleRate` to the options and make sure you have installed the `@sentry/tracing` package. This will also turn on the `BrowserTracing` integration for automatic instrumentation of the browser.
43
+
To enable tracing, supply either`tracesSampleRate`or `tracesSampler`to the options and make sure you have installed the `@sentry/tracing` package. This will also turn on the `BrowserTracing` integration for automatic instrumentation of pageloads and navigations.
44
44
45
45
```javascript
46
46
{
@@ -49,8 +49,31 @@ To enable Tracing support, supply the `tracesSampleRate` to the options and make
49
49
{
50
50
resolve:"@sentry/gatsby",
51
51
options: {
52
-
dsn:process.env.SENTRY_DSN, // this is the default
53
-
tracesSampleRate:1, // this is just to test, you should lower this in production
52
+
dsn:process.env.SENTRY_DSN, // this is the default
53
+
54
+
// A rate of 1 means all traces will be sent, so it's good for testing.
55
+
// In production, you'll likely want to either choose a lower rate or use `tracesSampler` instead (see below).
56
+
tracesSampleRate:1,
57
+
58
+
// Alternatively:
59
+
tracesSampler:samplingContext=> {
60
+
// Examine provided context data (along with anything in the global namespace) to decide the sample rate
61
+
// for this transaction.
62
+
// Can return 0 to drop the transaction entirely.
63
+
64
+
if ("...") {
65
+
return0.5// These are important - take a big sample
66
+
}
67
+
elseif ("...") {
68
+
return0.01// These are less important or happen much more frequently - only take 1% of them
69
+
}
70
+
elseif ("...") {
71
+
return0// These aren't something worth tracking - drop all transactions like this
72
+
}
73
+
else {
74
+
return0.1// Default sample rate
75
+
}
76
+
}
54
77
}
55
78
},
56
79
// ...
@@ -68,7 +91,7 @@ If you want to supply options to the `BrowserTracing` integration, use the `brow
68
91
resolve:"@sentry/gatsby",
69
92
options: {
70
93
dsn:process.env.SENTRY_DSN, // this is the default
71
-
tracesSampleRate:1, //this is just to test, you should lower this in production
94
+
tracesSampleRate:1, //or tracesSampler (see above)
0 commit comments