# How to pass elastic-apm-traceparent and traceparent headers explicitly

**URL:** <https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640>\
**Category:** APM\
**Tags:** java\
**Created:** [July 26, 2020, 8:24am UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640 "2020-07-26T08:24:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [July 26, 2020, 8:24am UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/1 "2020-07-26T08:24:15Z")

</div>

Hi,

I have the following use case,  
Service 1 calls service 2, but it is not a normal service (sync/async) call, service 1 writes to its outbound MySQL table with all the data (request body, headers, URL, etc), later a daemon picks up entries in this table and calls the service 2.

In this scenario, elastic apm is not directly involved in the service 2 call, I was thinking, I will write the elastic-apm-traceparent and traceparent headers also to outbounds table so that it gets passed along by the daemon.

Is there a way I can get elastic-apm-traceparent and traceparent values in my application calss while writing to outbound table?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 26, 2020, 8:42am UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/2 "2020-07-26T08:42:46Z")

</div>

There is certainly a way!  
The [`injectTraceHeaders`](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-transaction-inject-trace-headers) and [`startTransactionWithRemoteParent`](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-start-transaction-with-remote-parent-header) APIs can be used. Implement a `HeaderInjector` to get the header name/value pairs through the `addHeader` method and store those. Then, implement a `HeaderExtractor` to read from DB when the `getFirstHeader` method is invoked.

---

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [July 26, 2020, 12:11pm UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/3 "2020-07-26T12:11:00Z")

</div>

Hi Eyal,  
Thanks for the reply.

One more question, there is a queuing system between my service 1 and service2. This queue only allows headers starting with "X-".

So in my above example, is there a way to tell service 2 agent that the elastic-apm-transparent is coming in X-elastic-apm-transparent ?

Otherwise, I may have to write custom code in the agent also.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 26, 2020, 12:24pm UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/4 "2020-07-26T12:24:35Z")

</div>

You can take care of that using the same - your `HeaderInjector#addHeader` implementation can append `X-` to the header name before storing it and the `HeaderExtractor#getFirstHeader` implementation can append `X-` to any requested header before looking it up.

---

<div class="post-metadata">

**Author:** ![manohar\_deepu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manohar_deepu/32/72797_2.png) [@manohar\_deepu](https://discuss.elastic.co/u/manohar_deepu)\
**Post date:** [July 26, 2020, 2:32pm UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/5 "2020-07-26T14:32:34Z")

</div>

Eyal,

The solution which you have given is still at Service 1 side - while storing in db and reading from db before sending to service 2.

My question is service 2 will get the header as X-elastic-apm-transparent. Even if the service 2 agent understand X-elastic-apm-transparent is indeed elastic-apm-transparent ?

Do I need to do anything extra since service 2 agent is getting header as X-elastic-apm-transparent ?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 26, 2020, 3:21pm UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/6 "2020-07-26T15:21:56Z")

</div>

In service 1, when the call is made, you invoke `injectTraceHeaders` and the `HeaderInjector` implementation will be called by the agent with a header name and value. In this place, you can add `X-` to the header name and store it with the given value.  
When the daemon reads it from the DB, IIUC, it will end up doing the actual external call, with the `X-` header. Then, on the accepting side, you would call `startTransactionWithRemoteParent`, where the `HeaderExtractor` implementation needs to look for a `X-` header, so it would retrieve the request header from the payload, but with an appended `X-`.  
I hope this helps. If I didn't understand you setup fully, read through the documentation of these APIs and I think you should figure out a way to achieve that.

---

<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:** [August 16, 2020, 11:22am UTC](https://discuss.elastic.co/t/how-to-pass-elastic-apm-traceparent-and-traceparent-headers-explicitly/242640/7 "2020-08-16T11:22:06Z")

</div>

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