Search in sources :

Example 1 with Metric

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

the class GarbageCollectorMetricSet method getMetrics.

@Override
public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<String, Metric>();
    for (final GarbageCollectorMXBean gc : garbageCollectors) {
        final String name = WHITESPACE.matcher(gc.getName()).replaceAll("-");
        gauges.put(name(name, "count"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return gc.getCollectionCount();
            }
        });
        gauges.put(name(name, "time"), new Gauge<Long>() {

            @Override
            public Long getValue() {
                return gc.getCollectionTime();
            }
        });
    }
    return Collections.unmodifiableMap(gauges);
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Metric(com.codahale.metrics.Metric)

Example 2 with Metric

use of com.codahale.metrics.Metric 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 3 with Metric

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

the class JCacheGaugeSet method getMetrics.

@Override
public Map<String, Metric> getMetrics() {
    Set<ObjectInstance> cacheBeans = getCacheBeans();
    List<String> availableStatsNames = retrieveStatsNames();
    Map<String, Metric> gauges = new HashMap<String, Metric>(cacheBeans.size() * availableStatsNames.size());
    for (ObjectInstance cacheBean : cacheBeans) {
        ObjectName objectName = cacheBean.getObjectName();
        String cacheName = objectName.getKeyProperty("Cache");
        for (String statsName : availableStatsNames) {
            JmxAttributeGauge jmxAttributeGauge = new JmxAttributeGauge(objectName, statsName);
            gauges.put(name(cacheName, toSpinalCase(statsName)), jmxAttributeGauge);
        }
    }
    return Collections.unmodifiableMap(gauges);
}
Also used : JmxAttributeGauge(com.codahale.metrics.JmxAttributeGauge) HashMap(java.util.HashMap) ObjectInstance(javax.management.ObjectInstance) Metric(com.codahale.metrics.Metric) ObjectName(javax.management.ObjectName)

Example 4 with Metric

use of com.codahale.metrics.Metric in project HikariCP by brettwooldridge.

the class TestMetrics method testMetricUsage.

@Test
public void testMetricUsage() throws SQLException {
    assumeFalse(Os.isFamily(Os.FAMILY_WINDOWS));
    MetricRegistry metricRegistry = new MetricRegistry();
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setMetricRegistry(metricRegistry);
    config.setInitializationFailTimeout(0);
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection connection = ds.getConnection()) {
            UtilityElf.quietlySleep(250L);
        }
        Histogram histo = metricRegistry.getHistograms(new MetricFilter() {

            /** {@inheritDoc} */
            @Override
            public boolean matches(String name, Metric metric) {
                return name.equals(MetricRegistry.name("testMetricUsage", "pool", "Usage"));
            }
        }).values().iterator().next();
        assertEquals(1, histo.getCount());
        double seventyFifth = histo.getSnapshot().get75thPercentile();
        assertTrue("Seventy-fith percentile less than 250ms: " + seventyFifth, seventyFifth >= 250.0);
    }
}
Also used : Histogram(com.codahale.metrics.Histogram) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) MetricFilter(com.codahale.metrics.MetricFilter) MetricRegistry(com.codahale.metrics.MetricRegistry) Connection(java.sql.Connection) Metric(com.codahale.metrics.Metric) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 5 with Metric

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

Aggregations

Metric (com.codahale.metrics.Metric)58 Test (org.junit.Test)27 MetricRegistry (com.codahale.metrics.MetricRegistry)16 Map (java.util.Map)16 HashMap (java.util.HashMap)13 Counter (com.codahale.metrics.Counter)10 Gauge (com.codahale.metrics.Gauge)10 Timer (com.codahale.metrics.Timer)10 MetricFilter (com.codahale.metrics.MetricFilter)8 MetricFilter (com.kixeye.chassis.support.metrics.MetricFilter)8 Meter (com.codahale.metrics.Meter)7 Histogram (com.codahale.metrics.Histogram)4 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)4 Timed (com.codahale.metrics.annotation.Timed)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 IOException (java.io.IOException)3 List (java.util.List)3 Random (java.util.Random)3 Collectors (java.util.stream.Collectors)3