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);
}
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);
}
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);
}
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);
}
}
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));
}
Aggregations