你可以通過兩種方式控制發送到 Sentry 的 transactions 的量。
Uniform Sample Rate
如果您希望 transactions 的 cross-section 均勻,無論您在應用程序中的何處或在什麼情況下發生,並且對下文所述的默認繼承和優先級行為感到滿意,設置統一採樣率都是一個不錯的選擇。
為此,請將 Sentry.init()
中的 tracesSampleRate
選項設置為 0 到 1 之間的一個數字。設置此選項後,創建的每個 transaction 將有一定百分比的機會被髮送到 Sentry。(因此,例如,如果將 tracesSampleRate
設置為 0.2
,將記錄和發送大約 20% 的 transactions。)如下所示:
Sentry.init({ // ... tracesSampleRate: 0.2, });
Dynamic Sampling Function
如果您滿足以下條件,則提供採樣功能是一個不錯的選擇:
- 想要以不同的 rates 採樣不同的 transactions
- 想要完全過濾掉一些 transactions
- 要修改下面描述的默認優先級和繼承行為
若要進行動態採樣,請將 Sentry.init()
中的 tracesSampler
選項設置為一個函數,該函數將接受 samplingContext
對象並返回 0 到 1 之間的採樣率。例如:
Sentry.init({ // ... tracesSampler: samplingContext => { // Examine provided context data (including parent decision, if any) along // with anything in the global namespace to compute the sample rate or // sampling decision for this transaction if ("...") { // These are important - take a big sample return 0.5; } else if ("...") { // These are less important or happen much more frequently - only take 1% return 0.01; } else if ("...") { // These aren't something worth tracking - drop all transactions like this return 0; } else { // Default sample rate return 0.1; } }; });
為了方便起見,該函數還可以返回布爾值。返回 true
等於返回 1
,並保證 transaction 將被髮送到 Sentry。返回 false
等於返回 0
,這將確保不會將 transaction 發送給 Sentry。
Default Sampling Context Data
創建 transaction 時,傳遞給 tracesSampler
的 samplingContext
對象中包含的信息因平臺和集成而異。
對於基於瀏覽器的 SDK,它至少包括以下內容:
// contents of `samplingContext` { transactionContext: { name: string; // human-readable identifier, like "GET /users" op: string; // short description of transaction type, like "pageload" } parentSampled: boolean; // if this transaction has a parent, its sampling decision location: Location | WorkerLocation; // the window.location or self.location object ... // custom context as passed to `startTransaction` }
Custom Sampling Context Data
手動創建 transaction 時,可以通過將數據作為可選的第二個參數傳遞給 startTransaction
來將數據添加到 samplingContext
。如果您希望採樣器可以訪問但不想將其作為 tags
或 data
附加到 transaction 中的數據(例如敏感信息或太大而無法隨 transaction 發送的信息),這將非常有用。例如:
Sentry.startTransaction( { // `transactionContext` - will be recorded on transaction name: 'Search from navbar', op: 'search', tags: { testGroup: 'A3', treatmentName: 'eager load', }, }, // `customSamplingContext` - won't be recorded { // PII userId: '12312012', // too big to send resultsFromLastSearch: { ... } }, );
Inheritance
無論 transaction 的抽樣決策是什麼,該決策都將傳遞給其 child spans,並從那裡傳遞給它們隨後在其他服務中引起的任何 transactions。(有關如何完成傳播的更多信息,請參見 Connecting Backend and Frontend Transactions。)
如果當前正在創建的 transaction 是那些後續 transactions 之一(換句話說,如果它具有父 transaction),則上游(父)抽樣決策將始終包含在抽樣上下文數據中,以便您的 tracesSampler
選擇是否和何時繼承該決定。(在大多數情況下,繼承是正確的選擇,這樣就不會出現部分跟蹤。)
在某些 SDK 中,為方便起見,tracesSampler
函數可以返回一個布爾值,這樣,如果這是期望的行為,則可以直接返回父級的決策。
tracesSampler: samplingContext => { // always inherit if (samplingContext.parentSampled !== undefined) { return samplingContext.parentSampled } ... // rest of sampling logic here }
如果您使用的是 tracesSampleRate
而不是 tracesSampler
,則該決策將始終被繼承。
Forcing a Sampling Decision
如果在 transaction 創建時知道是否要將 transaction 發送給 Sentry,則還可以選擇將抽樣決策直接傳遞給 transaction 構造函數(請注意,不在 customSamplingContext
對象中)。如果這樣做,則 transaction 將不受 tracesSampleRate
的約束,也不會運行tracesSampler
,因此您可以指望通過的決策不會被覆蓋。
Sentry.startTransaction({ name: "Search from navbar", sampled: true, });
Precedence
transaction 有多種方法可以得出抽樣決策。
- 根據在
tracesSampleRate
中設置的靜態採樣率進行隨機採樣 - 根據
tracesSampler
返回的動態採樣率進行隨機採樣 tracesSampler
返回的絕對決策(100% 機會或 0% 機會)- 如果 transaction 有父級,則繼承其父級的抽樣決策
- 絕對決策權傳遞給
startTransaction
當有可能出現不止一種情況時,應遵循以下優先規則:
- 如果將抽樣決策傳遞給
startTransaction
(請參見上面的 Forcing a Sampling Decision),則無論其他任何因素,都將使用該決策。 - 如果定義了
tracesSampler
,則將使用其決策。它可以選擇保留或忽略任何父抽樣決策,或者使用抽樣上下文數據做出自己的決策,或者為 transaction 選擇抽樣率。 - 如果未定義
tracesSampler
,但是有一個父採樣決策,則將使用父採樣決策。 - 如果未定義
tracesSampler
且沒有父級採樣決策,則將使用tracesSampleRate
。