Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之采样 Transactions

  • 2021 年 1 月 22 日
  • 筆記

系列

  1. Sentry-Go SDK 中文实践指南
  2. 一起来刷 Sentry For Go 官方文档之 Enriching Events
  3. Snuba:Sentry 新的搜索基础设施(基于 ClickHouse 之上)
  4. Sentry 10 K8S 云原生架构探索,Vue App 1 分钟快速接入
  5. Sentry(v20.12.1) K8S云原生架构探索,玩转前/后端监控与事件日志大数据分析,高性能高可用+可扩展可伸缩集群部署
  6. Sentry(v20.12.1) K8S 云原生架构探索,Sentry JavaScript SDK 三种安装加载方式
  7. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT SDK 配置详解
  8. Sentry(v20.12.1) K8S 云原生架构探索, SENTRY FOR JAVASCRIPT 手动捕获事件基本用法
  9. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT Source Maps 详解
  10. Sentry(v20.12.1) K8S 云原生架构探索,SENTRY FOR JAVASCRIPT 故障排除
  11. Sentry(v20.12.1) K8S 云原生架构探索,1分钟上手 JavaScript 性能监控
  12. Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之管理 Transactions

你可以通过两种方式控制发送到 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 时,传递给 tracesSamplersamplingContext 对象中包含的信息因平台和集成而异。

对于基于浏览器的 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。如果您希望采样器可以访问但不想将其作为 tagsdata 附加到 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

当有可能出现不止一种情况时,应遵循以下优先规则:

  1. 如果将抽样决策传递给 startTransaction(请参见上面的 Forcing a Sampling Decision),则无论其他任何因素,都将使用该决策。
  2. 如果定义了 tracesSampler ,则将使用其决策。它可以选择保留或忽略任何父抽样决策,或者使用抽样上下文数据做出自己的决策,或者为 transaction 选择抽样率。
  3. 如果未定义 tracesSampler,但是有一个父采样决策,则将使用父采样决策。
  4. 如果未定义 tracesSampler 且没有父级采样决策,则将使用 tracesSampleRate

中文文档陆续同步到:

我是为少。
微信:uuhells123。
公众号:黑客下午茶。
谢谢点赞支持👍👍👍!