Skip to content

Commit c989438

Browse files
committed
Construct LettuceObservationContext with parent observation
After this commit, LettuceObservationContext.setParentObservation() is called right after Context rather than Observation created, then Context.getParentObservation() could be used in ObservationPredicate to determine whether Observation should be created. Fix spring-projectsGH-2591
1 parent aea748d commit c989438

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/observability/MicrometerTracingAdapter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* arguments will be captured in traces including these that may contain sensitive details.
4545
*
4646
* @author Mark Paluch
47+
* @author Yanming Zhou
4748
* @since 3.0
4849
*/
4950
public class MicrometerTracingAdapter implements Tracing {
@@ -121,24 +122,23 @@ public MicrometerTracer(ObservationRegistry observationRegistry) {
121122

122123
@Override
123124
public Tracer.Span nextSpan() {
124-
return this.postProcessSpan(createObservation());
125+
return this.postProcessSpan(createObservation(null));
125126
}
126127

127128
@Override
128129
public Tracer.Span nextSpan(TraceContext traceContext) {
129-
130-
if (traceContext instanceof MicrometerTraceContext micrometerTraceContext) {
131-
132-
return micrometerTraceContext.observation == null ? nextSpan()
133-
: postProcessSpan(createObservation().parentObservation(micrometerTraceContext.observation()));
134-
}
135-
136-
return nextSpan();
130+
return postProcessSpan(createObservation(traceContext));
137131
}
138132

139-
private Observation createObservation() {
133+
private Observation createObservation(@Nullable TraceContext traceContext) {
140134
return RedisObservation.REDIS_COMMAND_OBSERVATION.observation(observationRegistry,
141-
() -> new LettuceObservationContext(serviceName));
135+
() -> {
136+
LettuceObservationContext context = new LettuceObservationContext(serviceName);
137+
if (traceContext instanceof MicrometerTraceContext micrometerTraceContext && micrometerTraceContext.observation != null) {
138+
context.setParentObservation(micrometerTraceContext.observation);
139+
}
140+
return context;
141+
});
142142
}
143143

144144
private Tracer.Span postProcessSpan(Observation observation) {

0 commit comments

Comments
 (0)