Sentry(v20.12.1) K8S 雲原生架構探索,JavaScript 效能監控之取樣 Transactions
阿新 • • 發佈:2021-01-22
![](https://img2020.cnblogs.com/blog/436453/202101/436453-20210122133238574-1311220754.png)
## 系列
1. [Sentry-Go SDK 中文實踐指南](https://mp.weixin.qq.com/s/MLqRSh7D0YZzCKfG5xvsZw)
2. [一起來刷 Sentry For Go 官方文件之 Enriching Events](https://mp.weixin.qq.com/s/0Ke1gozUyUVny4-7B2mw0w)
3. [Snuba:Sentry 新的搜尋基礎設施(基於 ClickHouse 之上)](https://mp.weixin.qq.com/s/RKaBfEh8Dlgt_iuHt10OPg)
4. [Sentry 10 K8S 雲原生架構探索,Vue App 1 分鐘快速接入](https://mp.weixin.qq.com/s/Rwwz4MJytA7va8zE8A5Y0w)
5. [Sentry(v20.12.1) K8S雲原生架構探索,玩轉前/後端監控與事件日誌大資料分析,高效能高可用+可擴充套件可伸縮叢集部署](https://mp.weixin.qq.com/s/5xYtkzNx31dLsCAhoMPGrQ)
6. [Sentry(v20.12.1) K8S 雲原生架構探索,Sentry JavaScript SDK 三種安裝載入方式](https://mp.weixin.qq.com/s/EV9M3Ru5rK5atAjTl1D9PA)
7. [Sentry(v20.12.1) K8S 雲原生架構探索,SENTRY FOR JAVASCRIPT SDK 配置詳解](https://mp.weixin.qq.com/s/dlaejrn1kWBPiUJSmrzwow)
8. [Sentry(v20.12.1) K8S 雲原生架構探索, SENTRY FOR JAVASCRIPT 手動捕獲事件基本用法](https://mp.weixin.qq.com/s/4b4ojLkKGM6QX0z4Jq8nNQ)
9. [Sentry(v20.12.1) K8S 雲原生架構探索,SENTRY FOR JAVASCRIPT Source Maps 詳解](https://mp.weixin.qq.com/s/-FySq0YUf2lJY2DjOORcIg)
10. [Sentry(v20.12.1) K8S 雲原生架構探索,SENTRY FOR JAVASCRIPT 故障排除](https://mp.weixin.qq.com/s/BHyLOCI53EUoswUjm1-dXw)
11. [Sentry(v20.12.1) K8S 雲原生架構探索,1分鐘上手 JavaScript 效能監控](https://mp.weixin.qq.com/s/Z3Dp1fyzAEc5c89vy3Bzmw)
12. [Sentry(v20.12.1) K8S 雲原生架構探索,JavaScript 效能監控之管理 Transactions](https://mp.weixin.qq.com/s/Si2GLgGKsCwErJjJKFvtQg)
你可以通過兩種方式控制傳送到 Sentry 的 transactions 的量。
## Uniform Sample Rate
如果您希望 transactions 的 cross-section 均勻,無論您在應用程式中的何處或在什麼情況下發生,並且對下文所述的預設繼承和優先順序行為感到滿意,設定統一取樣率都是一個不錯的選擇。
為此,請將 `Sentry.init()` 中的 `tracesSampleRate` 選項設定為 0 到 1 之間的一個數字。設定此選項後,建立的每個 transaction 將有一定百分比的機會被髮送到 Sentry。(因此,例如,如果將 `tracesSampleRate` 設定為 `0.2`,將記錄和傳送大約 20% 的 transactions。)如下所示:
```js
Sentry.init({
// ...
tracesSampleRate: 0.2,
});
```
## Dynamic Sampling Function
如果您滿足以下條件,則提供取樣功能是一個不錯的選擇:
* 想要以不同的 rates 取樣不同的 transactions
* 想要完全過濾掉一些 transactions
* 要修改下面描述的預設優先順序和繼承行為
若要進行動態取樣,請將 `Sentry.init()` 中的 `tracesSampler` 選項設定為一個函式,該函式將接受 `samplingContext` 物件並返回 0 到 1 之間的取樣率。例如:
```js
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,它至少包括以下內容:
```js
// 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 傳送的資訊),這將非常有用。例如:
```js
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` 函式可以返回一個布林值,這樣,如果這是期望的行為,則可以直接返回父級的決策。
```js
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`,因此您可以指望通過的決策不會被覆蓋。
```js
Sentry.startTransaction({
name: "Search from navbar",
sampled: true,
});
```
## Precedence
transaction 有多種方法可以得出抽樣決策。
* 根據在 `tracesSampleRate` 中設定的靜態取樣率進行隨機取樣
* 根據 `tracesSampler` 返回的動態取樣率進行隨機取樣
* `tracesSampler` 返回的絕對決策(100% 機會或 0% 機會)
* 如果 transaction 有父級,則繼承其父級的抽樣決策
* 絕對決策權傳遞給 `startTransaction`
當有可能出現不止一種情況時,應遵循以下優先規則:
1. 如果將抽樣決策傳遞給 `startTransaction`(請參見上面的 Forcing a Sampling Decision),則無論其他任何因素,都將使用該決策。
2. 如果定義了 `tracesSampler` ,則將使用其決策。它可以選擇保留或忽略任何父抽樣決策,或者使用抽樣上下文資料做出自己的決策,或者為 transaction 選擇抽樣率。
3. 如果未定義 `tracesSampler`,但是有一個父取樣決策,則將使用父取樣決策。
4. 如果未定義 `tracesSampler` 且沒有父級取樣決策,則將使用 `tracesSampleRate`。
中文文件陸續同步到:
* https://getsentry.hacker-linner.com
```
我是為少。
微信:uuhells123。
公眾號:黑客下午茶。
謝謝點贊支援