Search in sources :

Example 1 with RatioGauge

use of com.codahale.metrics.RatioGauge in project metrics by dropwizard.

the class MemoryUsageGaugeSet method getMetrics.

@Override
public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<String, Metric>();
    gauges.put("total.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getInit() + mxBean.getNonHeapMemoryUsage().getInit();
        }
    });
    gauges.put("total.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed() + mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("total.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax() + mxBean.getNonHeapMemoryUsage().getMax();
        }
    });
    gauges.put("total.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted() + mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("heap.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getInit();
        }
    });
    gauges.put("heap.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("heap.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getMax();
        }
    });
    gauges.put("heap.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    gauges.put("non-heap.init", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getInit();
        }
    });
    gauges.put("non-heap.used", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getUsed();
        }
    });
    gauges.put("non-heap.max", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getMax();
        }
    });
    gauges.put("non-heap.committed", new Gauge<Long>() {

        @Override
        public Long getValue() {
            return mxBean.getNonHeapMemoryUsage().getCommitted();
        }
    });
    gauges.put("non-heap.usage", new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
            return Ratio.of(usage.getUsed(), usage.getMax());
        }
    });
    for (final MemoryPoolMXBean pool : memoryPools) {
        final String poolName = name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("-"));
        gauges.put(name(poolName, "usage"), new RatioGauge() {

            @Override
            protected Ratio getRatio() {
                MemoryUsage usage = pool.getUsage();
                return Ratio.of(usage.getUsed(), usage.getMax() == -1 ? usage.getCommitted() : usage.getMax());
            }
        });
        gauges.put(name(poolName, "max"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getMax();
            }
        });
        gauges.put(name(poolName, "used"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getUsed();
            }
        });
        gauges.put(name(poolName, "committed"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getCommitted();
            }
        });
        // Only register GC usage metrics if the memory pool supports usage statistics.
        if (pool.getCollectionUsage() != null) {
            gauges.put(name(poolName, "used-after-gc"), new Gauge<Long>() {

                @Override
                public Long getValue() {
                    return pool.getCollectionUsage().getUsed();
                }
            });
        }
        gauges.put(name(poolName, "init"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return pool.getUsage().getInit();
            }
        });
    }
    return Collections.unmodifiableMap(gauges);
}
Also used : RatioGauge(com.codahale.metrics.RatioGauge) Metric(com.codahale.metrics.Metric) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 2 with RatioGauge

use of com.codahale.metrics.RatioGauge in project metrics by dropwizard.

the class InstrumentedHandler method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final String prefix = this.prefix == null ? name(getHandler().getClass(), name) : name(this.prefix, name);
    this.requests = metricRegistry.timer(name(prefix, "requests"));
    this.dispatches = metricRegistry.timer(name(prefix, "dispatches"));
    this.activeRequests = metricRegistry.counter(name(prefix, "active-requests"));
    this.activeDispatches = metricRegistry.counter(name(prefix, "active-dispatches"));
    this.activeSuspended = metricRegistry.counter(name(prefix, "active-suspended"));
    this.asyncDispatches = metricRegistry.meter(name(prefix, "async-dispatches"));
    this.asyncTimeouts = metricRegistry.meter(name(prefix, "async-timeouts"));
    this.responses = new Meter[] { // 1xx
    metricRegistry.meter(name(prefix, "1xx-responses")), // 2xx
    metricRegistry.meter(name(prefix, "2xx-responses")), // 3xx
    metricRegistry.meter(name(prefix, "3xx-responses")), // 4xx
    metricRegistry.meter(name(prefix, "4xx-responses")), // 5xx
    metricRegistry.meter(name(prefix, "5xx-responses")) };
    this.getRequests = metricRegistry.timer(name(prefix, "get-requests"));
    this.postRequests = metricRegistry.timer(name(prefix, "post-requests"));
    this.headRequests = metricRegistry.timer(name(prefix, "head-requests"));
    this.putRequests = metricRegistry.timer(name(prefix, "put-requests"));
    this.deleteRequests = metricRegistry.timer(name(prefix, "delete-requests"));
    this.optionsRequests = metricRegistry.timer(name(prefix, "options-requests"));
    this.traceRequests = metricRegistry.timer(name(prefix, "trace-requests"));
    this.connectRequests = metricRegistry.timer(name(prefix, "connect-requests"));
    this.moveRequests = metricRegistry.timer(name(prefix, "move-requests"));
    this.otherRequests = metricRegistry.timer(name(prefix, "other-requests"));
    metricRegistry.register(name(prefix, "percent-4xx-1m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getOneMinuteRate(), requests.getOneMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-4xx-5m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getFiveMinuteRate(), requests.getFiveMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-4xx-15m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[3].getFifteenMinuteRate(), requests.getFifteenMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-1m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getOneMinuteRate(), requests.getOneMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-5m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getFiveMinuteRate(), requests.getFiveMinuteRate());
        }
    });
    metricRegistry.register(name(prefix, "percent-5xx-15m"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(responses[4].getFifteenMinuteRate(), requests.getFifteenMinuteRate());
        }
    });
    this.listener = new AsyncListener() {

        private long startTime;

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            asyncTimeouts.mark();
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            startTime = System.currentTimeMillis();
            event.getAsyncContext().addListener(this);
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            final AsyncContextState state = (AsyncContextState) event.getAsyncContext();
            final HttpServletRequest request = (HttpServletRequest) state.getRequest();
            final HttpServletResponse response = (HttpServletResponse) state.getResponse();
            updateResponses(request, response, startTime);
            if (state.getHttpChannelState().getState() != HttpChannelState.State.DISPATCHED) {
                activeSuspended.dec();
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncContextState(org.eclipse.jetty.server.AsyncContextState) RatioGauge(com.codahale.metrics.RatioGauge) AsyncListener(javax.servlet.AsyncListener) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Example 3 with RatioGauge

use of com.codahale.metrics.RatioGauge in project metrics by dropwizard.

the class InstrumentedQueuedThreadPool method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    final String prefix = this.prefix == null ? name(QueuedThreadPool.class, getName()) : name(this.prefix, getName());
    metricRegistry.register(name(prefix, "utilization"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(getThreads() - getIdleThreads(), getThreads());
        }
    });
    metricRegistry.register(name(prefix, "utilization-max"), new RatioGauge() {

        @Override
        protected Ratio getRatio() {
            return Ratio.of(getThreads() - getIdleThreads(), getMaxThreads());
        }
    });
    metricRegistry.register(name(prefix, "size"), new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return getThreads();
        }
    });
    metricRegistry.register(name(prefix, "jobs"), new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            // ArrayBlockingQueue for its queue, and is therefore a constant-time operation.
            return getQueue().size();
        }
    });
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RatioGauge(com.codahale.metrics.RatioGauge)

