sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chet...@apache.org
Subject svn commit: r1729533 - in /sling/trunk/contrib/extensions/tracer/src/main/java/org/apache/sling/tracer/internal: JSONRecording.java TracerLogServlet.java
Date Wed, 10 Feb 2016 05:53:13 GMT
Author: chetanm
Date: Wed Feb 10 05:53:13 2016
New Revision: 1729533

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

Expose the size of recording in web console

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/TracerLogServlet.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=1729533&r1=1729532&r2=1729533&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
Wed Feb 10 05:53:13 2016
@@ -50,6 +50,7 @@ class JSONRecording implements Recording
     private static final Logger log = LoggerFactory.getLogger(JSONRecording.class);
     private final String method;
     private final String requestId;
+    private final String uri;
     private final List<String> queries = new ArrayList<String>();
     private final List<LogEntry> logs = new ArrayList<LogEntry>();
     private RequestProgressTracker tracker;
@@ -58,6 +59,7 @@ class JSONRecording implements Recording
     public JSONRecording(String requestId, HttpServletRequest r) {
         this.requestId = requestId;
         this.method = r.getMethod();
+        this.uri = r.getRequestURI();
     }
 
     public boolean render(Writer w) throws IOException {
@@ -77,6 +79,17 @@ class JSONRecording implements Recording
         return false;
     }
 
+    public long size() {
+        if (json != null){
+            return json.length;
+        }
+        return 0;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
     //~---------------------------------------< Recording >
 
     @Override

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=1729533&r1=1729532&r2=1729533&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
Wed Feb 10 05:53:13 2016
@@ -21,6 +21,7 @@ package org.apache.sling.tracer.internal
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
@@ -116,7 +117,8 @@ class TracerLogServlet extends SimpleWeb
     }
 
     private void renderStatus(PrintWriter pw) {
-        pw.printf("<p class='statline'>Log Tracer Recordings: %d recordings</p>%n",
cache.size());
+        pw.printf("<p class='statline'>Log Tracer Recordings: %d recordings, %s memory</p>%n",
cache.size(),
+                memorySize());
 
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
         pw.println("<span style='float: left; margin-left: 1em'>Tracer Recordings</span>");
@@ -124,15 +126,25 @@ class TracerLogServlet extends SimpleWeb
         pw.println("</div>");
     }
 
+    private String memorySize() {
+        long size = 0;
+        for (JSONRecording r : cache.asMap().values()){
+            size += r.size();
+        }
+        return humanReadableByteCount(size);
+    }
+
     private void renderRequests(PrintWriter pw) {
         if (cache.size() > 0){
             pw.println("<ul>");
-            for (String id : cache.asMap().keySet()){
-                pw.printf("<li><a href='%s/%s.json'>%s</a></li>",
LABEL, id, id);
+            for (Map.Entry<String, JSONRecording> e : cache.asMap().entrySet()){
+                String id = e.getKey();
+                JSONRecording r = e.getValue();
+                pw.printf("<li><a href='%s/%s.json'>%s</a> - %s (%s)</li>",
LABEL, id, id, r.getUri(),
+                        humanReadableByteCount(r.size()));
             }
             pw.println("</ul>");
         }
-
     }
 
     private static String getRequestId(HttpServletRequest request) {
@@ -193,6 +205,23 @@ class TracerLogServlet extends SimpleWeb
         return UUID.randomUUID().toString();
     }
 
+    /**
+     * Returns a human-readable version of the file size, where the input represents
+     * a specific number of bytes. Based on http://stackoverflow.com/a/3758880/1035417
+     */
+    private static String humanReadableByteCount(long bytes) {
+        if (bytes < 0) {
+            return "0";
+        }
+        int unit = 1000;
+        if (bytes < unit) {
+            return bytes + " B";
+        }
+        int exp = (int) (Math.log(bytes) / Math.log(unit));
+        char pre = "kMGTPE".charAt(exp - 1);
+        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
+    }
+
     void resetCache(){
         cache.invalidateAll();
     }



Mime
View raw message