How to use injectTraceHeaders?

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));

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.

You need to implement the

HeaderInjector

and the HeaderExtractor

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

1 Like

Thanks,but I just can only use jdk 7.

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:

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

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