Search in sources :

Example 41 with Counter

use of com.codahale.metrics.Counter in project spring-boot by spring-projects.

the class DropwizardMetricServices method incrementInternal.

private void incrementInternal(String name, long value) {
    if (name.startsWith("meter")) {
        Meter meter = this.registry.meter(name);
        meter.mark(value);
    } else {
        name = wrapCounterName(name);
        Counter counter = this.registry.counter(name);
        counter.inc(value);
    }
}
Also used : Counter(com.codahale.metrics.Counter) Meter(com.codahale.metrics.Meter)

Example 42 with Counter

use of com.codahale.metrics.Counter in project spring-boot by spring-projects.

the class MetricRegistryMetricReader method findOne.

@Override
public Metric<?> findOne(String metricName) {
    String name = this.names.get(metricName);
    if (name == null) {
        return null;
    }
    com.codahale.metrics.Metric metric = this.registry.getMetrics().get(name);
    if (metric == null) {
        return null;
    }
    if (metric instanceof Counter) {
        Counter counter = (Counter) metric;
        return new Metric<Number>(metricName, counter.getCount());
    }
    if (metric instanceof Gauge) {
        Object value = ((Gauge<?>) metric).getValue();
        if (value instanceof Number) {
            return new Metric<>(metricName, (Number) value);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Ignoring gauge '" + name + "' (" + metric + ") as its value is not a Number");
        }
        return null;
    }
    if (metric instanceof Sampling) {
        if (metricName.contains(".snapshot.")) {
            Number value = getMetric(((Sampling) metric).getSnapshot(), metricName);
            if (metric instanceof Timer) {
                // convert back to MILLISEC
                value = TimeUnit.MILLISECONDS.convert(value.longValue(), TimeUnit.NANOSECONDS);
            }
            return new Metric<>(metricName, value);
        }
    }
    return new Metric<>(metricName, getMetric(metric, metricName));
}
Also used : Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Metric(org.springframework.boot.actuate.metrics.Metric) Sampling(com.codahale.metrics.Sampling) Gauge(com.codahale.metrics.Gauge)

Example 43 with Counter

use of com.codahale.metrics.Counter in project alluxio by Alluxio.

the class WebInterfaceMasterMetricsServlet method populateValues.

/**
   * Populates key, value pairs for UI display.
   *
   * @param request The {@link HttpServletRequest} object
   * @throws IOException if an I/O error occurs
   */
private void populateValues(HttpServletRequest request) throws IOException {
    MetricRegistry mr = MetricsSystem.METRIC_REGISTRY;
    Long masterCapacityTotal = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(BlockMaster.Metrics.CAPACITY_TOTAL)).getValue();
    Long masterCapacityUsed = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(BlockMaster.Metrics.CAPACITY_USED)).getValue();
    int masterCapacityUsedPercentage = (masterCapacityTotal > 0) ? (int) (100L * masterCapacityUsed / masterCapacityTotal) : 0;
    request.setAttribute("masterCapacityUsedPercentage", masterCapacityUsedPercentage);
    request.setAttribute("masterCapacityFreePercentage", 100 - masterCapacityUsedPercentage);
    Long masterUnderfsCapacityTotal = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.UFS_CAPACITY_TOTAL)).getValue();
    Long masterUnderfsCapacityUsed = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.UFS_CAPACITY_USED)).getValue();
    int masterUnderfsCapacityUsedPercentage = (masterUnderfsCapacityTotal > 0) ? (int) (100L * masterUnderfsCapacityUsed / masterUnderfsCapacityTotal) : 0;
    request.setAttribute("masterUnderfsCapacityUsedPercentage", masterUnderfsCapacityUsedPercentage);
    request.setAttribute("masterUnderfsCapacityFreePercentage", 100 - masterUnderfsCapacityUsedPercentage);
    Map<String, Counter> counters = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return !(name.endsWith("Ops"));
        }
    });
    Map<String, Counter> rpcInvocations = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return name.endsWith("Ops");
        }
    });
    Map<String, Metric> operations = new TreeMap<>();
    // Remove the instance name from the metrics.
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        operations.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    String filesPinnedProperty = MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.FILES_PINNED);
    operations.put(MetricsSystem.stripInstanceAndHost(filesPinnedProperty), mr.getGauges().get(filesPinnedProperty));
    Map<String, Counter> rpcInvocationsUpdated = new TreeMap<>();
    for (Map.Entry<String, Counter> entry : rpcInvocations.entrySet()) {
        rpcInvocationsUpdated.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    populateCounterValues(operations, rpcInvocationsUpdated, request);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) TreeMap(java.util.TreeMap) MetricFilter(com.codahale.metrics.MetricFilter) Counter(com.codahale.metrics.Counter) Metric(com.codahale.metrics.Metric) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 44 with Counter

