CoderTR
CoderTR

Reputation: 500

Best way to pass custom headers along with Sleuth X-B3* headers

As per the README here , I am using the following configuration to pass x-vcap-request-id and x-vcap-group-id from one application to the other.

@Bean
public Factory propagationFactory() {
    return brave.propagation.ExtraFieldPropagation.newFactory(brave.propagation.B3Propagation.FACTORY,
            "x-vcap-request-id", "x-vcap-group-id");
}

@Bean
public TracingFactoryBean tracing() {
    TracingFactoryBean tracingFactoryBean = new TracingFactoryBean();
    tracingFactoryBean.setPropagationFactory(propagationFactory());
    return tracingFactoryBean;
}

However, this configuration is messing up default sleuth behavior. With this code, Sleuth no longer adds TraceId and SpanId to the log

What's the best/recommended way to pass along custom headers between microservices ?

Upvotes: 3

Views: 12005

Answers (3)

Krzysztof Tomaszewski
Krzysztof Tomaszewski

Reputation: 1154

With latest Spring Cloud Sleuth you need just to set properties:

spring.sleuth.baggage.remote-fields=my-header,other-header
spring.sleuth.baggage.correlation-fields=my-header,other-header

Maybe even only one of them needed. Anyway it will not harm to set both properies to the same value: a list of HTTP header names to be propagated. This, when using RestTemplate, WebClient or Feign client, will cause adding listed headers to the outgoing HTTP request when the incoming HTTP request has these headers. Please note that HTTP client must be a Spring bean so Spring Cloud Sleuth can instrument it.

Upvotes: 0

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11169

If you read the docs on prefixed fields you'll see the following section

A difference from previous versions of Sleuth is that, with Brave, you must pass the list of baggage keys. There are two properties to achieve this. With the spring.sleuth.baggage-keys, you set keys that get prefixed with baggage- for HTTP calls and baggage_ for messaging. You can also use the spring.sleuth.propagation-keys property to pass a list of prefixed keys that are whitelisted without any prefix.

Just set the properties and things will work out of the box. It's always good to read the project documentation.

Upvotes: 4

Gajendra Singh Rao
Gajendra Singh Rao

Reputation: 11

I haven't used TracingFactoryBean, but adding Slf4jCurrentTraceContext in TracingBuilder solved it for me.

return Tracing.newBuilder().propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "request-id"))
        .currentTraceContext(Slf4jCurrentTraceContext.create())
        .build();

Upvotes: 1

Related Questions