Skip to content

Commit

Permalink
SOLR-16960 Tests should sometimes run with a Tracer (not no-op) (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
stillalex authored Oct 2, 2023
1 parent 0bdf356 commit bd09cb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,20 @@ public static void ifNotNoop(Span span, Consumer<Span> 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<Span> 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);
}
Expand Down

0 comments on commit bd09cb5

Please sign in to comment.