use of org.eclipse.microprofile.metrics.Timer in project Payara by payara.
the class TimedInterceptor method applyInterceptor.
@Override
protected <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E element) throws Exception {
String name = resolver.timed(bean.getBeanClass(), element).metricName();
Timer timer = (Timer) registry.getMetrics().get(name);
if (timer == null) {
throw new IllegalStateException("No timer with name [" + name + "] found in registry [" + registry + "]");
}
Timer.Context time = timer.time();
try {
return context.proceed();
} finally {
time.stop();
}
}
use of org.eclipse.microprofile.metrics.Timer in project Payara by payara.
the class OpenMetricsExporterTest method exportTimer.
@Test
public void exportTimer() {
Timer timer = mock(Timer.class);
when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(23L));
when(timer.getCount()).thenReturn(80L);
when(timer.getMeanRate()).thenReturn(0.004292520715985437d);
when(timer.getOneMinuteRate()).thenReturn(2.794076465421066E-14d);
when(timer.getFiveMinuteRate()).thenReturn(4.800392614619373E-4d);
when(timer.getFifteenMinuteRate()).thenReturn(0.01063191047532505d);
Snapshot snapshot = mock(Snapshot.class);
when(timer.getSnapshot()).thenReturn(snapshot);
when(snapshot.getMin()).thenReturn(169916L);
when(snapshot.getMax()).thenReturn(560869L);
when(snapshot.getMean()).thenReturn(415041d);
when(snapshot.getStdDev()).thenReturn(652907d);
when(snapshot.getMedian()).thenReturn(293324d);
when(snapshot.get75thPercentile()).thenReturn(344914d);
when(snapshot.get95thPercentile()).thenReturn(543647d);
when(snapshot.get98thPercentile()).thenReturn(2706543d);
when(snapshot.get99thPercentile()).thenReturn(5608694d);
when(snapshot.get999thPercentile()).thenReturn(5608694d);
MetricID metricID = new MetricID("response_time");
Metadata metadata = Metadata.builder().withName(metricID.getName()).withDescription("Server response time for /index.html").withUnit(MetricUnits.NANOSECONDS).build();
assertOutputEqualsFile("Timer.txt", metricID, timer, metadata);
}
use of org.eclipse.microprofile.metrics.Timer in project Payara by payara.
the class TimedInterceptorTest method assertTimed.
private void assertTimed(int expectedStartCount) throws Exception {
Method element = TestUtils.getTestMethod();
Class<?> bean = getClass();
AnnotationReader<Timed> reader = AnnotationReader.TIMED;
Timer timer = MetricUtils.getOrRegisterByMetadataAndTags(registry, Timer.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
TimedInterceptor.proceedTimed(context, element, bean, registry::getMetric);
assertEquals(expectedStartCount + 1, timer.getCount());
TimedInterceptor.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 {
TimedInterceptor.proceedTimed(context, element, bean, (metricId, type) -> null);
fail("Expected a IllegalStateException because the metric does not exist");
} catch (IllegalStateException ex) {
assertEquals("No Timer 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 {
TimedInterceptor.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());
// test should run in < 1 sec
assertTrue(timer.getMeanRate() > 3d);
}
use of org.eclipse.microprofile.metrics.Timer in project Payara by payara.
the class JsonExporterGetTest method exportTimer.
@Test
public void exportTimer() {
Timer timer = mock(Timer.class);
when(timer.getElapsedTime()).thenReturn(Duration.ofMillis(45678L));
when(timer.getCount()).thenReturn(29382L);
when(timer.getMeanRate()).thenReturn(12.185627192860734d);
when(timer.getOneMinuteRate()).thenReturn(12.563d);
when(timer.getFiveMinuteRate()).thenReturn(12.364d);
when(timer.getFifteenMinuteRate()).thenReturn(12.126d);
Snapshot snapshot = mock(Snapshot.class);
when(timer.getSnapshot()).thenReturn(snapshot);
when(snapshot.getMin()).thenReturn(169916L);
when(snapshot.getMax()).thenReturn(5608694L);
when(snapshot.getMean()).thenReturn(415041.00024926325d);
when(snapshot.getStdDev()).thenReturn(652907.9633011606d);
when(snapshot.getMedian()).thenReturn(293324.0d);
when(snapshot.get75thPercentile()).thenReturn(344914d);
when(snapshot.get95thPercentile()).thenReturn(543647d);
when(snapshot.get98thPercentile()).thenReturn(2706543d);
when(snapshot.get99thPercentile()).thenReturn(5608694d);
when(snapshot.get999thPercentile()).thenReturn(5608694d);
// example uses same values for both timers so we can get away with just one
// but conceptually those should be two different timer instances
export(new MetricID("responseTime"), timer);
export(new MetricID("responseTime", new Tag("servlet", "two")), timer);
assertOutputEqualsFile("Timer.json");
}
use of org.eclipse.microprofile.metrics.Timer 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