use of org.eclipse.microprofile.metrics.annotation.Timed in project Payara by payara.
the class MetricsResolver method getMetadata.
public <T extends Annotation> Metadata getMetadata(String name, T annotation) {
Metadata metadata;
String[] tags;
if (Counted.class.isInstance(annotation)) {
Counted counted = (Counted) annotation;
metadata = new Metadata(name, counted.displayName(), counted.description(), COUNTER, counted.unit());
tags = counted.tags();
} else if (Gauge.class.isInstance(annotation)) {
Gauge gauge = (Gauge) annotation;
metadata = new Metadata(name, gauge.displayName(), gauge.description(), GAUGE, gauge.unit());
tags = gauge.tags();
} else if (Metered.class.isInstance(annotation)) {
Metered metered = (Metered) annotation;
metadata = new Metadata(name, metered.displayName(), metered.description(), METERED, metered.unit());
tags = metered.tags();
} else if (Timed.class.isInstance(annotation)) {
Timed timed = (Timed) annotation;
metadata = new Metadata(name, timed.displayName(), timed.description(), TIMER, timed.unit());
tags = timed.tags();
} else {
throw new IllegalArgumentException("Unsupported Metrics [" + annotation.getClass().getName() + "]");
}
for (String tag : tags) {
metadata.addTag(tag);
}
return metadata;
}
use of org.eclipse.microprofile.metrics.annotation.Timed 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.annotation.Timed in project Payara by payara.
the class MetricsInterceptor method registerMetrics.
private <E extends Member & AnnotatedElement> void registerMetrics(Class<?> bean, E element, Object target) {
MetricsResolver.Of<Counted> counted = resolver.counted(bean, element);
if (counted.isPresent()) {
registry.counter(counted.metadata());
}
MetricsResolver.Of<Metered> metered = resolver.metered(bean, element);
if (metered.isPresent()) {
registry.meter(metered.metadata());
}
MetricsResolver.Of<Timed> timed = resolver.timed(bean, element);
if (timed.isPresent()) {
registry.timer(timed.metadata());
}
if (element instanceof Method && element.isAnnotationPresent(org.eclipse.microprofile.metrics.annotation.Gauge.class)) {
MetricsResolver.Of<Gauge> gauge = resolver.gauge(bean, (Method) element);
if (gauge.isPresent()) {
registry.register(gauge.metricName(), new GaugeImpl((Method) element, target), gauge.metadata());
}
}
}
use of org.eclipse.microprofile.metrics.annotation.Timed in project wildfly-swarm by wildfly-swarm.
the class MetricsInterceptor method registerMetrics.
private <E extends Member & AnnotatedElement> void registerMetrics(Class<?> bean, E element) {
MetricResolver.Of<Counted> counted = resolver.counted(bean, element);
if (counted.isPresent()) {
Counted t = counted.metricAnnotation();
Metadata metadata = getMetadata(counted.metricName(), t.unit(), t.description(), t.displayName(), MetricType.COUNTER, t.tags());
registry.counter(metadata);
}
MetricResolver.Of<Metered> metered = resolver.metered(bean, element);
if (metered.isPresent()) {
Metered t = metered.metricAnnotation();
Metadata metadata = getMetadata(metered.metricName(), t.unit(), t.description(), t.displayName(), MetricType.METERED, t.tags());
registry.meter(metadata);
}
MetricResolver.Of<Timed> timed = resolver.timed(bean, element);
if (timed.isPresent()) {
Timed t = timed.metricAnnotation();
Metadata metadata = getMetadata(timed.metricName(), t.unit(), t.description(), t.displayName(), MetricType.TIMER, t.tags());
registry.timer(metadata);
}
}
Aggregations