Search in sources :

Example 26 with MetricID

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

the class MBeanMetadataHelper method registerMetadata.

/**
 * Registers metrics as MBeans
 *
 * @param metricRegistry Registry to add metrics to
 * @param metadataList List of all {@link MBeanMetadata} representing a
 * {@link Metric}
 * @param globalTags
 * @param isRetry true if this is not initial registration, this is used to
 * register lazy-loaded MBeans
 * @return the list of unresolved MBean Metadata
 */
public List<MBeanMetadata> registerMetadata(MetricRegistry metricRegistry, List<MBeanMetadata> metadataList, boolean isRetry) {
    if (!metricRegistry.getNames().isEmpty() && !isRetry) {
        metricRegistry.removeMatching(MetricFilter.ALL);
    }
    List<MBeanMetadata> unresolvedMetadataList = resolveDynamicMetadata(metadataList);
    for (MBeanMetadata beanMetadata : metadataList) {
        List<Tag> tags = new ArrayList<>();
        for (XmlTag tag : beanMetadata.getTags()) {
            tags.add(new Tag(tag.getName(), tag.getValue()));
        }
        try {
            if (metricRegistry.getNames().contains(beanMetadata.getName()) && metricRegistry.getMetricIDs().contains(new MetricID(beanMetadata.getName(), tags.toArray(new Tag[tags.size()])))) {
                continue;
            }
            Metric type;
            MBeanExpression mBeanExpression = new MBeanExpression(beanMetadata.getMBean());
            switch(beanMetadata.getTypeRaw()) {
                case COUNTER:
                    type = new MBeanCounterImpl(mBeanExpression);
                    break;
                case GAUGE:
                    type = new MBeanGuageImpl(mBeanExpression);
                    break;
                default:
                    throw new IllegalStateException("Unsupported type : " + beanMetadata);
            }
            metricRegistry.register(beanMetadata, type, tags.toArray(new Tag[tags.size()]));
        } catch (IllegalArgumentException ex) {
            LOGGER.log(WARNING, ex.getMessage(), ex);
        }
    }
    return unresolvedMetadataList;
}
Also used : ArrayList(java.util.ArrayList) MetricID(org.eclipse.microprofile.metrics.MetricID) Metric(org.eclipse.microprofile.metrics.Metric) Tag(org.eclipse.microprofile.metrics.Tag)

Example 27 with MetricID

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

the class JsonExporterOptionsTest method singleMetricOption.

