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