Example 4 with RatioGauge

use of com.codahale.metrics.RatioGauge in project hbase by apache.

the class TestMetricsConnection method testStaticMetrics.

@Test
public void testStaticMetrics() throws IOException {
    final byte[] foo = Bytes.toBytes("foo");
    final RegionSpecifier region = RegionSpecifier.newBuilder().setValue(ByteString.EMPTY).setType(RegionSpecifierType.REGION_NAME).build();
    final int loop = 5;
    for (int i = 0; i < loop; i++) {
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Get"), GetRequest.getDefaultInstance(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Scan"), ScanRequest.getDefaultInstance(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Multi"), MultiRequest.getDefaultInstance(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Mutate"), MutateRequest.newBuilder().setMutation(ProtobufUtil.toMutation(MutationType.APPEND, new Append(foo))).setRegion(region).build(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Mutate"), MutateRequest.newBuilder().setMutation(ProtobufUtil.toMutation(MutationType.DELETE, new Delete(foo))).setRegion(region).build(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Mutate"), MutateRequest.newBuilder().setMutation(ProtobufUtil.toMutation(MutationType.INCREMENT, new Increment(foo))).setRegion(region).build(), MetricsConnection.newCallStats());
        METRICS.updateRpc(ClientService.getDescriptor().findMethodByName("Mutate"), MutateRequest.newBuilder().setMutation(ProtobufUtil.toMutation(MutationType.PUT, new Put(foo))).setRegion(region).build(), MetricsConnection.newCallStats());
    }
    for (MetricsConnection.CallTracker t : new MetricsConnection.CallTracker[] { METRICS.getTracker, METRICS.scanTracker, METRICS.multiTracker, METRICS.appendTracker, METRICS.deleteTracker, METRICS.incrementTracker, METRICS.putTracker }) {
        assertEquals("Failed to invoke callTimer on " + t, loop, t.callTimer.getCount());
        assertEquals("Failed to invoke reqHist on " + t, loop, t.reqHist.getCount());
        assertEquals("Failed to invoke respHist on " + t, loop, t.respHist.getCount());
    }
    RatioGauge executorMetrics = (RatioGauge) METRICS.getMetricRegistry().getMetrics().get(METRICS.getExecutorPoolName());
    RatioGauge metaMetrics = (RatioGauge) METRICS.getMetricRegistry().getMetrics().get(METRICS.getMetaPoolName());
    assertEquals(Ratio.of(0, 3).getValue(), executorMetrics.getValue(), 0);
    assertEquals(Double.NaN, metaMetrics.getValue(), 0);
}
Also used : RatioGauge(com.codahale.metrics.RatioGauge) RegionSpecifier(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier) Test(org.junit.Test)

Aggregations

RatioGauge (com.codahale.metrics.RatioGauge)4 Metric (com.codahale.metrics.Metric)1 IOException (java.io.IOException)1 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)1 MemoryUsage (java.lang.management.MemoryUsage)1 AsyncEvent (javax.servlet.AsyncEvent)1 AsyncListener (javax.servlet.AsyncListener)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RegionSpecifier (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier)1 AsyncContextState (org.eclipse.jetty.server.AsyncContextState)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1 Test (org.junit.Test)1