@Test
public void singleMetricOption() {
    Gauge<Long> fooVal = () -> 1L;
    MetricID fooValID = new MetricID("fooVal", new Tag("store", "webshop"));
    Metadata fooValMeta = Metadata.builder().withName("fooVal").withDescription("The size of foo after each request").withUnit(MetricUnits.MILLISECONDS).withDisplayName("Size of foo").withType(MetricType.GAUGE).build();
    export(fooValID, fooVal, fooValMeta);
    assertOutputEqualsFile("Options1.json");
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 28 with MetricID

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

the class JsonExporterOptionsTest method gaugesWithNonNumberValuesDoExportMetadata.

@Test
public void gaugesWithNonNumberValuesDoExportMetadata() {
    Gauge<String> gauge = () -> "hello world";
    MetricID metricID = new MetricID("test3");
    Metadata metadata = Metadata.builder().withName(metricID.getName()).withType(MetricType.GAUGE).build();
    assertOutputEquals("\n" + "{\n" + "    \"test3\": {\n" + "        \"unit\": \"none\",\n" + "        \"type\": \"gauge\"\n" + "    }\n" + "}", metricID, gauge, metadata);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata) Test(org.junit.Test)

Example 29 with MetricID

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

the class MetricRegistryImpl method findMetricOrCreate.

@SuppressWarnings("unchecked")
private <T extends Metric> T findMetricOrCreate(Metadata metadata, boolean useExistingMetadata, T metric, Tag... tags) {
    MetricID metricID = new MetricID(metadata.getName(), tags);
    MetricFamily<?> family = metricsFamiliesByName.get(metricID.getName());
    if (family == null) {
        return register(metadata, useExistingMetadata, metric, tags);
    }
    Metric existing = family.get(metricID);
    if (existing == null) {
        checkSameType(metricID.getName(), metadata, family.metadata);
        return register(metadata, useExistingMetadata, metric, tags);
    }
    if (useExistingMetadata && metadata.getType() != family.metadata.getType() || !useExistingMetadata && !metadata.equals(family.metadata)) {
        throw new IllegalArgumentException(String.format("Tried to lookup a metric with conflicting metadata, looup is %s, existing is %s", metadata.toString(), family.metadata.toString()));
    }
    return (T) existing;
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metric(org.eclipse.microprofile.metrics.Metric)

Example 30 with MetricID

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

the class MetricRegistryImpl method register.

@SuppressWarnings("unchecked")
private <T extends Metric> T register(Metadata metadata, boolean useExistingMetadata, T metric, Tag... tags) {
    if (metadata.getTypeRaw() == MetricType.INVALID) {
        metadata = withType(metadata, MetricType.from(metric.getClass()));
    }
    String name = metadata.getName();
    checkNameIsNotNullOrEmpty(name);
    if (useExistingMetadata) {
        Metadata existingMetadata = getMetadata(name);
        if (existingMetadata != null) {
            checkSameType(name, metadata, existingMetadata);
            metadata = existingMetadata;
        }
    }
    final Metadata newMetadata = metadata;
    final T newMetric = metric != null ? metric : (T) createMetricInstance(newMetadata);
    MetricFamily<T> family = (MetricFamily<T>) metricsFamiliesByName.computeIfAbsent(name, key -> new MetricFamily<>(newMetadata));
    MetricID metricID = new MetricID(name, tags);
    if (family.metadata != newMetadata) {
        checkReusableMetadata(name, newMetadata, family.metadata);
    }
    T current = family.metrics.computeIfAbsent(metricID, key -> newMetric);
    notifyRegistrationListeners(metricID);
    return current;
}
Also used : ALL(org.eclipse.microprofile.metrics.MetricFilter.ALL) Histogram(org.eclipse.microprofile.metrics.Histogram) Metadata(org.eclipse.microprofile.metrics.Metadata) SortedSet(java.util.SortedSet) Gauge(org.eclipse.microprofile.metrics.Gauge) Tag(org.eclipse.microprofile.metrics.Tag) Metric(org.eclipse.microprofile.metrics.Metric) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) METERED(org.eclipse.microprofile.metrics.MetricType.METERED) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Level(java.util.logging.Level) Meter(org.eclipse.microprofile.metrics.Meter) ConcurrentGauge(org.eclipse.microprofile.metrics.ConcurrentGauge) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Counter(org.eclipse.microprofile.metrics.Counter) Collectors.toMap(java.util.stream.Collectors.toMap) SIMPLE_TIMER(org.eclipse.microprofile.metrics.MetricType.SIMPLE_TIMER) Map(java.util.Map) HISTOGRAM(org.eclipse.microprofile.metrics.MetricType.HISTOGRAM) Collections.emptyMap(java.util.Collections.emptyMap) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) MetricType(org.eclipse.microprofile.metrics.MetricType) Vetoed(javax.enterprise.inject.Vetoed) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) COUNTER(org.eclipse.microprofile.metrics.MetricType.COUNTER) Logger(java.util.logging.Logger) MetricID(org.eclipse.microprofile.metrics.MetricID) Timer(org.eclipse.microprofile.metrics.Timer) List(java.util.List) MetricFilter(org.eclipse.microprofile.metrics.MetricFilter) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) TreeMap(java.util.TreeMap) Entry(java.util.Map.Entry) TIMER(org.eclipse.microprofile.metrics.MetricType.TIMER) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) GAUGE(org.eclipse.microprofile.metrics.MetricType.GAUGE) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) SortedMap(java.util.SortedMap) MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata)

Aggregations

MetricID (org.eclipse.microprofile.metrics.MetricID)53 Test (org.junit.Test)41 Metadata (org.eclipse.microprofile.metrics.Metadata)31 Tag (org.eclipse.microprofile.metrics.Tag)25 Counter (org.eclipse.microprofile.metrics.Counter)10 Metric (org.eclipse.microprofile.metrics.Metric)6 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)6 Snapshot (org.eclipse.microprofile.metrics.Snapshot)6 Histogram (org.eclipse.microprofile.metrics.Histogram)5 ConcurrentGauge (org.eclipse.microprofile.metrics.ConcurrentGauge)4 Timer (org.eclipse.microprofile.metrics.Timer)4 Gauge (org.eclipse.microprofile.metrics.Gauge)3 MonitoringDataCollector (fish.payara.monitoring.collect.MonitoringDataCollector)2 ArrayList (java.util.ArrayList)2 Meter (org.eclipse.microprofile.metrics.Meter)2 NoSuchRegistryException (fish.payara.microprofile.metrics.exception.NoSuchRegistryException)1 TimerImpl (fish.payara.microprofile.metrics.impl.TimerImpl)1 MBeanMetadata (fish.payara.microprofile.metrics.jmx.MBeanMetadata)1 FileNotFoundException (java.io.FileNotFoundException)1 Collections.emptyMap (java.util.Collections.emptyMap)1