0

I am using the package elastic-apm-node for sending the APM data to Elastic via Nodejs app. I need to do something like apm.captureError(error) to send the error to Elastic.

Is there some where wherein i can send simple console.log() to Elastic ?

I basically need functionality wherein i can track every function getting called in my App.

Function A (){
  console.log("Function A called , send to Elastic");
  doSomething()
}

Function doSomething(){
  console.log("Function doSomething called, send to Elastic");
}

I got some example using Elastic FileBeat , but i think this should be achievable using APM as the connection is already made between the app and Elastic.

Gaurav_soni
  • 6,064
  • 8
  • 32
  • 49

1 Answers1

0

APM libraries like that (from any provider, it's the same with NewRelic or Datadog, etc.) work by instrumenting (basically, wrapping) functions in common libraries like Express and Koa, so they automatically pick up on function calls like executing routes, but won't pick up on something else unrelated. And APM is for performance (and error) monitoring; logs are really a separate concern, which is why there's a separate solution for logs.

But you could do it with custom spans, and overriding console.log or writing a wrapper. Example:

const elasticLog = (data) => {
  const span = apm.startSpan(data)
  span.end()
}

const logger = process.env.NODE_ENV === 'production' ? elasticLog : console.log

function foo () {
  logger('In foo')
  bar()
}

function bar () {
  logger('In bar')
}

As for automatically detecting names of called functions, that's tricky. And running the logger on every function that's called without having to actually call it is pretty much impossible in this sort of context.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42