Apache Solr open-source search software
SOLR-16960 Tests should sometimes run with a Tracer (not no-op) (#1943)
Alex D85f5c351858d
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex ba8adb622de..24ec5b37e1e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -225,6 +225,8 @@ Other Changes* SOLR-16978: Be case insensitive when parsing booleans from text (Tomás Fernández Löbbe)+* SOLR-16960: Tests should sometimes run with a Tracer (not no-op) (Alex Deparvu)+================== 9.3.0 ==================Upgrade Notesdiff --git a/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.java b/solr/core/src/java/org/apache/solr/util/tracing/TraceUtils.javaindex 4d305f373fd..80fc524556b 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@@ -26,6 +26,7 @@ import io.opentelemetry.context.Context;import io.opentelemetry.context.propagation.TextMapPropagator;import java.util.List;import java.util.function.Consumer;+import java.util.function.Predicate;import javax.servlet.http.HttpServletRequest;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.request.SolrQueryRequest;@@ -64,6 +65,12 @@ public class TraceUtils {public static final String TAG_DB_TYPE_SOLR = "solr";+ /**+ * this should only be changed in the context of testing, otherwise it would risk not recording+ * trace data.+ */+ public static Predicate<Span> IS_RECORDING = Span::isRecording;+public static Tracer getGlobalTracer() {return GlobalOpenTelemetry.getTracer("solr");}@@ -94,7 +101,7 @@ public class TraceUtils {}public static void ifNotNoop(Span span, Consumer<Span> consumer) {- if (span.isRecording()) {+ if (IS_RECORDING.test(span)) {consumer.accept(span);}}diff --git a/solr/test-framework/build.gradle b/solr/test-framework/build.gradleindex 6c772aac512..cb0ddabf7fa 100644--- a/solr/test-framework/build.gradle+++ b/solr/test-framework/build.gradle@@ -58,6 +58,7 @@ dependencies {permitUnusedDeclared 'commons-cli:commons-cli'implementation 'org.apache.httpcomponents:httpclient'implementation 'org.apache.httpcomponents:httpcore'+ implementation 'io.opentelemetry:opentelemetry-api'implementation 'org.eclipse.jetty.toolchain:jetty-servlet-api'implementation 'org.eclipse.jetty:jetty-server'diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.javaindex 3da6dd7a23e..5d7a9e610ec 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java@@ -83,6 +83,7 @@ import org.apache.solr.embedded.JettyConfig;import org.apache.solr.embedded.JettySolrRunner;import org.apache.solr.util.TimeOut;import org.apache.solr.util.tracing.SimplePropagator;+import org.apache.solr.util.tracing.TraceUtils;import org.apache.zookeeper.KeeperException;import org.eclipse.jetty.server.handler.HandlerWrapper;import org.eclipse.jetty.servlet.ServletHolder;@@ -1243,6 +1244,7 @@ public class MiniSolrCloudCluster {// eager init to prevent OTEL init races caused by test setupif (!disableTraceIdGeneration && TracerConfigurator.TRACE_ID_GEN_ENABLED) {SimplePropagator.load();+ injectRandomRecordingFlag();}JettyConfig jettyConfig = jettyConfigBuilder.build();@@ -1299,5 +1301,23 @@ public class MiniSolrCloudCluster {this.disableTraceIdGeneration = true;return this;}++ /**+ * Randomizes the tracing Span::isRecording check.+ *+ * <p>This will randomize the Span::isRecording check so we have better coverage of all methods+ * that deal with span creation without having to enable otel module.+ *+ * <p>It only makes sense to call this if we are using the alwaysOn tracer, the OTEL tracer+ * already has this flag turned on and randomizing it would risk not recording trace data.+ *+ * <p>Note. Tracing is not a SolrCloud only feature. this method is placed here for convenience+ * only, any test can make use of this example for more complete coverage of the tracing+ * mechanics.+ */+ private void injectRandomRecordingFlag() {+ boolean isRecording = LuceneTestCase.rarely();+ TraceUtils.IS_RECORDING = (ignored) -> isRecording;+ }}}