This is an automated email from the ASF dual-hosted git repository.
stillalex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new bd09cb5ac59 SOLR-16960 Tests should sometimes run with a Tracer (not
no-op) (#1972)
bd09cb5ac59 is described below
commit bd09cb5ac59b76a3318fd543d1dcfbaa8fb3977c
Author: Alex D <[email protected]>
AuthorDate: Mon Oct 2 11:02:49 2023 -0700
SOLR-16960 Tests should sometimes run with a Tracer (not no-op) (#1972)
---
.../src/java/org/apache/solr/servlet/ServletUtils.java | 2 +-
.../src/java/org/apache/solr/util/tracing/TraceUtils.java | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
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 @@ public abstract class ServletUtils {
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.common.AttributeKey;
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 class TraceUtils {
}
}
+ /**
+ * 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);
}