Search in sources :

Example 1 with Meter

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

the class MeteredInterceptor method applyInterceptor.

@Override
protected <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E element) throws Exception {
    String name = resolver.metered(bean.getBeanClass(), element).metricName();
    Meter meter = (Meter) registry.getMetrics().get(name);
    if (meter == null) {
        throw new IllegalStateException("No meter with name [" + name + "] found in registry [" + registry + "]");
    }
    try {
        return context.proceed();
    } finally {
        meter.mark();
    }
}
Also used : Meter(org.eclipse.microprofile.metrics.Meter)

Example 2 with Meter

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

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

Meter (org.eclipse.microprofile.metrics.Meter)3 Gauge (org.eclipse.microprofile.metrics.Gauge)2 Metric (org.eclipse.microprofile.metrics.Metric)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 Metadata (org.eclipse.microprofile.metrics.Metadata)1