Ensures that sampler proxy gets eagerly resolved; fixes gh-2107
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.autoconfig.instrument.redis;
|
||||
|
||||
import brave.sampler.Sampler;
|
||||
import io.lettuce.core.tracing.Tracing;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
@@ -51,10 +52,22 @@ public class TraceRedisAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(Tracing.class)
|
||||
TraceLettuceClientResourcesBuilderCustomizer traceLettuceClientResourcesBuilderCustomizer(Tracing tracing) {
|
||||
TraceLettuceClientResourcesBuilderCustomizer traceLettuceClientResourcesBuilderCustomizer(Tracing tracing,
|
||||
Sampler sampler) {
|
||||
eagerlyInitializePotentiallyRefreshScopeSampler(sampler);
|
||||
return new TraceLettuceClientResourcesBuilderCustomizer(tracing);
|
||||
}
|
||||
|
||||
/**
|
||||
* We need to do the eager method invocation. Since this might be @RefreshScope, a
|
||||
* proxy is being created. Trying to resolve the proxy from a different thread
|
||||
* than main can lead to cross thread locking.
|
||||
* @param sampler potentially refresh scope sampler
|
||||
*/
|
||||
private void eagerlyInitializePotentiallyRefreshScopeSampler(Sampler sampler) {
|
||||
sampler.isSampled(0L);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -151,6 +151,19 @@ class TraceFunctionAroundWrapperTests {
|
||||
assertThat(tracer.spans).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_tracing_with_message_flux_to_flux_without_message() {
|
||||
FunctionRegistration<MessageFluxToFluxFunction> registration = new FunctionRegistration<>(
|
||||
new MessageFluxToFluxFunction(), "greeter").type(FunctionType.of(MessageFluxToFluxFunction.class));
|
||||
catalog.register(registration);
|
||||
FunctionInvocationWrapper function = catalog.lookup("greeter");
|
||||
|
||||
Object result = (Object) ((Flux) wrapper.apply("hello", function)).blockFirst();
|
||||
|
||||
assertThat(result).isEqualTo("hello");
|
||||
assertThat(tracer.spans).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_tracing_with_consumer() {
|
||||
GreeterConsumer consumer = new GreeterConsumer();
|
||||
@@ -465,4 +478,18 @@ class TraceFunctionAroundWrapperTests {
|
||||
|
||||
}
|
||||
|
||||
static class MessageFluxToFluxFunction implements Function<Flux<Message<?>>, Flux<?>> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MessageFluxToFluxFunction.class);
|
||||
|
||||
@Override
|
||||
public Flux<?> apply(Flux<Message<?>> input) {
|
||||
return input.map(s -> {
|
||||
log.info("Logging [{}] from map", s);
|
||||
return s.getPayload();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user