1

在 Nuxtjs 项目中配置/启用 Elastic 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'
})

有没有其他方法可以做到这一点?

4

4 回答 4

3

我们设法使用自定义 Nuxt 模块来实现这一点,该模块明确要求 Node 模块在启动 APM 模块后进行检测。

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
          }
        }
      }
    };
于 2021-08-27T11:00:45.390 回答
1

根据我所见,使用 stocknuxt命令行应用程序似乎没有“正确”的方法来执行此操作。问题似乎是,虽然nuxt.config.js用户第一次有机会添加一些 javascript,但nuxt命令行应用程序在此配置文件为required. 这意味着弹性代理(或任何 APM 代理)没有机会连接到模块中。

Nuxt 团队目前的建议似乎是

  1. nuxt通过手动调用-r

         {
           "scripts": {
             "start": "node -r elastic-apm-node node_modules/nuxt/.bin/nuxt"
           }
         }  
    
  2. 在您选择的框架中以编程方式跳过nuxt使用 NuxtJS作为中间件

         const { loadNuxt } = require('nuxt')
         const nuxtPromise = loadNuxt('start')
         app.use((req, res) => { nuxtPromise.then(nuxt => nuxt.render(req, res)) })
    
于 2020-12-29T20:34:40.397 回答
1

基于 Alan Storm 的回答(来自 Nuxt 团队),我使它工作但稍作修改:

  • 我创建了一个名为 nodeApm.js 的文件,在其中添加了以下代码:
    const nodeApm = require('elastic-apm-node')
    
    if (!nodeApm.isStarted()) { ... // configuration magic }
    
  • 在脚本部分中,我添加了:
    "start": "node -r ./nodeApm.js node_modules/nuxt/.bin/nuxt"
    
于 2021-01-31T10:30:39.620 回答
1

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

要使其工作,请按照下列步骤操作:

1.) 在你的根目录中创建一个 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 文件夹丢失了“。” 在目录名中

!在所有其他答案中,人们忘记了最后的开始参数

“开始”:“节点 -r ./nodeApm.js node_modules/nuxt/bin/nuxt开始”,

于 2022-02-17T16:04:52.547 回答