Distributed tracing with custom transactions (Node.js agent)


I wonder if it's possible to do distributed tracing with custom transactions?
I see there is an automated distributed tracing for services communicating via HTTP.
But what about other means of IPC, like message brokers or child processes, and custom transactions?

I was experimenting with child processes, but haven't got much of a result.
I tried to do the following:

On master process I started a transaction as usual.

const transaction = agent.startTransaction('dt-master', 'job');

Then I tried to extract and pass to the child process transaction and trace IDs, which actually already seemed hack-ish:

await callChild(

Then I used these IDs as the 3rd argument to the agent.startTransaction() function in a child process:

const childTransaction = agent.startTransaction('dt-child', 'job', id);
// or
const childTransaction = agent.startTransaction('dt-child', 'job', traceId);

Then I added some spans to both transactions and flushed the agent.

But in both cases I got two separate transactions in APM UI.

So, I wonder, is there any way to do it properly?


The third argument to agent.startTransaction(...) should be the context object itself. You can do transaction._context.toString() to get the complete context in a string format to pass to the other process. Also, I would recommend using the context of the nearest span, if you can, rather than a transaction, so the UI can nest it better.

Thanks a lot for your answer, I'm gonna try it tomorrow.

It worked, thanks!

Great! Glad I could help. :slight_smile:

Also, just a minor note: we aren't currently officially supporting the argument to pass in the traceparent header value, so it's possible that interface could change. It seems like there's a use case for it though, so I'll see about making that official.

1 Like

Just to weigh in on what Stephen said above: I suggest that you pay attention to our CHANGELOG to see when we release official support for this.

And I would also urge you to guard against what will happen if _context is undefined as this is a private API that might change in a patch version without warning. So you could do something like this:

const traceparent = transaction._context && transaction._context.toString()
1 Like

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