Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* arguments will be captured in traces including these that may contain sensitive details.
*
* @author Mark Paluch
* @author Yanming Zhou
* @since 3.0
*/
public class MicrometerTracingAdapter implements Tracing {
Expand Down Expand Up @@ -121,29 +122,28 @@ public MicrometerTracer(ObservationRegistry observationRegistry) {

@Override
public Tracer.Span nextSpan() {
return this.postProcessSpan(createObservation());
return this.postProcessSpan(createObservation(null));
}

@Override
public Tracer.Span nextSpan(TraceContext traceContext) {

if (traceContext instanceof MicrometerTraceContext micrometerTraceContext) {

return micrometerTraceContext.observation == null ? nextSpan()
: postProcessSpan(createObservation().parentObservation(micrometerTraceContext.observation()));
}

return nextSpan();
return postProcessSpan(createObservation(traceContext));
}

private Observation createObservation() {
private Observation createObservation(@Nullable TraceContext traceContext) {
return RedisObservation.REDIS_COMMAND_OBSERVATION.observation(observationRegistry,
() -> new LettuceObservationContext(serviceName));
() -> {
LettuceObservationContext context = new LettuceObservationContext(serviceName);
if (traceContext instanceof MicrometerTraceContext micrometerTraceContext) {
context.setParentObservation(micrometerTraceContext.observation);
}
return context;
});
}

private Tracer.Span postProcessSpan(Observation observation) {

return observation != null && !observation.isNoop()
return !observation.isNoop()
? new MicrometerSpan(observation.observationConvention(observationConvention))
: NoOpSpan.INSTANCE;
}
Expand Down Expand Up @@ -292,6 +292,7 @@ public void finish() {
record MicrometerTraceContextProvider(ObservationRegistry registry) implements TraceContextProvider {

@Override
@Nullable
public TraceContext getTraceContext() {

Observation observation = registry.getCurrentObservation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Collection of tests that log metrics and tracing using the synchronous API.
*
* @author Mark Paluch
* @author Yanming Zhou
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfig.class)
Expand Down Expand Up @@ -77,6 +78,9 @@ public SampleTestRunnerConsumer yourCode() {
.containsEntry("net.sock.peer.port", "" + SettingsUtils.getPort());
assertThat(finishedSpan.getTags()).containsKeys("db.operation");
}

assertThat(TestConfig.PARENT_OBSERVATION_NAMES_COLLECTED_IN_PREDICATE).isNotEmpty();
TestConfig.PARENT_OBSERVATION_NAMES_COLLECTED_IN_PREDICATE.clear();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.micrometer.observation.ObservationRegistry;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
Expand All @@ -33,15 +35,23 @@

/**
* @author Mark Paluch
* @author Yanming Zhou
*/
@Configuration
class TestConfig {

static final MeterRegistry METER_REGISTRY = new SimpleMeterRegistry();
static final ObservationRegistry OBSERVATION_REGISTRY = ObservationRegistry.create();
static final List<String> PARENT_OBSERVATION_NAMES_COLLECTED_IN_PREDICATE = new ArrayList<>();

static {
OBSERVATION_REGISTRY.observationConfig().observationHandler(new DefaultMeterObservationHandler(METER_REGISTRY));
OBSERVATION_REGISTRY.observationConfig().observationPredicate((name, context) -> {
if (context.getParentObservation() != null) {
PARENT_OBSERVATION_NAMES_COLLECTED_IN_PREDICATE.add(context.getParentObservation().getContextView().getName());
}
return true;
});
}

@Bean(destroyMethod = "timer")
Expand Down