sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-tracer] 19/49: SLING-5459 - Recording of tracer logs
Date Wed, 18 Oct 2017 23:26:03 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tracer.git

commit 71d3afa7e3869875e936b00a8066e369fb4129e6
Author: Chetan Mehrotra <chetanm@apache.org>
AuthorDate: Wed Feb 10 05:53:13 2016 +0000

    SLING-5459 - Recording of tracer logs
    
    Expose the size of recording in web console
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1729533 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/tracer/internal/JSONRecording.java       | 13 ++++++++
 .../sling/tracer/internal/TracerLogServlet.java    | 37 +++++++++++++++++++---
 2 files changed, 46 insertions(+), 4 deletions(-)

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 a7b29d9..592477b 100644
--- a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
+++ b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
@@ -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
diff --git a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
index ae9d1b8..827d890 100644
--- a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
+++ b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
@@ -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 SimpleWebConsolePlugin implements TraceLogRecorde
     }
 
     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 SimpleWebConsolePlugin implements TraceLogRecorde
         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 SimpleWebConsolePlugin implements TraceLogRecorde
         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();
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message