安裝
安裝跟蹤軟件包:
ESM
# Using yarn yarn add @sentry/tracing # Using npm npm install @sentry/tracing
CDN
<script <!-- Note that `bundle.tracing.min.js` contains both `@sentry/browser` AND `@sentry/tracing`, and should therefore be used in place of `@sentry/browser`'s bundle rather than in addition to it. --> src="https://browser.sentry-cdn.com/5.29.2/bundle.tracing.min.js" integrity="sha384-4zxA5Bnxor/VkZae20EqPP3A/6vDlw1ZhqF7EvpmeTfWYFjPIDdaUSOk/q7G/bYw" crossorigin="anonymous" ></script>
配置
通過以下兩種方式在您的應用中啟用性能監控:
- 使用 SDK 配置中的
tracesSampleRate
選項將所有transactions
的統一採樣率設置為0
到1
之間的數字。(例如,要發送20%
的transactions
,請將tracesSampleRate
設置為0.2
。) - 通過為
tracesSampler
配置選項提供功能,基於 transaction 本身及其捕獲的上下文動態控制採樣率。
ESM
// If you're using one of our integration packages, like `@sentry/react` or // `@sentry/angular`, substitute its name for `@sentry/browser` here import * as Sentry from "@sentry/browser"; // If taking advantage of automatic instrumentation (highly recommended) import { Integrations as TracingIntegrations } from "@sentry/tracing"; // Or, if only manually tracing // import * as _ from "@sentry/tracing" // Note: You MUST import the package in some way for tracing to work Sentry.init({ dsn: "https://[email protected]/0", // This enables automatic instrumentation (highly recommended), but is not // necessary for purely manual usage integrations: [new TracingIntegrations.BrowserTracing()], // To set a uniform sample rate tracesSampleRate: 0.2 // Alternatively, to control sampling dynamically tracesSampler: samplingContext => { ... } });
CDN
Sentry.init({ dsn: "https://[email protected]/0", // This enables automatic instrumentation (highly recommended), but is not // necessary for purely manual usage integrations: [new Sentry.Integrations.BrowserTracing()], // To set a uniform sample rate tracesSampleRate: 0.2 // Alternatively, to control sampling dynamically tracesSampler: samplingContext => { ... } });
如果設置了這些選項之一,則將在您的應用程序中啟用跟蹤。雖然這些選項是互斥的,但是如果您同時設置了這兩個選項,tracesSampler
將具有優先權。您可以在 Sampling Transactions 中瞭解有關它們如何工作的更多信息。
驗證
首次啟用跟蹤時,通過將 tracesSampleRate
設置為 1.0
來驗證其是否正常運行,因為這可以確保將每個事務發送到 Sentry。
一旦測試完成,我們建議在生產中降低這個值,方法是降低您的 tracesSampleRate
值,或者切換到使用 tracesSampler
來動態取樣和過濾您的 transaction。
在沒有采樣的情況下,我們的自動檢測將在任何用戶加載任何頁面或在應用程序中的任何位置導航時發送 transaction。那是很多 transactions!採樣可以實現代表性數據,而無需佔用系統或 Sentry transaction 配額。