diff --git a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java index 5297fe44401..2de3b3f2582 100644 --- a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java +++ b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java @@ -236,7 +236,7 @@ private static void traceHttpRequestExecution2( try (var scope = context.with(span).makeCurrent()) { assert scope != null; // prevent javac warning about scope being unused TraceUtils.setSpan(request, span); - TraceUtils.ifNotNoop( + TraceUtils.ifValidTraceId( span, s -> MDCLoggingContext.setTracerId(s.getSpanContext().getTraceId())); tracedExecution.run(); } catch (ExceptionWhileTracing e) { diff --git a/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java b/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java index 7e9daa37bbc..7edfe8fc55f 100644 --- a/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java +++ b/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java @@ -21,6 +21,7 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.TraceId; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.TextMapPropagator; @@ -108,6 +109,20 @@ public static void ifNotNoop(Span span, Consumer consumer) { } } + /** + * Sometimes the tests will use a recoding noop span to verify the complete code path so we need + * to distinguish this case and only perform a specific operation (like updating the MDC context) + * only in case the generated trace id is valid + * + * @param span current span + * @param consumer consumer to be called + */ + public static void ifValidTraceId(Span span, Consumer consumer) { + if (TraceId.isValid(span.getSpanContext().getTraceId())) { + consumer.accept(span); + } + } + public static void setSpan(HttpServletRequest req, Span span) { req.setAttribute(REQ_ATTR_TRACING_SPAN, span); }