Add status/memory endpoint to get heap usage, max heap, and free heap.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/02352dff Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/02352dff Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/02352dff Branch: refs/heads/asf-site Commit: 02352dff0ba164dfcf2789457c0c65e0deee8771 Parents: a8ba65f Author: Mike Dunker <mdun...@apigee.com> Authored: Thu Apr 21 10:08:25 2016 -0700 Committer: Mike Dunker <mdun...@apigee.com> Committed: Thu Apr 21 10:08:25 2016 -0700 ---------------------------------------------------------------------- .../org/apache/usergrid/utils/StringUtils.java | 8 +++++++ .../org/apache/usergrid/rest/RootResource.java | 23 ++++++++++++++++++++ 2 files changed, 31 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/02352dff/stack/core/src/main/java/org/apache/usergrid/utils/StringUtils.java ---------------------------------------------------------------------- diff --git a/stack/core/src/main/java/org/apache/usergrid/utils/StringUtils.java b/stack/core/src/main/java/org/apache/usergrid/utils/StringUtils.java index b91b866..6bb44d8 100644 --- a/stack/core/src/main/java/org/apache/usergrid/utils/StringUtils.java +++ b/stack/core/src/main/java/org/apache/usergrid/utils/StringUtils.java @@ -17,6 +17,7 @@ package org.apache.usergrid.utils; +import java.text.DecimalFormat; import java.util.Arrays; import org.apache.commons.io.IOUtils; @@ -169,4 +170,11 @@ public class StringUtils extends org.apache.commons.lang.StringUtils { } return null; } + + public static String readableByteSize(long size) { + if(size <= 0) return "0"; + final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" }; + int digitGroups = (int) (Math.log10(size)/Math.log10(1024)); + return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups]; + } } http://git-wip-us.apache.org/repos/asf/usergrid/blob/02352dff/stack/rest/src/main/java/org/apache/usergrid/rest/RootResource.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/RootResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/RootResource.java index 75ed567..9701105 100644 --- a/stack/rest/src/main/java/org/apache/usergrid/rest/RootResource.java +++ b/stack/rest/src/main/java/org/apache/usergrid/rest/RootResource.java @@ -248,6 +248,29 @@ public class RootResource extends AbstractContextResource implements MetricProce } + @GET + @Path("/status/memory") + @JSONP + @Produces({MediaType.APPLICATION_JSON, "application/javascript"}) + public ApiResponse getMemoryStats(){ + + ApiResponse response = createApiResponse(); + + ObjectNode node = JsonNodeFactory.instance.objectNode(); + + long heapSize = Runtime.getRuntime().totalMemory(); + long heapMaxSize = Runtime.getRuntime().maxMemory(); + long heapFreeSize = Runtime.getRuntime().freeMemory(); + + node.put( "currentHeap", org.apache.usergrid.utils.StringUtils.readableByteSize(heapSize) ); + node.put( "maxHeap", org.apache.usergrid.utils.StringUtils.readableByteSize(heapMaxSize) ); + node.put( "freeHeap", org.apache.usergrid.utils.StringUtils.readableByteSize(heapFreeSize) ); + + response.setProperty( "status", node ); + return response; + + } + private void dumpMetrics( ObjectNode node ) {