spring-cloud-sleuth icon indicating copy to clipboard operation
spring-cloud-sleuth copied to clipboard

[BUG] StackOverflow when multiple propagation types including CUSTOM

Open bademux opened this issue 1 year ago • 0 comments

Given: spring sleuth 3.1.3 lib

application.properties

spring.sleuth.propagation.type=B3,CUSTOM

and

public class Config {
    @Bean
    public PropagationFactorySupplier myCustomPropagator() {
        return MyCustomPropagator::new;
    }
}

When: starting spring application Then: Stackoverflow exception

Analysis: compositePropagationFactorySupplier attempts to create itself with beanFactory.getBeanProvider(PropagationFactorySupplier.class)

class org.springframework.cloud.sleuth.autoconfig.brave.BraveBridgeConfiguration{
	@Bean
	@ConditionalOnMissingBean
	PropagationFactorySupplier compositePropagationFactorySupplier(BeanFactory beanFactory,
			SleuthBaggageProperties baggageProperties, SleuthPropagationProperties properties) {
		return new CompositePropagationFactorySupplier(beanFactory, baggageProperties.getLocalFields(),
				properties.getType());
	}
}
class org.springframework.cloud.sleuth.brave.bridge.CompositePropagationFactory extends Propagation.Factory implements Propagation<String> {

	CompositePropagationFactory(BeanFactory beanFactory, BraveBaggageManager braveBaggageManager,
			List<String> localFields, List<PropagationType> types) {
...
		LazyPropagationFactory lazyPropagationFactory = new LazyPropagationFactory(
				beanFactory.getBeanProvider(PropagationFactorySupplier.class));
...
	}
}

Proposition: While there can be several solution I suggest to rework CUSTOM Propagation.Factory providers by configuring them as class names, e.g.

spring.sleuth.propagation.type=B3,com.example.MyCustomPropagator

And not passing BeanFactory into the bean :)

TemporaryFix:

public class Config {
    @Bean
    public PropagationFactorySupplier tmplSleuthPropagator() {
        return MyCustomPropagator::new;
    }
    
    /**
     * Fix overriding compositePropagationFactorySupplier creation
     * remove when fixed <a href="https://github.com/spring-cloud/spring-cloud-sleuth/issues/2188">...</a>
     */
    @Bean
    public BaggagePropagation.FactoryBuilder baggagePropagationFactoryBuilder(ApplicationContext beanFactory,
                                                                              SleuthBaggageProperties baggageProperties,
                                                                              SleuthPropagationProperties properties) {
        var supplier = new CompositePropagationFactorySupplier(beanFactory, baggageProperties.getLocalFields(), properties.getType());
        return BaggagePropagation.newFactoryBuilder(supplier.get());
    }
    
}

bademux avatar Jul 22 '22 16:07 bademux