# Distributed tracing with custom transactions (Node.js agent)

**URL:** <https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671>\
**Category:** APM\
**Created:** [January 17, 2019, 3:35pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671 "2019-01-17T15:35:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![treble.snake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/treble.snake/32/39818_2.png) [@treble.snake](https://discuss.elastic.co/u/treble.snake)\
**Post date:** [January 17, 2019, 3:35pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/1 "2019-01-17T15:35:45Z")

</div>

Hi,

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.

```auto
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:

```auto
await callChild(
  transaction._context.traceId,
  transaction._context.id);

```

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

```auto
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?

Thanks!

---

<div class="post-metadata">

**Author:** ![stephenbelanger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenbelanger/32/34880_2.png) [@stephenbelanger](https://discuss.elastic.co/u/stephenbelanger)\
**Post date:** [January 21, 2019, 6:47pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/2 "2019-01-21T18:47:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![treble.snake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/treble.snake/32/39818_2.png) [@treble.snake](https://discuss.elastic.co/u/treble.snake)\
**Post date:** [January 21, 2019, 8:22pm UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/3 "2019-01-21T20:22:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![treble.snake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/treble.snake/32/39818_2.png) [@treble.snake](https://discuss.elastic.co/u/treble.snake)\
**Post date:** [January 22, 2019, 11:37am UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/4 "2019-01-22T11:37:39Z")

</div>

It worked, thanks!

---

<div class="post-metadata">

**Author:** ![stephenbelanger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenbelanger/32/34880_2.png) [@stephenbelanger](https://discuss.elastic.co/u/stephenbelanger)\
**Post date:** [January 23, 2019, 1:07am UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/5 "2019-01-23T01:07:32Z")

</div>

Great! Glad I could help. 🙂

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.

---

<div class="post-metadata">

**Author:** ![wa7son](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wa7son/32/21259_2.png) [@wa7son](https://discuss.elastic.co/u/wa7son)\
**Post date:** [January 23, 2019, 9:16am UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/6 "2019-01-23T09:16:00Z")

</div>

Just to weigh in on what Stephen said above: I suggest that you pay attention to our [CHANGELOG](https://github.com/elastic/apm-agent-nodejs/blob/master/CHANGELOG.md) 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:

```auto
const traceparent = transaction._context && transaction._context.toString()

```

---

<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:** [February 13, 2019, 5:16am UTC](https://discuss.elastic.co/t/distributed-tracing-with-custom-transactions-node-js-agent/164671/7 "2019-02-13T05:16:01Z")

</div>

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