Search in sources :

Example 21 with Gauge

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

the class MetricRepositoryAutoConfigurationTests method dropwizardInstalledIfPresent.

@Test
public void dropwizardInstalledIfPresent() {
    this.context = new AnnotationConfigApplicationContext(MetricsDropwizardAutoConfiguration.class, MetricRepositoryAutoConfiguration.class, AopAutoConfiguration.class);
    GaugeService gaugeService = this.context.getBean(GaugeService.class);
    assertThat(gaugeService).isNotNull();
    gaugeService.submit("foo", 2.7);
    DropwizardMetricServices exporter = this.context.getBean(DropwizardMetricServices.class);
    assertThat(exporter).isEqualTo(gaugeService);
    MetricRegistry registry = this.context.getBean(MetricRegistry.class);
    @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getMetrics().get("gauge.foo");
    assertThat(gauge.getValue()).isEqualTo(new Double(2.7));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BufferGaugeService(org.springframework.boot.actuate.metrics.buffer.BufferGaugeService) GaugeService(org.springframework.boot.actuate.metrics.GaugeService) MetricRegistry(com.codahale.metrics.MetricRegistry) AopAutoConfiguration(org.springframework.boot.autoconfigure.aop.AopAutoConfiguration) DropwizardMetricServices(org.springframework.boot.actuate.metrics.dropwizard.DropwizardMetricServices) Gauge(com.codahale.metrics.Gauge) Test(org.junit.Test)

Example 22 with Gauge

use of com.codahale.metrics.Gauge 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 23 with Gauge

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

the class AlluxioMasterRestServiceHandlerTest method getMetrics.

@Test
public void getMetrics() {
    final int FILES_PINNED_TEST_VALUE = 100;
    String filesPinnedProperty = MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.FILES_PINNED);
    Gauge<Integer> filesPinnedGauge = new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return FILES_PINNED_TEST_VALUE;
        }
    };
    MetricSet mockMetricsSet = mock(MetricSet.class);
    Map<String, Metric> map = new HashMap<>();
    map.put(filesPinnedProperty, filesPinnedGauge);
    when(mockMetricsSet.getMetrics()).thenReturn(map);
    MetricsSystem.METRIC_REGISTRY.registerAll(mockMetricsSet);
    Response response = mHandler.getMetrics();
    try {
        assertNotNull("Response must be not null!", response);
        assertNotNull("Response must have a entry!", response.getEntity());
        assertTrue("Entry must be a SortedMap!", (response.getEntity() instanceof SortedMap));
        SortedMap<String, Long> metricsMap = (SortedMap<String, Long>) response.getEntity();
        assertFalse("Metrics Map must be not empty!", (metricsMap.isEmpty()));
        assertTrue("Map must contain key " + filesPinnedProperty + "!", metricsMap.containsKey(filesPinnedProperty));
        assertEquals(FILES_PINNED_TEST_VALUE, metricsMap.get(filesPinnedProperty).longValue());
    } finally {
        response.close();
    }
}
Also used : HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Gauge(com.codahale.metrics.Gauge) MetricSet(com.codahale.metrics.MetricSet) Response(javax.ws.rs.core.Response) SortedMap(java.util.SortedMap) Metric(com.codahale.metrics.Metric) Test(org.junit.Test)

Example 24 with Gauge

use of com.codahale.metrics.Gauge 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 25 with Gauge

use of com.codahale.metrics.Gauge 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)

Aggregations

Gauge (com.codahale.metrics.Gauge)57 Test (org.junit.Test)38 Counter (com.codahale.metrics.Counter)10 Meter (com.codahale.metrics.Meter)8 Metric (com.codahale.metrics.Metric)8 Timer (com.codahale.metrics.Timer)7 SortedMap (java.util.SortedMap)7 Histogram (com.codahale.metrics.Histogram)6 MetricRegistry (com.codahale.metrics.MetricRegistry)6 Map (java.util.Map)6 Matchers.anyString (org.mockito.Matchers.anyString)3 Snapshot (com.codahale.metrics.Snapshot)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 TreeMap (java.util.TreeMap)2 Semaphore (java.util.concurrent.Semaphore)2 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)2 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)2 AsmGauge (com.alibaba.jstorm.common.metric.AsmGauge)1 MetricSet (com.codahale.metrics.MetricSet)1