Ensures that sampler proxy gets eagerly resolved; fixes gh-2107

This commit is contained in:
Marcin Grzejszczak
2022-02-02 09:57:50 +01:00
parent 2f2ced8593
commit 2b9b76e5dc
2 changed files with 41 additions and 1 deletions

View File

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

View File

@@ -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();
});
}
}
}