instrumentation.js
instrumentation.js|ts
文件用于将监视和日志记录工具集成到你的应用中。这使你可以跟踪应用的性能和行为,并调试生产中的问题。
¥The instrumentation.js|ts
file is used to integrate monitoring and logging tools into your application. This allows you to track the performance and behavior of your application, and to debug issues in production.
要使用它,请将文件放在应用的根目录中,或者如果使用的话,放在 src
文件夹 中。
¥To use it, place the file in the root of your application or inside a src
folder if using one.
配置选项
¥Config Option
检测目前是一项实验性功能,要使用 instrumentation
文件,你必须通过在 next.config.js
中定义 experimental.instrumentationHook = true;
来明确选择加入:
¥Instrumentation is currently an experimental feature, to use the instrumentation
file, you must explicitly opt-in by defining experimental.instrumentationHook = true;
in your next.config.js
:
module.exports = {
experimental: {
instrumentationHook: true,
},
}
导出
¥Exports
register
(必需的)
¥register
(required)
该文件导出一个 register
函数,该函数在启动新的 Next.js 服务器实例时调用一次。register
可以是异步函数。
¥The file exports a register
function that is called once when a new Next.js server instance is initiated. register
can be an async function.
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel('next-app')
}
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel('next-app')
}
版本历史
¥Version History
版本 | 变化 |
---|---|
v14.0.4 | Turbopack 对 instrumentation 的支持 |
v13.2.0 | instrumentation 作为实验性功能引入 |