instrumentation.js

instrumentation.js|ts 檔案用於整合監控與記錄工具到您的應用程式中。這讓您可以追蹤應用程式的效能與行為,並在生產環境中偵錯問題。

要使用它,請將檔案放置在應用程式的根目錄,或如果您有使用 src 資料夾 的話,放在該資料夾內。

設定選項

Instrumentation 目前是實驗性功能,要使用 instrumentation 檔案,您必須在 next.config.js 中明確啟用 experimental.instrumentationHook = true;

next.config.js
module.exports = {
  experimental: {
    instrumentationHook: true,
  },
}

匯出項目

register (必要)

該檔案需匯出一個 register 函式,此函式會在初始化新的 Next.js 伺服器實例時被呼叫一次register 可以是非同步函式。

import { registerOTel } from '@vercel/otel'

export function register() {
  registerOTel('next-app')
}

版本歷史

版本變更內容
v14.0.4新增對 instrumentation 的 Turbopack 支援
v13.2.0以實驗性功能形式引入 instrumentation

On this page