首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向NuxtJS项目添加APM的最佳方法是什么?

向NuxtJS项目添加APM的最佳方法是什么?
EN

Stack Overflow用户
提问于 2020-12-28 22:19:51
回答 4查看 1.1K关注 0票数 1

在Nuxtjs项目中配置/启用弹性APM代理的正确方法是什么?

我向这份文件推荐了一个定制的NodeJS应用程序。关键的外卖是:

重要的是,在您需要Node.js应用程序中的任何其他模块之前,即在http之前和路由器之前启动代理。

我在nuxt.config.js中添加了以下代码片段,但APM代理没有启动或工作。我没有在应用程序日志中看到任何错误。

代码语言:javascript
运行
复制
var apm = require('elastic-apm-node').start({
  serviceName: 'nuxt-app',
  serverUrl: 'http://ELK_APM_SERVER:8200'
})

还有别的办法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-12-29 20:34:40

根据我所看到的,在股票nuxt命令行应用程序中,似乎没有一种“正确”的方法来做到这一点。问题似乎是,虽然nuxt.config.js是用户第一次有机会添加一些javascript,但nuxt命令行应用程序在此配置文件为required之前引导节点的HTTP框架。这意味着弹性代理(或任何APM代理)没有机会与模块挂钩。

来自Nuxt团队的目前的建议似乎是

  1. 通过nuxt手动调用-r {“脚本”:{“开始”:“节点-r弹性-apm-节点_模块/nuxt/..bin/nuxt”}}
  2. 跳过nuxt以编程方式使用NuxtJS作为您选择的框架中的中间件 const { loadNuxt }=需要量(‘nuxt’) const nuxtPromise = loadNuxt('start') app.use((req,res) => { nuxtPromise.then(nuxt => nuxt.render(req,res) }) }
票数 1
EN

Stack Overflow用户

发布于 2021-08-27 11:00:45

我们成功地使用了一个定制的Nuxt模块,该模块在启动APM模块后显式地要求Node模块进行检测。

modules/elastic-apm.js

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
    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
          }
        }
      }
    };
票数 4
EN

Stack Overflow用户

发布于 2022-02-17 16:04:52

所有答案都过时了,从一开始就不正确(17.02.2022)

要使它正常工作,请执行以下步骤:

1.)在根dir中创建具有以下内容的nodeApm.js:

代码语言:javascript
运行
复制
const nodeApm = require('elastic-apm-node')

if (!nodeApm.isStarted()) { 
    nodeApm.start()
}

2.)使用环境变量来存储您的配置。例如:

代码语言:javascript
运行
复制
ELASTIC_APM_SERVICE_NAME=NUXT_PRODUCTION
ELASTIC_APM_SECRET_TOKEN=yoursecrettokenhere

3.)编辑您的package.json

代码语言:javascript
运行
复制
"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",

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65484826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档