How to send error message to Elastic APM and show the error in Kibana tab

JasonREC
Mon, 03 Apr 2023 08:54:35 +0000
APM

I followed the set up of this page

elastic.co

Monitoring AWS Lambda Node.js Functions | APM Node.js Agent Reference [4.x] |...

I am using the template.yml to set up my lambda function and Elastic APM

...
Resources:
  yourLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Environment:
          Variables:
            NODE_OPTIONS: -r elastic-apm-node/start
            ELASTIC_APM_LAMBDA_APM_SERVER: <YOUR-APM-SERVER-URL>
            ELASTIC_APM_SECRET_TOKEN: <YOUR-APM-SECRET-TOKEN>
            ELASTIC_APM_SEND_STRATEGY: background

According to the offical webpage it said,

By default, the Node.js agent will watch for uncaught exceptions and send them to Elastic APM automatically. But in most cases, errors are not thrown but returned via a callback, caught by a promise, or simply manually created. Those errors will not automatically be sent to Elastic APM. To manually send an error to Elastic APM, simply call apm.captureError() with the error:

var err = new Error('Ups, something broke!')

apm.captureError(err)

I am wonder if I have to npm install the 'elastic-apm-node' and import it into my code so as to manually send an error to Elastic APM, like apm.captureError(err)

import apm from 'elastic-apm-node'
 apm.start({

            serviceName: serviecName,

            secretToken: secretToken,

            serverUrl: serviceUrl,
        })
apm.captureError(err)

But I have already stated that NODE_OPTIONS: -r elastic-apm-node/start in my template.yml, wouldn't it be started the elastic apm agent twice!?

Could anyone help?
Thanks

10 posts - 3 participants