Search in sources :

Example 1 with Metadata

use of org.eclipse.microprofile.metrics.Metadata 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 Metadata

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

the class MetricRegistryImpl method getOrAdd.

private <T extends Metric> T getOrAdd(Metadata metadata, MetricType metricType) {
    String name = metadata.getName();
    Metadata existingMetadata = metadataMap.get(name);
    if (existingMetadata == null) {
        Metric metric;
        switch(metricType) {
            case COUNTER:
                metric = new CounterImpl();
                break;
            case GAUGE:
                throw new IllegalArgumentException(String.format("Unsupported operation for Gauge ['%s']", name));
            case METERED:
                metric = new MeterImpl();
                break;
            case HISTOGRAM:
                metric = new HistogramImpl();
                break;
            case TIMER:
                metric = new TimerImpl();
                break;
            case INVALID:
            default:
                throw new IllegalStateException("Invalid metric type : " + metricType);
        }
        register(metadata, metric);
    } else if (!existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
        throw new IllegalArgumentException(String.format("Metric ['%s'] type['%s'] does not match with existing type['%s']", name, metadata.getType(), existingMetadata.getType()));
    }
    return (T) metricMap.get(name);
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata) Metric(org.eclipse.microprofile.metrics.Metric)

Example 3 with Metadata

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

the class MetricRegistryImpl method register.

@Override
public <T extends Metric> T register(Metadata metadata, T metric) throws IllegalArgumentException {
    String name = metadata.getName();
    if (name == null || name.trim().isEmpty()) {
        throw new IllegalArgumentException("Metric name must not be null or empty");
    }
    Metadata existingMetadata = metadataMap.get(name);
    if (existingMetadata != null) {
        // if existing metric declared not reusable
        if (!existingMetadata.isReusable()) {
            throw new IllegalArgumentException(String.format("Metric ['%s'] already exists and declared not reusable", name));
        }
        // Only metrics of the same type can be reused under the same name
        if (!existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
            throw new IllegalArgumentException(String.format("Metric ['%s'] type['%s'] does not match with existing type['%s']", name, metadata.getType(), existingMetadata.getType()));
        }
        // reusable does not apply to gauges
        if (GAUGE.equals(metadata.getTypeRaw())) {
            throw new IllegalArgumentException(String.format("Gauge type metric['%s'] is not reusable", name));
        }
    }
    metricMap.put(name, metric);
    metadataMap.put(name, metadata);
    return metric;
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata)

Example 4 with Metadata

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

the class MetricsInterceptor method metrics.

@AroundConstruct
private Object metrics(InvocationContext context) throws Exception {
    Class<?> bean = context.getConstructor().getDeclaringClass();
    LOGGER.infof("MetricsInterceptor, bean=%s\n", bean);
    // Registers the bean constructor metrics
    registerMetrics(bean, context.getConstructor());
    // Registers the methods metrics over the bean type hierarchy
    Class<?> type = bean;
    do {
        // TODO: discover annotations declared on implemented interfaces
        for (Method method : type.getDeclaredMethods()) {
            if (!method.isSynthetic() && !Modifier.isPrivate(method.getModifiers())) {
                registerMetrics(bean, method);
            }
        }
        type = type.getSuperclass();
    } while (!Object.class.equals(type));
    Object target = context.proceed();
    // Registers the gauges over the bean type hierarchy after the target is constructed as it is required for the gauge invocations
    type = bean;
    do {
        // TODO: discover annotations declared on implemented interfaces
        for (Method method : type.getDeclaredMethods()) {
            MetricResolver.Of<Gauge> gauge = resolver.gauge(bean, method);
            if (gauge.isPresent()) {
                Gauge g = gauge.metricAnnotation();
                Metadata metadata = getMetadata(gauge.metricName(), g.unit(), g.description(), g.displayName(), MetricType.GAUGE, g.tags());
                registry.register(metadata, new ForwardingGauge(method, context.getTarget()));
            }
        }
        type = type.getSuperclass();
    } while (!Object.class.equals(type));
    return target;
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata) Method(java.lang.reflect.Method) Gauge(org.eclipse.microprofile.metrics.annotation.Gauge) AroundConstruct(javax.interceptor.AroundConstruct)

Example 5 with Metadata

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

the class JmxWorker method getMetrics.

public Map<String, Double> getMetrics(MetricRegistry.Type scope) {
    Map<String, Metadata> metadataMap = MetricRegistryFactory.get(scope).getMetadata();
    Map<String, Double> outcome = new HashMap<>();
    for (Metadata m : metadataMap.values()) {
        if (!(m instanceof ExtendedMetadata)) {
            throw new IllegalStateException("Not extended Metadata " + m);
        }
        ExtendedMetadata em = (ExtendedMetadata) m;
        Double val = getValue(em.getMbean()).doubleValue();
        outcome.put(em.getName(), val);
    }
    return outcome;
}
Also used : HashMap(java.util.HashMap) Metadata(org.eclipse.microprofile.metrics.Metadata)

Aggregations

Metadata (org.eclipse.microprofile.metrics.Metadata)65 MetricID (org.eclipse.microprofile.metrics.MetricID)31 Test (org.junit.Test)30 Tag (org.eclipse.microprofile.metrics.Tag)12 Counter (org.eclipse.microprofile.metrics.Counter)9 Metric (org.eclipse.microprofile.metrics.Metric)9 MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)8 HashMap (java.util.HashMap)6 Map (java.util.Map)4 Histogram (org.eclipse.microprofile.metrics.Histogram)4 ArrayList (java.util.ArrayList)3 Meter (org.eclipse.microprofile.metrics.Meter)3 Gauge (org.eclipse.microprofile.metrics.annotation.Gauge)3 ObjectName (javax.management.ObjectName)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Gauge (org.eclipse.microprofile.metrics.Gauge)2 MetricType (org.eclipse.microprofile.metrics.MetricType)2 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)2 Snapshot (org.eclipse.microprofile.metrics.Snapshot)2