Search in sources :

Example 6 with Counter

use of com.codahale.metrics.Counter in project riposte by Nike-Inc.

the class CodahaleMetricsListenerTest method setupMetricRegistryAndCodahaleMetricsCollector.

private void setupMetricRegistryAndCodahaleMetricsCollector() {
    metricRegistryMock = mock(MetricRegistry.class);
    cmcMock = mock(CodahaleMetricsCollector.class);
    doReturn(metricRegistryMock).when(cmcMock).getMetricRegistry();
    registeredTimerMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Timer timerMock = mock(Timer.class);
        registeredTimerMocks.put(name, timerMock);
        return timerMock;
    }).when(metricRegistryMock).timer(anyString());
    registeredMeterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Meter meterMock = mock(Meter.class);
        registeredMeterMocks.put(name, meterMock);
        return meterMock;
    }).when(metricRegistryMock).meter(anyString());
    registeredCounterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Counter counterMock = mock(Counter.class);
        registeredCounterMocks.put(name, counterMock);
        return counterMock;
    }).when(metricRegistryMock).counter(anyString());
    registeredHistogramMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Histogram histogramMock = mock(Histogram.class);
        registeredHistogramMocks.put(name, histogramMock);
        return histogramMock;
    }).when(metricRegistryMock).histogram(anyString());
    registeredGauges = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Metric metric = invocation.getArgumentAt(1, Metric.class);
        if (metric instanceof Gauge)
            registeredGauges.put(name, (Gauge) metric);
        else if (metric instanceof Histogram)
            registeredHistogramMocks.put(name, (Histogram) metric);
        else
            throw new RuntimeException("Expected Gauge or Histogram, but received: " + metric.getClass().getName());
        return metric;
    }).when(metricRegistryMock).register(anyString(), any(Metric.class));
}
Also used : Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) MetricRegistry(com.codahale.metrics.MetricRegistry) Metric(com.codahale.metrics.Metric) Matchers.anyString(org.mockito.Matchers.anyString) Gauge(com.codahale.metrics.Gauge)

Example 7 with Counter

use of com.codahale.metrics.Counter in project opennms by OpenNMS.

the class StressCommand method doExecute.

@Override
protected Object doExecute() throws Exception {
    // Create the client
    final RpcClient<EchoRequest, EchoResponse> client = rpcClientFactory.getClient(EchoRpcModule.INSTANCE);
    // Create metrics to capture the results
    final MetricRegistry metrics = new MetricRegistry();
    final Histogram responseTimes = metrics.histogram("response-times");
    final Counter successes = metrics.counter("successes");
    final Counter failures = metrics.counter("failures");
    // Build and issue the requests
    System.out.printf("Executing %d requests.\n", count);
    final String message = Strings.repeat("*", messageSize);
    final CountDownLatch doneSignal = new CountDownLatch(count);
    long beforeExec = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        client.execute(buildRequest(message)).whenComplete((r, e) -> {
            if (e != null) {
                failures.inc();
            } else {
                responseTimes.update(System.currentTimeMillis() - r.getId());
                successes.inc();
            }
            doneSignal.countDown();
        });
    }
    long afterExec = System.currentTimeMillis();
    // Wait for the responses...
    System.out.printf("Waiting for responses.\n");
    while (true) {
        try {
            if (doneSignal.await(1, TimeUnit.SECONDS)) {
                // Done!
                System.out.printf("\nDone!\n");
                break;
            }
        } catch (InterruptedException e) {
            System.out.println("\nInterrupted!");
            break;
        }
        System.out.print(".");
        System.out.flush();
    }
    long afterResponse = System.currentTimeMillis();
    System.out.println();
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.MILLISECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    reporter.report();
    reporter.close();
    System.out.printf("Total miliseconds elapsed: %d\n", afterResponse - beforeExec);
    System.out.printf("Miliseconds spent generating requests: %d\n", afterExec - beforeExec);
    System.out.printf("Miliseconds spent waiting for responses: %d\n", afterResponse - afterExec);
    return null;
}
Also used : EchoResponse(org.opennms.core.rpc.echo.EchoResponse) Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) ConsoleReporter(com.codahale.metrics.ConsoleReporter) MetricRegistry(com.codahale.metrics.MetricRegistry) EchoRequest(org.opennms.core.rpc.echo.EchoRequest) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with Counter

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

