SuperAPI AI 工具集

sentry-span-streaming-js

部署与运维

查看项目

更新于 10-15 21:51

技能介绍

用途:把 Sentry JavaScript SDK 从默认的 transaction 静态追踪生命周期迁移到 span streaming,使 span 完成时即单独发送,而非最后批量打包成 transaction,从而降低延迟并支持按 span 处理。适用场景为用户要求“启用 span streaming”“迁移到 span streaming”,或提到 traceLifecycle、spanStreamingIntegration、withStreamedSpan,或希望更低延迟的 span 投递。

主要步骤:先检测环境与现有 Sentry 配置,区分浏览器、服务端或框架 SDK;再逐处修改 Sentry.init。服务端 SDK 添加 traceLifecycle: 'stream';浏览器 SDK 在 integrations 中加入 Sentry.spanStreamingIntegration(),该集成会自动启用流式模式。框架 SDK 需分别迁移客户端与服务端配置,如 Next.js 的 sentry.client.config.ts 与 sentry.server.config.ts、SvelteKit 的 src/hooks.client.ts 与 src/hooks.server.ts 等。

迁移要点:beforeSendSpan 回调必须用 Sentry.withStreamedSpan() 包裹,否则回退到静态模式,且字段变化为 description 改为 name、data 改为 attributes,span 类型为 StreamedSpanJSON。流式模式下 tag 不再作用于 span,需在保留原 setTag/setTags 的同时补充对应的 setAttribute/setAttributes。beforeSendTransaction 在流式模式下无效,应按用途改用 ignoreSpans 或 beforeSendSpan 后移除。可选配置 ignoreSpans,支持名称、操作与属性的字符串或正则匹配;浏览器可选配置 v2 剖析,使用 browserProfilingIntegration()、profileSessionSampleRate 与 profileLifecycle,不要混用旧的 profilesSampleRate。

验证与限制:迁移后运行类型检查与构建,并在浏览器开发者工具中确认 span 以独立请求、内容类型 application/vnd.sentry.items.span.v2+json 出现。前提为 SDK 版本 >=10.66.0 且已启用 tracing。以上未提及的价格、平台支持范围等信息暂未提供。