use of com.codahale.metrics.Counter in project alluxio by Alluxio.

the class WebInterfaceWorkerMetricsServlet method populateValues.

/**
   * Populates key, value pairs for UI display.
   *
   * @param request The {@link HttpServletRequest} object
   * @throws IOException if an I/O error occurs
   */
private void populateValues(HttpServletRequest request) throws IOException {
    MetricRegistry mr = MetricsSystem.METRIC_REGISTRY;
    Long workerCapacityTotal = (Long) mr.getGauges().get(MetricsSystem.getWorkerMetricName(DefaultBlockWorker.Metrics.CAPACITY_TOTAL)).getValue();
    Long workerCapacityUsed = (Long) mr.getGauges().get(MetricsSystem.getWorkerMetricName(DefaultBlockWorker.Metrics.CAPACITY_USED)).getValue();
    int workerCapacityUsedPercentage = (workerCapacityTotal > 0) ? (int) (100L * workerCapacityUsed / workerCapacityTotal) : 0;
    request.setAttribute("workerCapacityUsedPercentage", workerCapacityUsedPercentage);
    request.setAttribute("workerCapacityFreePercentage", 100 - workerCapacityUsedPercentage);
    Map<String, Counter> counters = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return !(name.endsWith("Ops"));
        }
    });
    Map<String, Counter> rpcInvocations = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return name.endsWith("Ops");
        }
    });
    Map<String, Metric> operations = new TreeMap<>();
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        operations.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    String blockCachedProperty = MetricsSystem.getWorkerMetricName(DefaultBlockWorker.Metrics.BLOCKS_CACHED);
    operations.put(MetricsSystem.stripInstanceAndHost(blockCachedProperty), mr.getGauges().get(blockCachedProperty));
    Map<String, Counter> rpcInvocationsUpdated = new TreeMap<>();
    for (Map.Entry<String, Counter> entry : rpcInvocations.entrySet()) {
        rpcInvocationsUpdated.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    populateCounterValues(operations, rpcInvocationsUpdated, request);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) TreeMap(java.util.TreeMap) MetricFilter(com.codahale.metrics.MetricFilter) Counter(com.codahale.metrics.Counter) Metric(com.codahale.metrics.Metric) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 45 with Counter

use of com.codahale.metrics.Counter in project camel by apache.

the class MetricComponentSpringTest method testMetricsRegistryFromCamelRegistry.

@Test
public void testMetricsRegistryFromCamelRegistry() throws Exception {
    // TODO - 12.05.2014, Lauri - is there any better way to set this up?
    MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class);
    Counter mockCounter = Mockito.mock(Counter.class);
    InOrder inOrder = Mockito.inOrder(mockRegistry, mockCounter);
    when(mockRegistry.counter("A")).thenReturn(mockCounter);
    endpoint.expectedMessageCount(1);
    producer.sendBody(new Object());
    endpoint.assertIsSatisfied();
    inOrder.verify(mockRegistry, times(1)).counter("A");
    inOrder.verify(mockCounter, times(1)).inc(512L);
    inOrder.verifyNoMoreInteractions();
}
Also used : Counter(com.codahale.metrics.Counter) InOrder(org.mockito.InOrder) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Aggregations

Counter (com.codahale.metrics.Counter)66 Test (org.junit.Test)31 Map (java.util.Map)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 Timer (com.codahale.metrics.Timer)14 Histogram (com.codahale.metrics.Histogram)11 HashMap (java.util.HashMap)11 Gauge (com.codahale.metrics.Gauge)10 Meter (com.codahale.metrics.Meter)9 Metric (com.codahale.metrics.Metric)9 Random (java.util.Random)8 SolrInfoBean (org.apache.solr.core.SolrInfoBean)6 SortedMap (java.util.SortedMap)5 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)4 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)4 Description (com.google.gerrit.metrics.Description)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3