sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1729353 - in /sling/trunk/contrib/extensions/tracer/src: main/java/org/apache/sling/tracer/internal/ test/java/org/apache/sling/tracer/internal/
Date Tue, 09 Feb 2016 10:39:37 GMT
Author: chetanm
Date: Tue Feb  9 10:39:35 2016
New Revision: 1729353

URL: http://svn.apache.org/viewvc?rev=1729353&view=rev
Log:
SLING-5459 - Recording of tracer logs

Changed the approach to serialize the whole recording as json at the end of the request processing.
This would help in conserving memory usage.  Also  then we can size the recording and enforce
limit on size, keep json in compressed state etc

Modified:
    sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
    sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
    sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/Recording.java
    sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
    sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
    sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
    sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java

Modified: sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
Tue Feb  9 10:39:35 2016
@@ -19,6 +19,8 @@
 
 package org.apache.sling.tracer.internal;
 
+import java.io.IOException;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -29,18 +31,64 @@ import javax.servlet.http.HttpServletReq
 import org.apache.sling.api.request.RequestProgressTracker;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.io.JSONWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 class JSONRecording implements Recording {
+    private static final Logger log = LoggerFactory.getLogger(JSONRecording.class);
     private final String method;
+    private final String requestId;
     private final List<String> queries = new ArrayList<String>();
     private RequestProgressTracker tracker;
+    private String json;
 
-    public JSONRecording(HttpServletRequest r) {
+    public JSONRecording(String requestId, HttpServletRequest r) {
+        this.requestId = requestId;
         this.method = r.getMethod();
     }
 
-    public void render(Writer pw) throws JSONException {
-        JSONWriter jw = new JSONWriter(pw);
+    public boolean render(Writer pw) throws IOException {
+        if (json != null) {
+            pw.write(json);
+            return true;
+        }
+        return false;
+    }
+
+    //~---------------------------------------< Recording >
+
+    @Override
+    public void log(String logger, String format, Object[] params) {
+        if (TracerContext.QUERY_LOGGER.equals(logger)
+                && params != null && params.length == 2) {
+            queries.add((String) params[1]);
+        }
+    }
+
+    @Override
+    public void registerTracker(RequestProgressTracker tracker) {
+        this.tracker = tracker;
+    }
+
+    @Override
+    public void done() {
+        try {
+            if (json == null) {
+                json = toJSON();
+
+                //Let the tracker and other references go to
+                //not occupy memory
+                tracker = null;
+                queries.clear();
+            }
+        } catch (JSONException e) {
+            log.warn("Error occurred while converting the log data for request {} to JSON",
requestId, e);
+        }
+    }
+
+    private String toJSON() throws JSONException {
+        StringWriter sw = new StringWriter();
+        JSONWriter jw = new JSONWriter(sw);
         jw.setTidy(true);
         jw.object();
         jw.key("method").value(method);
@@ -63,24 +111,6 @@ class JSONRecording implements Recording
         }
         jw.endArray();
         jw.endObject();
-    }
-
-    //~---------------------------------------< Recording >
-
-    @Override
-    public void log(String logger, String format, Object[] params) {
-        if (TracerContext.QUERY_LOGGER.equals(logger)
-                && params != null && params.length == 2) {
-            queries.add((String) params[1]);
-        }
-    }
-
-    @Override
-    public void registerTracker(RequestProgressTracker tracker) {
-        this.tracker = tracker;
-    }
-
-    RequestProgressTracker getTracker() {
-        return tracker;
+        return sw.toString();
     }
 }

Modified: sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/LogTracer.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/LogTracer.java
Tue Feb  9 10:39:35 2016
@@ -318,6 +318,7 @@ public class LogTracer {
                 if (tracerContext != null) {
                     disableCollector();
                 }
+                recording.done();
             }
         }
 

Modified: sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/Recording.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/Recording.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/Recording.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/Recording.java
Tue Feb  9 10:39:35 2016
@@ -32,6 +32,11 @@ interface Recording {
         public void registerTracker(RequestProgressTracker tracker) {
 
         }
+
+        @Override
+        public void done() {
+
+        }
     };
 
     void log(String logger, String format, Object[] params);
