7.x 中的弃用
了解 Sentry JavaScript SDK 7.x 中的弃用内容以及如何处理它们
要修复 7.x
中的大部分弃用问题,您可以使用 @sentry/migr8
codemod 自动更新您的 SDK 使用方式。@sentry/migr8
需要 Node 18+。
npx @sentry/migr8@latest
我们的迁移工具将允许您选择要运行的更新,并自动更新您的代码。在某些情况下,我们无法自动为您更改代码。这些情况将被标记为 TODO(sentry)
注释。请确保在运行 @sentry/migr8
后审查所有代码更改!
以下是在 7.x
中最重要的弃用列表。请参阅 GitHub 上的 详细迁移文档,以获取所有弃用的综合列表。
弃用引入于 7.100.0
.
在 v7 中,集成是类,并可以像 integrations: [new Sentry.Integrations.ContextLines()]
这样添加。在 v8 中,集成将不再是类,而是函数。使用类的方式以及从 Integrations.XXX
哈希中访问集成的方式已被弃用,取而代之的是使用新的函数式集成。
- 例如,
new Integrations.LinkedErrors()
变为linkedErrorsIntegration()
。
以下列表显示了如何迁移集成:
客户端集成:
Old | New |
---|---|
new BrowserTracing() | browserTracingIntegration() |
new InboundFilters() | inboundFiltersIntegration() |
new FunctionToString() | functionToStringIntegration() |
new LinkedErrors() | linkedErrorsIntegration() |
new ModuleMetadata() | moduleMetadataIntegration() |
new Replay() | replayIntegration() |
new ReplayCanvas() | replayCanvasIntegration() |
new Feedback() | feedbackIntegration() |
new CaptureConsole() | captureConsoleIntegration() |
new Debug() | debugIntegration() |
new Dedupe() | dedupeIntegration() |
new ExtraErrorData() | extraErrorDataIntegration() |
new ReportingObserver() | reportingObserverIntegration() |
new RewriteFrames() | rewriteFramesIntegration() |
new SessionTiming() | sessionTimingIntegration() |
new HttpClient() | httpClientIntegration() |
new ContextLines() | contextLinesIntegration() |
new Breadcrumbs() | breadcrumbsIntegration() |
new GlobalHandlers() | globalHandlersIntegration() |
new HttpContext() | httpContextIntegration() |
new TryCatch() | browserApiErrorsIntegration() |
new BrowserProfilingIntegration() | browserProfilingIntegration() |
弃用引入于 7.100.0
.
BrowserTracing
集成及其自定义路由 instrumentation 在 v8 中已被弃用。
相反,您应该使用 Sentry.browserTracingIntegration()
。
import * as Sentry from "@sentry/browser";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
- integrations: [new Sentry.BrowserTracing()],
+ integrations: [Sentry.browserTracingIntegration()],
});
只有在您之前明确使用了 BrowserTracing
集成的情况下,才需要为加载器添加 integrations
配置。
弃用引入于 7.94.0
.
这弃用了客户端上的 getIntegrationById()
和 getIntegration()
。相反,应使用 getIntegrationByName()
。您可以选择传递一个集成泛型以方便使用。
const replay = getClient().getIntegrationByName<Replay>("Replay");
弃用引入于 7.110.0
.
Hub
一直是 Sentry SDK API 中非常重要的一部分。Hubs 是 SDK 的“并发单元”,用于跨线程跟踪数据并将数据作用域限定在代码的某些部分。由于它过于复杂且容易让高级用户感到困惑,它将被一组新的 API(即“新的 Scope API”)所取代。
Scope
在 SDK 中早已存在,但现在我们对其进行了扩展,因为我们发现它们足够强大,可以完全覆盖 Hub
API。
如果您当前正在使用 Hub
,请参阅以下表格以了解如何迁移到新 API:
旧 Hub API | 新 Scope API |
---|---|
new Hub() | withScope() ,withIsolationScope() 或 new Scope() |
hub.isOlderThan() | 已移除 - 用于比较 Hub 实例,这些实例将被移除 |
hub.bindClient() | scope.setClient() 和 client.init() 的组合 |
hub.pushScope() | Sentry.withScope() |
hub.popScope() | Sentry.withScope() |
hub.withScope() | Sentry.withScope() |
getClient() | Sentry.getClient() |
getScope() | 使用 Sentry.getCurrentScope() 获取当前活动的作用域 |
getIsolationScope() | Sentry.getIsolationScope() |
getStack() | 已移除 - 用于保存作用域的堆栈。现在直接使用作用域 |
getStackTop() | 已移除 - 用于保存作用域的堆栈。现在直接使用作用域 |
captureException() | Sentry.captureException() |
captureMessage() | Sentry.captureMessage() |
captureEvent() | Sentry.captureEvent() |
addBreadcrumb() | Sentry.addBreadcrumb() |
setUser() | Sentry.setUser() |
setTags() | Sentry.setTags() |
setExtras() | Sentry.setExtras() |
setTag() | Sentry.setTag() |
setExtra() | Sentry.setExtra() |
setContext() | Sentry.setContext() |
configureScope() | 已移除 - 现在作用域是并发单元 |
run() | Sentry.withScope() 或 Sentry.withIsolationScope() |
getIntegration() | client.getIntegration() |
startTransaction() | Sentry.startSpan() ,Sentry.startInactiveSpan() 或 Sentry.startSpanManual() |
traceHeaders() | 已移除 - 最接近的等效方法是 spanToTraceHeader(getActiveSpan()) |
captureSession() | Sentry.captureSession() |
startSession() | Sentry.startSession() |
endSession() | Sentry.endSession() |
shouldSendDefaultPii() | 已移除 - 最接近的等效方法是 Sentry.getClient().getOptions().sendDefaultPii |
Hub
构造函数也已被弃用,并将在下一个主要版本中移除。如果您像这样为多客户端使用创建 Hubs:
// OLD
const hub = new Hub();
hub.bindClient(client);
makeMain(hub);
而是如下初始化客户端:
// NEW
Sentry.withIsolationScope(() => {
Sentry.setCurrentClient(client);
client.init();
});
如果您使用 Hub 捕获事件如下:
// OLD
const client = new Client();
const hub = new Hub(client);
hub.captureException();
而是如下捕获隔离的事件:
// NEW
const client = new Client();
const scope = new Scope();
scope.setClient(client);
scope.captureException();
弃用引入于 7.85.0
.
您应该使用 addEventProcessor
而不是 addGlobalEventProcessor
,前者不会将事件处理器添加到全局,而是添加到当前客户端。
在大多数情况下,这两种方法的行为应该是相同的。只有在您有多个客户端的情况下才会有所不同——但那时您可能希望为每个客户端添加事件处理器,而不是全局添加。
在 v8 中,我们将完全移除全局事件处理器,因为这可以避免保持不必要的全局状态。
弃用引入于 7.89.0
.
您应该通过 getCurrentScope()
访问作用域并直接配置它,而不是通过 configureScope()
在回调中更新作用域:
Sentry.getCurrentScope().setTag("xx", "yy");
弃用引入于 7.89.0
.
您应该使用 Sentry.withScope(callback: (scope: Scope))
而不是手动推送/弹出作用域。
弃用引入于 7.93.0
.
在 v8 中,启动新 span 的 API 将从当前可用的选项中减少。从现在起,只有以下参数可以传递给 startSpan()
、startSpanManual()
和 startInactiveSpan()
:
name
attributes
origin
op
startTime
scope
弃用引入于 7.93.0
.
在 v8 中,旧的性能 API startTransaction()
(和 hub.startTransaction()
)以及 span.startChild()
将被移除。相反,请使用新的性能 API:
startSpan()
startSpanManual()
startInactiveSpan()
弃用引入于 7.100.0
.
从现在起,tracesSampler
回调函数将直接接收 name
和 attributes
,而不是 transactionContext
。您可以使用这些信息来做出采样决策,而 transactionContext
将在 8.x
中被移除。请注意,attributes
仅包含在创建 span 时的属性,某些属性可能只会在 span 生命周期的后期设置(因此在采样期间不可用)。
弃用引入于 7.93.0
.
相反,您可以通过 Sentry.getActiveSpan()
获取当前活动的 span。在使用新的性能 API startSpan()
和 startSpanManual()
时,设置作用域上的 span 会自动发生。
弃用引入于 7.93.0
.
相反,您不应该依赖活动事务,而是直接使用 startSpan()
API,这些 API 会为您处理这些问题。