You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2018/01/19 06:26:50 UTC

[GitHub] chetanmeh closed pull request #1: SLING-6506 - Handling of UNION'd queries in Sling Log Tracer

chetanmeh closed pull request #1: SLING-6506 - Handling of UNION'd queries in Sling Log Tracer
URL: https://github.com/apache/sling-org-apache-sling-tracer/pull/1
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
index b7355ca..192cc04 100644
--- a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
+++ b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
@@ -358,6 +358,7 @@ public void toJson(JSONWriter jw) throws IOException {
         String query;
         String plan;
         String caller;
+        int subPlans = 0;
 
         public void record(Level level, String logger, FormattingTuple tuple) {
             //Assuming in a series of log statement from query package we see 'query'
@@ -387,16 +388,44 @@ public void record(Level level, String logger, FormattingTuple tuple) {
                     if ("org.apache.jackrabbit.oak.query.QueryImpl".equals(logger)
                             && msg.startsWith("query plan ")){
                         //logDebug("query execute " + statement);
-                        plan = msg.substring("query plan ".length());
+                        if (subPlans == 0) {
+                            plan = msg.substring("query plan ".length());
+                        } else {
+                            subPlans--;
+                        }
                     } else if ("org.apache.jackrabbit.oak.query.UnionQueryImpl".equals(logger)
                             && msg.contains("query union plan") && args.length > 0){
                         // LOG.debug("query union plan {}", getPlan());
                         plan = nullSafeString(args[0]);
+
+                        // Determine number of sub-queries in this UNION query so they can be ignored
+                        int tmp = count(plan, "*/ union ");
+                        if (tmp > 0) {
+                            subPlans = tmp + 1;
+                        }
                     }
                 }
             }
         }
 
+        /**
+         * Counts the number of instances the needle in the haystack.
+         * @param haystack the string to count the occurrences of the needle in.
+         * @param needle the string to count the number of occurrences of.
+         * @return the number of occurences the needle appears in the haystack.
+         */
+        private int count(final String haystack, final String needle) {
+            int count = 0;
+            int i = 0;
+
+            while ((i = haystack.indexOf(needle, i)) != -1) {
+                i++;
+                count++;
+            }
+
+            return count;
+        }
+
         private String determineCaller() {
             StackTraceElement caller = queryCallerFinder.determineCaller(Thread.currentThread().getStackTrace());
             if (caller != null) {
diff --git a/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java b/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
index 81660aa..f5f1eff 100644
--- a/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
+++ b/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
@@ -36,12 +36,14 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public class JSONRecordingTest {
     static final String MDC_QUERY_ID = "oak.query.id";
     static String QE_LOGGER = "org.apache.jackrabbit.oak.query.QueryImpl";
+    static String UNION_QUERY_LOGGER = "org.apache.jackrabbit.oak.query.UnionQueryImpl";
     private HttpServletRequest request = mock(HttpServletRequest.class);
 
     private TracerConfig tc = new TracerConfig(TracerContext.QUERY_LOGGER, Level.INFO);
@@ -68,6 +70,43 @@ public void logQueries() throws Exception{
         assertEquals(1, json.getJsonArray("queries").size());
     }
 
+    @Test
+    public void logUnionQueries() throws Exception{
+        StringWriter sw = new StringWriter();
+
+        when(request.getMethod()).thenReturn("GET");
+        JSONRecording r = new JSONRecording("abc", request, true);
+
+        MDC.put(MDC_QUERY_ID, "1");
+        r.log(tc, Level.DEBUG, "org.apache.jackrabbit.oak.query.QueryEngineImpl",
+                tuple("Parsing {} statement: {}",  "XPATH", "SELECT FOO BAR"));
+        r.log(tc, Level.DEBUG, UNION_QUERY_LOGGER, tuple("query union plan FOO PLAN */ union BAR PLAN", "FOO PLAN */ union BAR PLAN"));
+        // Two sub-query plans for the split union
+        r.log(tc, Level.DEBUG, QE_LOGGER, tuple("query plan FOO PLAN", "xpath", "FOO PLAN"));
+        r.log(tc, Level.DEBUG, QE_LOGGER, tuple("query plan BAR PLAN", "xpath", "BAR PLAN"));
+
+
+        MDC.put(MDC_QUERY_ID, "2");
+        r.log(tc, Level.DEBUG, "org.apache.jackrabbit.oak.query.QueryEngineImpl",
+                tuple("Parsing {} statement: {}",  "XPATH", "SELECT FOO"));
+        r.log(tc, Level.DEBUG, QE_LOGGER, tuple("query plan FOO PLAN", "xpath", "FOO PLAN"));
+
+        r.done();
+        r.render(sw);
+
+        JsonObject json = Json.createReader(new StringReader(sw.toString())).readObject();
+        assertEquals("GET", json.getString("method"));
+        assertFalse(json.isNull("time"));
+        assertFalse(json.isNull("timestamp"));
+        assertEquals(2, json.getJsonArray("queries").size());
+
+        assertEquals(json.getJsonArray("queries").getJsonObject(0).getString("query"), "SELECT FOO BAR");
+        assertEquals(json.getJsonArray("queries").getJsonObject(0).getString("plan"), "FOO PLAN */ union BAR PLAN");
+
+        assertEquals(json.getJsonArray("queries").getJsonObject(1).getString("query"), "SELECT FOO");
+        assertEquals(json.getJsonArray("queries").getJsonObject(1).getString("plan"), "FOO PLAN");
+    }
+
     @Test
     public void requestTrackerLogs() throws Exception{
         StringWriter sw = new StringWriter();


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services