@@ -42,4 +47,9 @@ interface Recording {
      * @param tracker from current request
      */
     void registerTracker(RequestProgressTracker tracker);
+
+    /**
+     * Invoked at the end of request processing
+     */
+    void done();
 }

Modified: sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
Tue Feb  9 10:39:35 2016
@@ -79,8 +79,7 @@ class TracerLogServlet extends SimpleWeb
                 if (requestId != null) {
                     JSONRecording recording = cache.getIfPresent(requestId);
                     if (recording != null){
-                        recording.render(pw);
-                        responseDone = true;
+                        responseDone = recording.render(pw);
                     }
                 }
 
@@ -184,7 +183,7 @@ class TracerLogServlet extends SimpleWeb
     }
 
     private JSONRecording record(String requestId, HttpServletRequest request) {
-        JSONRecording data = new JSONRecording(request);
+        JSONRecording data = new JSONRecording(requestId, request);
         cache.put(requestId, data);
         return data;
     }

Modified: sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
Tue Feb  9 10:39:35 2016
@@ -38,11 +38,12 @@ public class JSONRecordingTest {
         StringWriter sw = new StringWriter();
 
         when(request.getMethod()).thenReturn("GET");
-        JSONRecording r = new JSONRecording(request);
+        JSONRecording r = new JSONRecording("abc", request);
 
         r.log(TracerContext.QUERY_LOGGER, "foo bar", new Object[]{"x" , "y"});
         r.log(TracerContext.QUERY_LOGGER, "foo bar", new Object[]{"x" , "z"});
 
+        r.done();
         r.render(sw);
 
         JSONObject json = new JSONObject(sw.toString());
@@ -53,10 +54,11 @@ public class JSONRecordingTest {
     @Test
     public void requestTrackerLogs() throws Exception{
         StringWriter sw = new StringWriter();
-        JSONRecording r = new JSONRecording(request);
+        JSONRecording r = new JSONRecording("abc", request);
 
         r.registerTracker(TestUtil.createTracker("x", "y"));
 
+        r.done();
         r.render(sw);
 
         JSONObject json = new JSONObject(sw.toString());

Modified: sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/LogTracerTest.java
Tue Feb  9 10:39:35 2016
@@ -20,6 +20,7 @@
 package org.apache.sling.tracer.internal;
 
 import java.io.IOException;
+import java.io.StringWriter;
 import java.util.Collection;
 import java.util.List;
 
@@ -41,6 +42,7 @@ import com.google.common.collect.Immutab
 import com.google.common.collect.Lists;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.request.RequestProgressTracker;
+import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.testing.mock.osgi.MockOsgi;
 import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
 import org.apache.sling.testing.mock.osgi.junit.OsgiContextCallback;
@@ -253,7 +255,13 @@ public class LogTracerTest {
         assertNotNull(requestId);
         Recording r = ((TracerLogServlet)context.getService(Servlet.class)).getRecording(requestId);
         assertTrue(r instanceof JSONRecording);
-        assertNotNull(((JSONRecording)r).getTracker());
+        JSONRecording jr = (JSONRecording) r;
+
+        StringWriter sw = new StringWriter();
+        jr.render(sw);
+        JSONObject json = new JSONObject(sw.toString());
+
+        assertEquals(2, json.getJSONArray("logs").length());
     }
 
 

Modified: sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java?rev=1729353&r1=1729352&r2=1729353&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
(original)
+++ sling/trunk/contrib/extensions/tracer/src/test/java/org/apache/sling/tracer/internal/TracerLogServletTest.java
Tue Feb  9 10:39:35 2016
@@ -95,6 +95,7 @@ public class TracerLogServletTest {
 
         Recording recording = logServlet.startRecording(request, response);
         recording.registerTracker(createTracker("x" ,"y"));
+        recording.done();
 
         ArgumentCaptor<String> requestIdCaptor = ArgumentCaptor.forClass(String.class);
         verify(response).setHeader(eq(TracerLogServlet.HEADER_TRACER_REQUEST_ID), requestIdCaptor.capture());



Mime
View raw message