You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在React中配置Sentry Scope并设置事务名称?

配置Scope与设置事务名称(基于profiler组件)

1. 初始化Scope与Profiler

先确保正确导入profiler组件及Scope相关模块,完成基础实例初始化:

const { profiler, Scope } = require('你的profiler依赖包');
// 启动profiler并配置基础服务信息
profiler.start({
  serviceName: '你的服务名称'
});
// 获取当前上下文的Scope实例
const currentScope = Scope.current();

2. 设置事务名称

在事务启动阶段,直接通过Scope实例或profiler的事务方法指定名称:

// 方式1:通过Scope设置当前事务名称
currentScope.setTransactionName('用户登录流程');

// 方式2:启动事务时直接指定名称
const transaction = profiler.startTransaction('订单结算流程');

3. 配置Scope上下文传递

如果需要在异步操作中保持Scope上下文关联,开启异步上下文传播(部分profiler组件默认支持,需根据实际API调整):

// 启用异步上下文传播
Scope.enableAsyncContext();

// 在异步任务中复用当前Scope
async function处理请求() {
  const scope = Scope.current();
  scope.setTransactionName('API请求处理');
  await 异步业务操作();
}

4. 验证配置有效性

可以通过打印日志或查看profiler监控面板,确认事务名称与Scope上下文是否正确绑定:

// 输出当前Scope关联的事务名称
console.log('当前事务名称:', Scope.current().getTransactionName());

内容的提问来源于stack exchange,提问作者Awais Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:57:28