Search in sources :

Example 1 with Metric

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

the class MetricsService method getMetricsAsMap.

public Map<String, Metric> getMetricsAsMap(String registryName, String metricName) throws NoSuchRegistryException, NoSuchMetricException {
    MetricRegistry registry = getRegistry(registryName);
    Map<String, Metric> metricMap = registry.getMetrics();
    if (metricMap.containsKey(metricName)) {
        return singletonMap(metricName, metricMap.get(metricName));
    } else {
        throw new NoSuchMetricException(metricName);
    }
}
Also used : NoSuchMetricException(fish.payara.microprofile.metrics.exception.NoSuchMetricException) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Metric(org.eclipse.microprofile.metrics.Metric)

Example 2 with Metric

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

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

the class MetricRegistryImpl method remove.

@Override
public boolean remove(String name) {
    final Metric metric = metricMap.remove(name);
    metadataMap.remove(name);
    return metric != null;
}
Also used : Metric(org.eclipse.microprofile.metrics.Metric)

Example 4 with Metric

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

the class JsonMetricWriter method getJsonFromMetrics.

private JsonObjectBuilder getJsonFromMetrics(Map<String, Metric> metricMap) {
    JsonObjectBuilder payloadBuilder = Json.createObjectBuilder();
    for (Map.Entry<String, Metric> entry : metricMap.entrySet()) {
        String metricName = entry.getKey();
        Metric metric = entry.getValue();
        if (Counter.class.isInstance(metric)) {
            payloadBuilder.add(metricName, ((Counter) metric).getCount());
        } else if (Gauge.class.isInstance(metric)) {
            Number value;
            Object gaugeValue;
            try {
                gaugeValue = ((Gauge) metric).getValue();
            } catch (IllegalStateException e) {
                // The forwarding gauge is unloaded
                continue;
            }
            if (!Number.class.isInstance(gaugeValue)) {
                LOGGER.log(Level.FINER, "Skipping JSON output for Gauge: {0} of type {1}", new Object[] { metricName, gaugeValue.getClass() });
                continue;
            }
            value = (Number) gaugeValue;
            addValueToJsonObject(payloadBuilder, metricName, value);
        } else if (Histogram.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getHistogramNumbers((Histogram) metric)));
        } else if (Meter.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getMeterNumbers((Meter) metric)));
        } else if (Timer.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getTimerNumbers((Timer) metric)));
        } else {
            LOGGER.log(Level.WARNING, "Metric type '{0} for {1} is invalid", new Object[] { metric.getClass(), metricName });
        }
    }
    return payloadBuilder;
}
Also used : Meter(org.eclipse.microprofile.metrics.Meter) Metric(org.eclipse.microprofile.metrics.Metric) JsonObjectBuilder(javax.json.JsonObjectBuilder) HashMap(java.util.HashMap) Map(java.util.Map) Gauge(org.eclipse.microprofile.metrics.Gauge)

Example 5 with Metric

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

the class PrometheusWriter method writeMetricMap.

private void writeMetricMap(StringBuilder builder, String registryName, Map<String, Metric> metricMap, Map<String, Metadata> metricMetadataMap) {
    for (Entry<String, Metric> entry : metricMap.entrySet()) {
        String metricName = entry.getKey();
        // Scope and name are separated by colon (:)
        if (!BASE.getName().equals(registryName) && !VENDOR.getName().equals(registryName)) {
            registryName = APPLICATION.getName();
        }
        String name = registryName + ":" + metricName;
        Metric metric = entry.getValue();
        Metadata metricMetadata = metricMetadataMap.get(metricName);
        String description = metricMetadata.getDescription() == null || metricMetadata.getDescription().trim().isEmpty() ? EMPTY_STRING : metricMetadata.getDescription();
        String tags = metricMetadata.getTagsAsString();
        String unit = metricMetadata.getUnit();
        PrometheusExporter exporter = new PrometheusExporter(builder);
        if (Counter.class.isInstance(metric)) {
            exporter.exportCounter((Counter) metric, name, description, tags);
        } else if (Gauge.class.isInstance(metric)) {
            exporter.exportGauge((Gauge) metric, name, description, tags, unit);
        } else if (Histogram.class.isInstance(metric)) {
            exporter.exportHistogram((Histogram) metric, name, description, tags, unit);
        } else if (Meter.class.isInstance(metric)) {
            exporter.exportMeter((Meter) metric, name, description, tags);
        } else if (Timer.class.isInstance(metric)) {
            exporter.exportTimer((Timer) metric, name, description, tags, unit);
        } else {
            LOGGER.log(Level.WARNING, "Metric type {0} for {1} is invalid", new Object[] { metric.getClass(), metricName });
        }
    }
}
Also used : Meter(org.eclipse.microprofile.metrics.Meter) Metadata(org.eclipse.microprofile.metrics.Metadata) Metric(org.eclipse.microprofile.metrics.Metric) Gauge(org.eclipse.microprofile.metrics.Gauge)

Aggregations

Metric (org.eclipse.microprofile.metrics.Metric)5 Gauge (org.eclipse.microprofile.metrics.Gauge)2 Metadata (org.eclipse.microprofile.metrics.Metadata)2 Meter (org.eclipse.microprofile.metrics.Meter)2 NoSuchMetricException (fish.payara.microprofile.metrics.exception.NoSuchMetricException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)1