在Nuxtjs项目中配置/启用弹性APM代理的正确方法是什么?
我向这份文件推荐了一个定制的NodeJS应用程序。关键的外卖是:
重要的是,在您需要Node.js应用程序中的任何其他模块之前,即在http之前和路由器之前启动代理。
我在nuxt.config.js中添加了以下代码片段,但APM代理没有启动或工作。我没有在应用程序日志中看到任何错误。
var apm = require('elastic-apm-node').start({
serviceName: 'nuxt-app',
serverUrl: 'http://ELK_APM_SERVER:8200'
})
还有别的办法吗?
发布于 2020-12-29 20:34:40
根据我所看到的,在股票nuxt
命令行应用程序中,似乎没有一种“正确”的方法来做到这一点。问题似乎是,虽然nuxt.config.js
是用户第一次有机会添加一些javascript,但nuxt
命令行应用程序在此配置文件为required
之前引导节点的HTTP框架。这意味着弹性代理(或任何APM代理)没有机会与模块挂钩。
来自Nuxt团队的目前的建议似乎是
nuxt
手动调用-r
{“脚本”:{“开始”:“节点-r弹性-apm-节点_模块/nuxt/..bin/nuxt”}}nuxt
和以编程方式使用NuxtJS作为您选择的框架中的中间件
const { loadNuxt }=需要量(‘nuxt’) const nuxtPromise = loadNuxt('start') app.use((req,res) => { nuxtPromise.then(nuxt => nuxt.render(req,res) }) }发布于 2021-08-27 11:00:45
我们成功地使用了一个定制的Nuxt模块,该模块在启动APM模块后显式地要求Node模块进行检测。
modules/elastic-apm.js
const apm = require('elastic-apm-node');
const defu = require('defu');
module.exports = function() {
this.nuxt.hook('ready', async(nuxt) => {
const runtimeConfig = defu(nuxt.options.privateRuntimeConfig, nuxt.options.publicRuntimeConfig);
const config = (runtimeConfig.elastic && runtimeConfig.elastic.apm) || {};
if (!config.serverUrl) {
return;
}
if (!apm.isStarted()) {
await apm.start(config);
// Now explicitly require the modules we want APM to hook into, as otherwise
// they would not be instrumented.
//
// Docs: https://www.elastic.co/guide/en/apm/agent/nodejs/master/custom-stack.html
// Modules: https://github.com/elastic/apm-agent-nodejs/tree/master/lib/instrumentation/modules
require('http');
require('http2');
require('https');
}
});
}
nuxt.config.js
module.exports = {
// Must be in modules, not buildModules
modules: ['~/modules/elastic-apm'],
publicRuntimeConfig: {
elastic: {
apm: {
serverUrl: process.env.ELASTIC_APM_SERVER_URL,
serviceName: 'my-nuxt-app',
usePathAsTransactionName: true // prevent "GET unknown route" transactions
}
}
}
};
发布于 2022-02-17 16:04:52
所有答案都过时了,从一开始就不正确(17.02.2022)
要使它正常工作,请执行以下步骤:
1.)在根dir中创建具有以下内容的nodeApm.js:
const nodeApm = require('elastic-apm-node')
if (!nodeApm.isStarted()) {
nodeApm.start()
}
2.)使用环境变量来存储您的配置。例如:
ELASTIC_APM_SERVICE_NAME=NUXT_PRODUCTION
ELASTIC_APM_SECRET_TOKEN=yoursecrettokenhere
3.)编辑您的package.json
"scripts": {
// if you want apm also on dev to test, add it also here
"dev": "node -r ./nodeApm.js node_modules/nuxt/bin/nuxt",
...
"start": "node -r ./nodeApm.js node_modules/nuxt/bin/nuxt start",
...
好了!如果是在2022年,node_modules bin文件夹丢失了“。在目录名中
好了!在所有的anwsers中,人们忘记了末尾的start参数。
“开始”:“节点-r ./nodeApm.js节点_/nuxt/bin/nuxt start",
https://stackoverflow.com/questions/65484826
复制相似问题