Search in sources :

Example 1 with Counted

use of org.eclipse.microprofile.metrics.annotation.Counted 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;
}
Also used : Counted(org.eclipse.microprofile.metrics.annotation.Counted) Metered(org.eclipse.microprofile.metrics.annotation.Metered) Timed(org.eclipse.microprofile.metrics.annotation.Timed) Metadata(org.eclipse.microprofile.metrics.Metadata) Gauge(org.eclipse.microprofile.metrics.annotation.Gauge)

Example 2 with Counted

use of org.eclipse.microprofile.metrics.annotation.Counted in project Payara by payara.

the class CountedInterceptor method applyInterceptor.

@Override
protected <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E element) throws Exception {
    MetricsResolver.Of<Counted> counted = resolver.counted(bean.getBeanClass(), element);
    Counter counter = (Counter) registry.getMetrics().get(counted.metricName());
    if (counter == null) {
        throw new IllegalStateException("No counter with name [" + counted.metricName() + "] found in registry [" + registry + "]");
    }
    counter.inc();
    try {
        return context.proceed();
    } finally {
        if (!counted.metricAnnotation().monotonic()) {
            counter.dec();
        }
    }
}
Also used : Counted(org.eclipse.microprofile.metrics.annotation.Counted) Counter(org.eclipse.microprofile.metrics.Counter) MetricsResolver(fish.payara.microprofile.metrics.cdi.MetricsResolver)

Example 3 with Counted

use of org.eclipse.microprofile.metrics.annotation.Counted in project wildfly-swarm by wildfly-swarm.

the class CountedInterceptor method countedCallable.

private <E extends Member & AnnotatedElement> Object countedCallable(InvocationContext context, E element) throws Exception {
    MetricResolver.Of<Counted> counted = resolver.counted(bean.getBeanClass(), element);
    String name = counted.metricName();
    Counter counter = (Counter) registry.getCounters().get(name);
    if (counter == null) {
        throw new IllegalStateException("No counter with name [" + name + "] found in registry [" + registry + "]");
    }
    LOGGER.debugf("Increment counter [metricName: %s]", name);
    counter.inc();
    try {
        return context.proceed();
    } finally {
        if (!counted.metricAnnotation().monotonic()) {
            LOGGER.debugf("Decrement counter [metricName: %s]", name);
            counter.dec();
        }
    }
}
Also used : Counted(org.eclipse.microprofile.metrics.annotation.Counted) Counter(org.eclipse.microprofile.metrics.Counter)

Example 4 with Counted

use of org.eclipse.microprofile.metrics.annotation.Counted 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());
        }
    }
}
Also used : Counted(org.eclipse.microprofile.metrics.annotation.Counted) GaugeImpl(fish.payara.microprofile.metrics.impl.GaugeImpl) Metered(org.eclipse.microprofile.metrics.annotation.Metered) Timed(org.eclipse.microprofile.metrics.annotation.Timed) MetricsResolver(fish.payara.microprofile.metrics.cdi.MetricsResolver) Method(java.lang.reflect.Method) Gauge(org.eclipse.microprofile.metrics.annotation.Gauge)

Example 5 with Counted

use of org.eclipse.microprofile.metrics.annotation.Counted in project Payara by payara.

the class CountedInterceptorTest method assertCounterIncrements.

private void assertCounterIncrements(int expectedStartCount) throws Exception {
    Method element = TestUtils.getTestMethod();
    Class<?> bean = getClass();
    AnnotationReader<Counted> reader = AnnotationReader.COUNTED;
    Counter counter = MetricUtils.getOrRegisterByMetadataAndTags(registry, Counter.class, reader.metadata(bean, element), reader.tags(reader.annotation(bean, element)));
    CountedInterceptor.proceedCounted(context, element, bean, registry::getMetric);
    assertEquals(expectedStartCount + 1, counter.getCount());
    CountedInterceptor.proceedCounted(context, element, bean, registry::getMetric);
    assertEquals(expectedStartCount + 2, counter.getCount());
    verify(context, times(expectedStartCount + 2)).proceed();
    // now test error when it does not exist
    try {
        CountedInterceptor.proceedCounted(context, element, bean, (metricId, type) -> null);
        fail("Expected a IllegalStateException because the metric does not exist");
    } catch (IllegalStateException ex) {
        assertEquals("No Counter with ID [" + reader.metricID(bean, element) + "] found in application registry", ex.getMessage());
    }
    verify(context, times(expectedStartCount + 2)).proceed();
}
Also used : Counted(org.eclipse.microprofile.metrics.annotation.Counted) Counter(org.eclipse.microprofile.metrics.Counter) Method(java.lang.reflect.Method)

Aggregations

Counted (org.eclipse.microprofile.metrics.annotation.Counted)6 Counter (org.eclipse.microprofile.metrics.Counter)3 Metered (org.eclipse.microprofile.metrics.annotation.Metered)3 Timed (org.eclipse.microprofile.metrics.annotation.Timed)3 MetricsResolver (fish.payara.microprofile.metrics.cdi.MetricsResolver)2 Method (java.lang.reflect.Method)2 Metadata (org.eclipse.microprofile.metrics.Metadata)2 Gauge (org.eclipse.microprofile.metrics.annotation.Gauge)2 GaugeImpl (fish.payara.microprofile.metrics.impl.GaugeImpl)1