# No APM event when sending from a node child process

**URL:** <https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862>\
**Category:** APM\
**Tags:** nodejs\
**Created:** [October 5, 2022, 10:44am UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862 "2022-10-05T10:44:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre-aurele-martin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre-aurele-martin/32/111687_2.png) [@pierre-aurele-martin](https://discuss.elastic.co/u/pierre-aurele-martin)\
**Post date:** [October 5, 2022, 10:44am UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/1 "2022-10-05T10:44:16Z")

</div>

**Kibana version** : Latest

**Elasticsearch version** : Latest

**APM Server version** : Latest

**APM Agent language and version** : Node latest

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**: pnpm

**Fresh install or upgraded from other version?** upgraded

**Is there anything special in your setup?** No

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

I hope this code is clear enough. Basically my issue is that I got nothing when using apm client in a node child process. I must be missing something but no idea what at this point.

This is the code I init with a simple node cmd:

```javascript
const { elasticApm } = require('@outmind/apm');
const cp = require('child_process');

const apmconf = {
  environment: 'staging',
  secretToken: 'xxx
  serverUrl: 'https://xxx:443',
  serviceName: 'deepsyncengine',
  serviceVersion: '1.0.0',
  active: true,
  logLevel: 'trace',
};

elasticApm.start(apmconf);

const getHours = () => {
  const a = new Date();
  return `${a.getHours()}:${a.getMinutes()}:${a.getSeconds()}`;
};

console.log('elasticApm is started => ', elasticApm.isStarted());

const service = async () => {
  const h = getHours();
  console.log('service is started', h);
  const transaction = elasticApm.startTransaction(`TEST-${h} Transaction started in my test`);
  console.log('elasticApm current transaction ==>', elasticApm.currentTraceparent);

  const span = elasticApm.startSpan('THIS IS A SPAN START');
  await new Promise((res, rej) => setTimeout(() => res(), 1500));
  span.end();

  const childProcess = cp.fork('./src/file-parser/pamworker.js', {});

  childProcess.send({
    parentTransaction: elasticApm.currentTraceparent,
    // apmClient: elasticApm,
  });

  childProcess.on('error', (e) => {
    console.log('error in childprocess ==>', e);
  });

  childProcess.on('exit', (code, signal) => {
    console.log('childprocess exit ==>', code, signal);

    transaction.end('success');
    console.log('transaction is ended', transaction.ended);

    console.log('service is endend');
  });
};

service();

```

This is the code of the worker in child process :

```javascript
const { elasticApm, makeApmConfig } = require('@outmind/apm');
const apmconf = {
  environment: 'staging',
  secretToken: 'xxx',
  serverUrl: 'https://xxx:443',
  serviceName: 'deepsyncengine',
  serviceVersion: '1.0.0',
  active: true,
  logLevel: 'trace',
};

const getHours = () => {
  const a = new Date();
  return `${a.getHours()}:${a.getMinutes()}:${a.getSeconds()}`;
};

process.on('message', async (data) => {
  elasticApm.start(apmconf);

  const d = getHours();
  console.log('message in worker data ==>', data, d);
  console.log('apm started in worker ==>', elasticApm.isStarted());

  const orphanTrans = elasticApm.startTransaction(`Worker orphanTrans ${d}`);

  // console.log('have a look at span worker ==>', orphanTrans);
  await new Promise((res, rej) => setTimeout(() => res(), 2000));
  orphanTrans.end('success');
  console.log('orphan trans ended');

  const transaction = elasticApm.startTransaction('Worker transaction', {
    childOf: data.parentTransaction,
  });

  console.log('have a look at transaction worker ==>', transaction.ended);
  await new Promise((res, rej) => setTimeout(() => res(), 2000));
  transaction.end('success');

  console.log('transaction worker ended', transaction.ended);

  process.exit(0);
});

```

Just dummy code but it proves what I want to do.

I got all the log.  
I can see my transaction in the first file, including the span I attach to it.

I can NOT see any event sent from the worker. Neither the orphan transaction nor the childOf.  
No error. The strack trace looks fine to me (very long but I can attach it to this post).

I'd appreciate any help on this !

Thanks all.

P.S: I did read this: [APM not collecting data for NodeJS applications which trigger services inside child processes. But correctly monitor in NodeJS express apps](https://discuss.elastic.co/t/apm-not-collecting-data-for-nodejs-applications-which-trigger-services-inside-child-processes-but-correctly-monitor-in-nodejs-express-apps/223056)

---

<div class="post-metadata">

**Author:** ![pierre-aurele-martin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre-aurele-martin/32/111687_2.png) [@pierre-aurele-martin](https://discuss.elastic.co/u/pierre-aurele-martin)\
**Post date:** [October 5, 2022, 10:52am UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/2 "2022-10-05T10:52:51Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/2/a/2a942f5bedb7aa5fcc9a1d24e1f298cd61cb9900.png)

This is what I got.

Transaction from main process OK.  
Span from main process ok.  
Delay from child process computing does show. Nothing in it.

I can not find it as an independent transaction either.

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [October 5, 2022, 2:41pm UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/3 "2022-10-05T14:41:14Z")

</div>

Hey there @pierre-aurele-martin -- thanks for giving the agent a try. We're going to try to reproduce the behavior you're seeing but have two quick questions first

1. Is there anything we should know about that's going on in the `@outmind/apm` package that's not standard in the agent

2. Is there a `process.exit` in your actual code or is that just there for the repro.

Re: #2 -- with the information provided so far my guess is that you're exiting the child process before its agent instance has a chance to send data off to elastic.

---

<div class="post-metadata">

**Author:** ![pierre-aurele-martin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre-aurele-martin/32/111687_2.png) [@pierre-aurele-martin](https://discuss.elastic.co/u/pierre-aurele-martin)\
**Post date:** [October 5, 2022, 2:45pm UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/4 "2022-10-05T14:45:33Z")

</div>

Hi @alanstorm

Thanks for having a look.

@outmind/apm is just:

```javascript
// make-apm-config is just to wrap some default config - nothing interesting
export * from './make-apm-config';
export { default as elasticApm } from 'elastic-apm-node';

```

There is a process.exit in my actual code. you make an interesting point, I'll timeout the process.exit for a few seconds and see if that's better 🙂

---

<div class="post-metadata">

**Author:** ![alanstorm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alanstorm/32/77134_2.png) [@alanstorm](https://discuss.elastic.co/u/alanstorm)\
**Post date:** [October 5, 2022, 2:49pm UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/5 "2022-10-05T14:49:51Z")

</div>

_nod_ -- also, better than the timeout, the agent API has a flush method

> **[Agent API | APM Node.js Agent Reference \[master\] | Elastic](https://www.elastic.co/guide/en/apm/agent/nodejs/master/agent-api.html#apm-flush)**

You can tell the agent to send all its data and then call the callback you pass to flush. Naively, that seems like a good place to kill the child process.

---

<div class="post-metadata">

**Author:** ![pierre-aurele-martin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre-aurele-martin/32/111687_2.png) [@pierre-aurele-martin](https://discuss.elastic.co/u/pierre-aurele-martin)\
**Post date:** [October 5, 2022, 3:01pm UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/6 "2022-10-05T15:01:16Z")

</div>

That's the solution ! Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 26, 2022, 11:01am UTC](https://discuss.elastic.co/t/no-apm-event-when-sending-from-a-node-child-process/315862/7 "2022-10-26T11:01:21Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
