Sleuth BaggageField in Spring IntegrationFlow

Viewed 240

How do we get a Spring Cloud Sleuth BaggageField in Spring IntegrationFlow working? Need help with adding it in the IntegrationFlow DSL

Added it in a transform(), however it is not being reflected.

spring.sleuth.baggage.local-fields: customField
spring.sleuth.baggage.correlation-fields: customField


@Bean
public BaggageField customField() {
    return BaggageField.create("customField");
}

@Bean
public ScopeDecorator mdcScopeDecorator() {
    // @formatter:off
    return MDCScopeDecorator.newBuilder()
                            .clear()
                            .add(SingleCorrelationField.newBuilder(customField())
                                                        .flushOnUpdate()
                                                        .build())
                            .build();
    // @formatter:on
}

return IntegrationFlows.from(jdbcMessageSource(), p -> p.poller(pollerSpec()))
                            .transform(message -> {
                                customField.updateValue("ARE");
                                return message;
                            }).split()
                            .channel(c -> c.executor(Executors.newCachedThreadPool()))
                            .transform(transformer, "transform")
                            .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                            .handle(Http.outboundGateway(url)
                                        .httpMethod(HttpMethod.POST)
                                        .expectedResponseType(String.class)
                                        .requestFactory(requestFactory))
                            .channel(httpOutputChannel())
                            .get();

}

1 Answers

I'm not familiar with the feature, but according Spring Cloud Sleuth docs: https://docs.spring.io/spring-cloud-sleuth/docs/current-SNAPSHOT/reference/html/project-features.html#features-baggage, it is just enough to have that customField injected into a service and call its updateValue():

// service
@Autowired
BaggageField countryCodeField;

countryCodeField.updateValue("new-value");

Since any endpoint in the IntegrationFlow is really a service, you should be able to get access to this bean from there and call it, respectively. Perhaps that transformer.transform() could really be used for such a population.

Correct me if I'm missing something from this feature perspective.

Related