Search in sources :

Example 6 with Metadata

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

the class MetricCDIExtension method registerCustomMetrics.

private void registerCustomMetrics(@Observes AfterDeploymentValidation adv, BeanManager manager) {
    MetricsService metricsService = Globals.getDefaultBaseServiceLocator().getService(MetricsService.class);
    MetricRegistry registry = metricsService.getOrAddRegistry(metricsService.getApplicationName());
    MetricsHelper helper = getReference(manager, MetricsHelper.class);
    for (Map.Entry<Producer<?>, AnnotatedMember<?>> entry : metrics.entrySet()) {
        AnnotatedMember<?> annotatedMember = entry.getValue();
        Producer<?> prod = entry.getKey();
        if (hasInjectionPoints(annotatedMember)) {
            continue;
        }
        Metadata metadata = helper.metadataOf(annotatedMember);
        registry.register(metadata.getName(), (Metric) prod.produce(manager.createCreationalContext(null)), metadata);
    }
    metrics.clear();
}
Also used : MetricProducer(fish.payara.microprofile.metrics.cdi.producer.MetricProducer) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) Producer(javax.enterprise.inject.spi.Producer) MetricRegistryProducer(fish.payara.microprofile.metrics.cdi.producer.MetricRegistryProducer) MetricsService(fish.payara.microprofile.metrics.MetricsService) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) MetricsHelper(fish.payara.microprofile.metrics.cdi.MetricsHelper) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) Metadata(org.eclipse.microprofile.metrics.Metadata) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Metadata

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

the class MBeanMetadataHelper method resolveDynamicMetadata.

/**
 * Resolve dynamic metadata by replacing specifier <b>%s</b> with the mbean value.
 *
 * @param metadataList list of MBean Metadata
 */
public void resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
    List<MBeanMetadata> resolvedMetadataList = new ArrayList<>();
    List<Metadata> removedMetadataList = new ArrayList<>(metadataList.size());
    for (MBeanMetadata metadata : metadataList) {
        if (metadata.getName().contains(SPECIFIER)) {
            MBeanExpression mBeanExpression;
            try {
                mBeanExpression = new MBeanExpression(metadata.getMBean().replace(SPECIFIER, "*"));
                String dynamicKey = mBeanExpression.findDynamicKey();
                for (ObjectName objName : mBeanExpression.queryNames(null)) {
                    String dynamicValue = objName.getKeyPropertyList().get(dynamicKey);
                    resolvedMetadataList.add(new MBeanMetadata(objName.getCanonicalName() + "/" + mBeanExpression.getAttributeName(), metadata.getName().replace(SPECIFIER, dynamicValue), metadata.getDisplayName().replace(SPECIFIER, dynamicValue), metadata.getDescription().replace(SPECIFIER, dynamicValue), metadata.getTypeRaw(), metadata.getUnit()));
                }
            } catch (IllegalArgumentException ex) {
                LOGGER.log(Level.SEVERE, ex, () -> metadata.getMBean() + " is invalid");
            } finally {
                removedMetadataList.add(metadata);
            }
        }
    }
    metadataList.removeAll(removedMetadataList);
    metadataList.addAll(resolvedMetadataList);
}
Also used : ArrayList(java.util.ArrayList) Metadata(org.eclipse.microprofile.metrics.Metadata) ObjectName(javax.management.ObjectName)

Example 8 with Metadata

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

Metadata (org.eclipse.microprofile.metrics.Metadata)8 Metric (org.eclipse.microprofile.metrics.Metric)2 MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)2 MetricsService (fish.payara.microprofile.metrics.MetricsService)1 MetricsHelper (fish.payara.microprofile.metrics.cdi.MetricsHelper)1 MetricProducer (fish.payara.microprofile.metrics.cdi.producer.MetricProducer)1 MetricRegistryProducer (fish.payara.microprofile.metrics.cdi.producer.MetricRegistryProducer)1 NoSuchMetricException (fish.payara.microprofile.metrics.exception.NoSuchMetricException)1 MBeanMetadataHelper.registerMetadata (fish.payara.microprofile.metrics.jmx.MBeanMetadataHelper.registerMetadata)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AnnotatedMember (javax.enterprise.inject.spi.AnnotatedMember)1 ProcessProducer (javax.enterprise.inject.spi.ProcessProducer)1 Producer (javax.enterprise.inject.spi.Producer)1 ObjectName (javax.management.ObjectName)1 Gauge (org.eclipse.microprofile.metrics.Gauge)1 Meter (org.eclipse.microprofile.metrics.Meter)1 Counted (org.eclipse.microprofile.metrics.annotation.Counted)1 Gauge (org.eclipse.microprofile.metrics.annotation.Gauge)1