# How to use injectTraceHeaders?

**URL:** <https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451>\
**Category:** APM\
**Tags:** java\
**Created:** [July 9, 2019, 4:30am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451 "2019-07-09T04:30:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [July 9, 2019, 4:30am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451/1 "2019-07-09T04:30:41Z")

</div>

**Kibana version** :6.5.1

**Elasticsearch version** :6.5.1

**APM Server version** :6.5.1

**APM Agent language and version** :java

I custom a span and I see can use 'span.injectTraceHeaders((name, value) -\> request.addHeader(name, value));' to implement RPC framework,but I don't know the name I can set? Another,the operate '-\>' must need java 8？beause when I direct use it，it compile failre,This is my code:  
span.injectTraceHeaders((name, value) -\> RpcContext.getContext().setAttachment(name, value));

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 9, 2019, 4:55am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451/2 "2019-07-09T04:55:01Z")

</div>

Yes that is a Java Lambda Expression which require Java 8+

You can take a look at this sample repo that has a very simple example of how to use the `HeaderInjector` and `HeaderExtractor` framework.

> **[GitHub - bvader/pipelineapmexample: Example of pipeline that uses APM...](https://github.com/bvader/pipelineapmexample)**
>
> Example of pipeline that uses APM pefrormance metrics - GitHub - bvader/pipelineapmexample: Example of pipeline that uses APM pefrormance metrics

You need to implement the

`HeaderInjector`

> **[Public API | APM Java Agent Reference \[1.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-transaction-inject-trace-headers)**

and the `HeaderExtractor`

> **[Public API | APM Java Agent Reference \[1.x\] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-start-transaction-with-remote-parent-header)**

It is up to you how you do that, in the message itself, in the header etc. This is just an example you probably want to do it more thoughtfully for production. This is an example with just raw TCP.

Note the Trace Timeline / Waterfall will probably not look correct in Elasticsearch 6.5.1 as there were fixes in Elasticsearch 7.2 for that visualization.

There is also this thread

> [@Highly Custom Distributed Tracing?](https://discuss.elastic.co/t/highly-custom-distributed-tracing/187206):
>
> So I would love to get distributed tracing working for our application but there is a case here when we do not use the standard infrastructure and move to a websocket. Essentially there are cases when we go `API --\> Websocket (UWS) --\> Node.JS Application --\> API (Localhost) --\> Node.js Worker Cluster (Express routed by PM2) In the worker cluster there will be plenty going on such as db requests and such. The API is just a localhost express app but it only has a single route so I would want t…

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [July 9, 2019, 5:29am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451/3 "2019-07-09T05:29:52Z")

</div>

Thanks,but I just can only use jdk 7.

---

<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 9, 2019, 5:36am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451/4 "2019-07-09T05:36:01Z")

</div>

To add on top of the last comment: you don't need to know the name of the header. The agent only expects you provide it with the ability to add the context- an implementation for the `HeaderInjector` interface that gets a name and a value and knows what to do with them in your specific framework. The agent will invoke this method with the right header name and header value. Same for the `HeaderExtractor`.  
Please take the time to read through @stephenb's examples.

For pre-8 Java, you can use an explicit implementation of the header in a "regular" class implementing it, or anonymous, as such:

```auto
span.injectTraceHeaders(new HeaderInjector() {
    @Override
    public void addHeader(String headerName, String headerValue) {
        RpcContext.getContext().setAttachment(headerName, headerValue));
    }
});

```

---

<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:** [July 30, 2019, 1:36am UTC](https://discuss.elastic.co/t/how-to-use-injecttraceheaders/189451/5 "2019-07-30T01:36:02Z")

</div>

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