use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.
the class JsonExporterGetTest method exportSimpleTimer.
@Test
public void exportSimpleTimer() {
SimpleTimer timer = mock(SimpleTimer.class);
when(timer.getCount()).thenReturn(1L);
when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(12300000000L));
when(timer.getMaxTimeDuration()).thenReturn(Duration.ofMillis(3231000000L));
when(timer.getMinTimeDuration()).thenReturn(Duration.ofMillis(25600000L));
export(new MetricID("simple_responseTime"), timer);
assertOutputEqualsFile("SimpleTimer.json");
}
use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.
the class OpenMetricsExporterTest method exportSimpleTimer.
@Test
public void exportSimpleTimer() {
SimpleTimer timer = mock(SimpleTimer.class);
when(timer.getCount()).thenReturn(12L);
when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(12300L));
when(timer.getMaxTimeDuration()).thenReturn(Duration.ofMillis(3231L));
when(timer.getMinTimeDuration()).thenReturn(Duration.ofNanos(25600000L));
MetricID metricID = new MetricID("response_time");
Metadata metadata = Metadata.builder().withName(metricID.getName()).withDescription("The number of calls to this REST endpoint #(1)").build();
assertOutputEqualsFile("SimpleTimer.txt", metricID, timer, metadata);
}
use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.
the class SimplyTimedInterceptorTest method assertTimed.
private void assertTimed(int expectedStartCount) throws Exception {
Method element = TestUtils.getTestMethod();
Class<?> bean = getClass();
AnnotationReader<SimplyTimed> reader = AnnotationReader.SIMPLY_TIMED;
SimpleTimer timer = MetricUtils.getOrRegisterByMetadataAndTags(registry, SimpleTimer.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
assertEquals(expectedStartCount + 1, timer.getCount());
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
assertEquals(expectedStartCount + 2, timer.getCount());
verify(context, times(2)).proceed();
// now test error when it does not exist
try {
SimplyTimedInterceptor.proceedTimed(context, element, bean, (metricId, type) -> null);
fail("Expected a IllegalStateException because the metric does not exist");
} catch (IllegalStateException ex) {
assertEquals("No SimpleTimer with ID [" + reader.metricID(bean, element) + "] found in application registry", ex.getMessage());
}
verify(context, times(2)).proceed();
// test a annotated method that throws an exception
when(context.proceed()).thenThrow(new RuntimeException("Error in method"));
try {
SimplyTimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
fail("Expected a RuntimeException");
} catch (RuntimeException ex) {
assertEquals("Error in method", ex.getMessage());
}
verify(context, times(3)).proceed();
// need to remove the 'thenThrow' behaviour
reset(context);
assertEquals(expectedStartCount + 3, timer.getCount());
assertTrue(timer.getElapsedTime().toNanos() > 0);
}
use of org.eclipse.microprofile.metrics.SimpleTimer in project Payara by payara.
the class MetricsServiceImpl method collectRegistry.
private static void collectRegistry(String contextName, MetricRegistry registry, MonitoringDataCollector collector) {
// OBS: this way of iterating the metrics in the registry is optimal because of its internal data organisation
for (String name : registry.getNames()) {
for (Entry<MetricID, Metric> entry : ((MetricRegistryImpl) registry).getMetrics(name).entrySet()) {
MetricID metricID = entry.getKey();
Metric metric = entry.getValue();
try {
MonitoringDataCollector metricCollector = tagCollector(contextName, metricID, collector);
if (metric instanceof Counting) {
metricCollector.collect(toName(metricID, "Count"), ((Counting) metric).getCount());
}
if (metric instanceof SimpleTimer) {
metricCollector.collect(toName(metricID, "Duration"), ((SimpleTimer) metric).getElapsedTime().toMillis());
}
if (metric instanceof Timer) {
metricCollector.collect(toName(metricID, "MaxDuration"), ((Timer) metric).getSnapshot().getMax());
}
if (metric instanceof Gauge) {
Object value = ((Gauge<?>) metric).getValue();
if (value instanceof Number) {
metricCollector.collect(toName(metricID, getMetricUnitSuffix(registry.getMetadata(name).unit())), ((Number) value));
}
}
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Failed to retrieve metric: " + metricID);
;
}
}
}
}
Aggregations