Search in sources :

Example 1 with MetricType

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

the class MetricRegistryImplTest method registerByMetadataAcceptsInstancesWithSameNameButDifferentTags.

@Test
public void registerByMetadataAcceptsInstancesWithSameNameButDifferentTags() {
    EnumSet<MetricType> types = EnumSet.allOf(MetricType.class);
    // obviously
    types.remove(MetricType.INVALID);
    // gauges cannot be created
    types.remove(MetricType.GAUGE);
    for (MetricType type : types) {
        String name = nextName();
        for (int i = 0; i < 10; i++) {
            final int n = i;
            MetadataBuilder metadata = withNameAnd(name).withType(type);
            Runnable register = () -> registry.register(metadata.build(), null, new Tag("a", "b" + n));
            // first time should work
            register.run();
            // works again as all metrics are reusable
            register.run();
        }
    }
}
Also used : MetadataBuilder(org.eclipse.microprofile.metrics.MetadataBuilder) MetricType(org.eclipse.microprofile.metrics.MetricType) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 2 with MetricType

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

the class MetricsRegistryImpl method register.

@Override
public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {
    if (metricMap.keySet().contains(name)) {
        throw new IllegalArgumentException("A metric with name " + name + " already exists");
    }
    MetricType type;
    Class<?> metricCls = metric.getClass();
    if (metricCls.getName().contains("Lambda")) {
        // TODO [0] is brittle
        String tname = metricCls.getGenericInterfaces()[0].getTypeName();
        tname = tname.substring(tname.lastIndexOf('.') + 1);
        tname = tname.toLowerCase();
        type = MetricType.from(tname);
    } else if (metricCls.isAnonymousClass()) {
        type = MetricType.from(metricCls.getInterfaces().length == 0 ? metricCls.getSuperclass().getInterfaces()[0] : metricCls.getInterfaces()[0]);
    } else {
        if (!metricCls.isInterface()) {
            // [0] is ok, as all our Impl classes implement exactly the one matching interface
            type = MetricType.from(metricCls.getInterfaces()[0]);
        } else {
            type = MetricType.from(metricCls);
        }
    }
    Metadata m = new Metadata(name, type);
    metricMap.put(name, metric);
    metadataMap.put(name, m);
    return metric;
}
Also used : MetricType(org.eclipse.microprofile.metrics.MetricType) Metadata(org.eclipse.microprofile.metrics.Metadata)

Example 3 with MetricType

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

the class AnnotationReaderTest method assertMetricType.

/*
     * Helpers...
     */
private static <E extends AnnotatedElement & Member> void assertMetricType(Map<Class<?>, MetricType> expected, E[] elements, Function<E, Class<?>> actualType) {
    AnnotationReader<Metric> reader = AnnotationReader.METRIC;
    for (E element : elements) {
        if (!element.isSynthetic()) {
            MetricType expectedType = expected.get(actualType.apply(element));
            Class<?> bean = element.getDeclaringClass();
            assertEquals(expectedType, reader.metadata(bean, element).getTypeRaw());
            assertEquals(expectedType, reader.metadata(TestUtils.fakeInjectionPointFor(element, element)).getTypeRaw());
            assertEquals(expectedType, reader.metadata(TestUtils.fakeMemberOf(element, element)).getTypeRaw());
        }
    }
}
Also used : MetricType(org.eclipse.microprofile.metrics.MetricType) Metric(org.eclipse.microprofile.metrics.annotation.Metric)

Aggregations

MetricType (org.eclipse.microprofile.metrics.MetricType)3 Metadata (org.eclipse.microprofile.metrics.Metadata)1 MetadataBuilder (org.eclipse.microprofile.metrics.MetadataBuilder)1 Tag (org.eclipse.microprofile.metrics.Tag)1 Metric (org.eclipse.microprofile.metrics.annotation.Metric)1 Test (org.junit.Test)1