diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-06-14 15:41:08 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-06-14 15:41:08 +0200 |
commit | 2788e1e0530c2d54d513ac460f919405a58657d6 (patch) | |
tree | b174c89bedb94e321e5516d892844e8e6ec79ca4 /container-search | |
parent | 8d8b96b3620f4de369bed60a9c19b6a5fc8b0e95 (diff) |
Turn tracing of the query on/off by trace.query=true/false
Diffstat (limited to 'container-search')
3 files changed, 37 insertions, 18 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/Query.java b/container-search/src/main/java/com/yahoo/search/Query.java index e4f62f83e99..131fe8d151a 100644 --- a/container-search/src/main/java/com/yahoo/search/Query.java +++ b/container-search/src/main/java/com/yahoo/search/Query.java @@ -692,32 +692,23 @@ public class Query extends com.yahoo.processing.Request implements Cloneable { * by an IllegalStateException. In other words, intended use is create the * new query, and attach the context to the invoking query as soon as the new * query is properly initialized. - * * <p> * This method will always set the argument query's context level to the context * level of this query. * - * @param query - * The query which should be traced as a part of this query. - * @throws IllegalStateException - * If the query given as argument already has context - * information. + * @param query the query which should be traced as a part of this query + * @throws IllegalStateException if the query given as argument already has context information */ public void attachContext(Query query) throws IllegalStateException { - query.setTraceLevel(getTraceLevel()); + query.getTrace().setLevel(getTrace().getLevel()); query.setExplainLevel(getExplainLevel()); - if (context == null) { - // Nothing to attach to. This is about the same as - // getTraceLevel() == 0, - // but is a direct test of what will make the function superfluous. - return; - } + if (context == null) return; if (query.getContext(false) != null) { // If we added the other query's context info as a subnode in this // query's context tree, we would have to check for loops in the // context graph. If we simply created a new node without checking, // we might silently overwrite useful information. - throw new IllegalStateException("Query to attach already has context information stored."); + throw new IllegalStateException("Query to attach already has context information stored"); } query.context = context; } diff --git a/container-search/src/main/java/com/yahoo/search/query/Trace.java b/container-search/src/main/java/com/yahoo/search/query/Trace.java index 873cda4415e..024e956e0ef 100644 --- a/container-search/src/main/java/com/yahoo/search/query/Trace.java +++ b/container-search/src/main/java/com/yahoo/search/query/Trace.java @@ -102,7 +102,7 @@ public class Trace implements Cloneable { public void trace(String message, boolean includeQuery, int traceLevel) { if ( ! isTraceable(traceLevel)) return; - if (includeQuery && parent.properties().getBoolean(CompoundName.fromComponents("trace", "query"), true)) + if (includeQuery && query) message += ": [" + queryTreeText() + "]"; log.log(Level.FINE, message); diff --git a/container-search/src/test/java/com/yahoo/search/searchchain/test/TraceTestCase.java b/container-search/src/test/java/com/yahoo/search/searchchain/test/TraceTestCase.java index f89e0d921d7..04b1fd85872 100644 --- a/container-search/src/test/java/com/yahoo/search/searchchain/test/TraceTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/searchchain/test/TraceTestCase.java @@ -48,11 +48,33 @@ public class TraceTestCase { public void testTracingOnIncorrectAPIUseParallel() { assertTracing(false,true); } - + + @Test + public void testTraceWithQuery() { + testQueryInTrace(true, "trace.query=true"); + testQueryInTrace(false, "trace.query=false"); + testQueryInTrace(true, ""); + } + + private void testQueryInTrace(boolean expectQueryInTrace, String queryParameters) { + Query query = new Query("?query=foo&trace.level=1&" + queryParameters); + Chain<Searcher> chain = new Chain<>(new Tracer("tracer1", true)); + Execution execution = new Execution(chain, Execution.Context.createContextStub()); + Result result = execution.search(query); + Iterator<String> trace = collectTrace(query).iterator(); + assertEquals("(level start)", trace.next()); + assertEquals(" No query profile is used", trace.next()); + assertEquals(" (level start)", trace.next()); + if (expectQueryInTrace) + assertEquals(" During tracer1: 0: [WEAKAND(100) foo]", trace.next()); + else + assertEquals(" During tracer1: 0", trace.next()); + } + @Test public void testTraceInvocationsUnfillableHits() { final int traceLevel = 5; - Query query = new Query("?tracelevel=" + traceLevel); + Query query = new Query("?trace.level=" + traceLevel); Chain<Searcher> forkingChain = new Chain<>(new Tracer("tracer1"), new Tracer("tracer2"), new Backend("backend1", false)); @@ -251,17 +273,23 @@ public class TraceTestCase { private static class Tracer extends Searcher { private final String name; + private final boolean traceQuery; private int counter = 0; public Tracer(String name) { + this(name, false); + } + + public Tracer(String name, boolean traceQuery) { super(new ComponentId(name)); this.name = name; + this.traceQuery = traceQuery; } @Override public Result search(Query query, Execution execution) { - query.trace("During " + name + ": " + (counter++),1); + query.trace("During " + name + ": " + (counter++), traceQuery, 1); return execution.search(query); } |