the class AlluxioWorkerRestServiceHandler method getMetricsInternal.

private Map<String, Long> getMetricsInternal() {
    MetricRegistry metricRegistry = MetricsSystem.METRIC_REGISTRY;
    // Get all counters.
    Map<String, Counter> counters = metricRegistry.getCounters();
    // Only the gauge for cached blocks is retrieved here, other gauges are statistics of
    // free/used spaces, those statistics can be gotten via other REST apis.
    String blocksCachedProperty = MetricsSystem.getWorkerMetricName(DefaultBlockWorker.Metrics.BLOCKS_CACHED);
    @SuppressWarnings("unchecked") Gauge<Integer> blocksCached = (Gauge<Integer>) metricRegistry.getGauges().get(blocksCachedProperty);
    // Get values of the counters and gauges and put them into a metrics map.
    SortedMap<String, Long> metrics = new TreeMap<>();
    for (Map.Entry<String, Counter> counter : counters.entrySet()) {
        metrics.put(counter.getKey(), counter.getValue().getCount());
    }
    metrics.put(blocksCachedProperty, blocksCached.getValue().longValue());
    return metrics;
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) TreeMap(java.util.TreeMap) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 9 with Counter

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

the class AlluxioMasterRestServiceHandler method getMetricsInternal.

private Map<String, Long> getMetricsInternal() {
    MetricRegistry metricRegistry = MetricsSystem.METRIC_REGISTRY;
    // Get all counters.
    Map<String, Counter> counters = metricRegistry.getCounters();
    // Only the gauge for pinned files is retrieved here, other gauges are statistics of
    // free/used
    // spaces, those statistics can be gotten via other REST apis.
    String filesPinnedProperty = MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.FILES_PINNED);
    @SuppressWarnings("unchecked") Gauge<Integer> filesPinned = (Gauge<Integer>) MetricsSystem.METRIC_REGISTRY.getGauges().get(filesPinnedProperty);
    // Get values of the counters and gauges and put them into a metrics map.
    SortedMap<String, Long> metrics = new TreeMap<>();
    for (Map.Entry<String, Counter> counter : counters.entrySet()) {
        metrics.put(counter.getKey(), counter.getValue().getCount());
    }
    metrics.put(filesPinnedProperty, filesPinned.getValue().longValue());
    return metrics;
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) TreeMap(java.util.TreeMap) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 10 with Counter

use of com.codahale.metrics.Counter in project graylog2-server by Graylog2.

the class MetricUtilsTest method safelyRegister.

@Test
public void safelyRegister() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final Gauge<Long> longGauge = new Gauge<Long>() {

        @Override
        public Long getValue() {
            return 0L;
        }
    };
    final Gauge<Long> newGauge = MetricUtils.safelyRegister(metricRegistry, "somename", longGauge);
    assertSame("metric objects are identical", longGauge, newGauge);
    try {
        MetricUtils.safelyRegister(metricRegistry, "somename", longGauge);
    } catch (Exception e) {
        fail("Should not have thrown: " + e.getMessage());
    }
    try {
        //noinspection unused
        final Counter somename = MetricUtils.safelyRegister(metricRegistry, "somename", new Counter());
    } catch (Exception e) {
        assertTrue("Registering a metric with a different metric type fails on using it", e instanceof ClassCastException);
    }
}
Also used : Counter(com.codahale.metrics.Counter) MetricRegistry(com.codahale.metrics.MetricRegistry) Gauge(com.codahale.metrics.Gauge) Test(org.junit.Test)

Aggregations

Counter (com.codahale.metrics.Counter)55 Test (org.junit.Test)24 Map (java.util.Map)15 Timer (com.codahale.metrics.Timer)14 MetricRegistry (com.codahale.metrics.MetricRegistry)13 HashMap (java.util.HashMap)11 Gauge (com.codahale.metrics.Gauge)10 Histogram (com.codahale.metrics.Histogram)10 Metric (com.codahale.metrics.Metric)9 Meter (com.codahale.metrics.Meter)8 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