Search in sources :

Example 26 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)

Example 27 with Metadata

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

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

the class MetricsRegistryImpl method register.

@Override
public <T extends Metric> T register(Metadata metadata, T metric) throws IllegalArgumentException {
    String name = metadata.getName();
    if (name == null) {
        throw new IllegalArgumentException("Metric name must not be null");
    }
    Metadata existingMetadata = metadataMap.get(name);
    boolean reusableFlag = (existingMetadata == null || existingMetadata.isReusable());
    // Gauges are not reusable
    if (metadata.getTypeRaw().equals(MetricType.GAUGE)) {
        reusableFlag = false;
    }
    if (metricMap.keySet().contains(metadata.getName()) && !reusableFlag) {
        throw new IllegalArgumentException("A metric with name " + metadata.getName() + " already exists");
    }
    if (existingMetadata != null && !existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
        throw new IllegalArgumentException("Passed metric type does not match existing type");
    }
    metricMap.put(name, metric);
    metadataMap.put(name, metadata);
    return metric;
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata)

Example 29 with Metadata

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

the class JsonExporter method writeMetricsForMap.

private void writeMetricsForMap(StringBuilder sb, Map<String, Metric> metricMap, Map<String, Metadata> metadataMap) {
    for (Iterator<Map.Entry<String, Metric>> iterator = metricMap.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry<String, Metric> entry = iterator.next();
        String key = entry.getKey();
        Metric value = entry.getValue();
        Metadata metadata = metadataMap.get(key);
        if (metadata == null) {
            throw new IllegalArgumentException("MD is null for " + key);
        }
        switch(metadata.getTypeRaw()) {
            case GAUGE:
            case COUNTER:
                Number val = getValueFromMetric(value, key);
                sb.append("  ").append('"').append(key).append('"').append(" : ").append(val);
                break;
            case METERED:
                MeterImpl meter = (MeterImpl) value;
                writeStartLine(sb, key);
                writeMeterValues(sb, meter);
                writeEndLine(sb);
                break;
            case TIMER:
                TimerImpl timer = (TimerImpl) value;
                writeStartLine(sb, key);
                writeTimerValues(sb, timer, metadata.getUnit());
                writeEndLine(sb);
                break;
            case HISTOGRAM:
                HistogramImpl hist = (HistogramImpl) value;
                writeStartLine(sb, key);
                sb.append("    \"count\": ").append(hist.getCount()).append(COMMA_LF);
                writeSnapshotValues(sb, hist.getSnapshot());
                writeEndLine(sb);
                break;
            default:
                LOG.error("JSE, Not yet supported: " + metadata);
        }
        if (iterator.hasNext()) {
            sb.append(',');
        }
        sb.append(LF);
    }
}
Also used : Metadata(org.eclipse.microprofile.metrics.Metadata) MeterImpl(org.wildfly.swarm.microprofile.metrics.runtime.app.MeterImpl) Metric(org.eclipse.microprofile.metrics.Metric) HistogramImpl(org.wildfly.swarm.microprofile.metrics.runtime.app.HistogramImpl) TimerImpl(org.wildfly.swarm.microprofile.metrics.runtime.app.TimerImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with Metadata

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

the class JsonExporter method exportOneMetric.

@Override
public StringBuilder exportOneMetric(MetricRegistry.Type scope, String metricName) {
    MetricRegistry registry = MetricRegistryFactory.get(scope);
    Map<String, Metric> metricMap = registry.getMetrics();
    Map<String, Metadata> metadataMap = registry.getMetadata();
    Metric m = metricMap.get(metricName);
    Map<String, Metric> outMap = new HashMap<>(1);
    outMap.put(metricName, m);
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    writeMetricsForMap(sb, outMap, metadataMap);
    sb.append("}");
    sb.append(JsonExporter.LF);
    return sb;
}
Also used : HashMap(java.util.HashMap) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Metadata(org.eclipse.microprofile.metrics.Metadata) Metric(org.eclipse.microprofile.metrics.Metric)

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