Search in sources :

Example 1 with Tag

use of org.eclipse.microprofile.metrics.Tag in project wildfly by wildfly.

the class MicroProfileVendorMetricRegistry method registerMetric.

@Override
public void registerMetric(org.wildfly.extension.metrics.Metric metric, MetricMetadata metadata) {
    final Metric mpMetric;
    if (metadata.getType() == COUNTER) {
        mpMetric = new Counter() {

            @Override
            public void inc() {
            }

            @Override
            public void inc(long n) {
            }

            @Override
            public long getCount() {
                OptionalDouble value = metric.getValue();
                if (!value.isPresent()) {
                    // RuntimeException, after logging at DEBUG. That's what we want.
                    throw LOGGER.metricUnavailable();
                }
                return (long) value.getAsDouble();
            }
        };
    } else {
        mpMetric = new Gauge<Number>() {

            @Override
            public Double getValue() {
                OptionalDouble value = metric.getValue();
                if (!value.isPresent()) {
                    // RuntimeException, after logging at DEBUG. That's what we want.
                    throw LOGGER.metricUnavailable();
                }
                return value.getAsDouble();
            }
        };
    }
    lock.writeLock().lock();
    try {
        synchronized (vendorRegistry) {
            // TODO does the writeLock eliminate the need for this synchronized?
            final Metadata mpMetadata;
            Metadata existingMetadata = vendorRegistry.getMetadata().get(metadata.getMetricName());
            if (existingMetadata != null) {
                mpMetadata = existingMetadata;
            } else {
                mpMetadata = new ExtendedMetadata(metadata.getMetricName(), metadata.getMetricName(), metadata.getDescription(), metadata.getType() == COUNTER ? MetricType.COUNTER : MetricType.GAUGE, metricUnit(metadata.getMeasurementUnit()), null, false, // so that the name of the metric does not change ("vendor_" will not be prepended to it).
                Optional.of(false));
            }
            Tag[] mpTags = toMicroProfileMetricsTags(metadata.getTags());
            vendorRegistry.register(mpMetadata, mpMetric, mpTags);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Counter(org.eclipse.microprofile.metrics.Counter) MetricMetadata(org.wildfly.extension.metrics.MetricMetadata) Metadata(org.eclipse.microprofile.metrics.Metadata) ExtendedMetadata(io.smallrye.metrics.ExtendedMetadata) WildFlyMetricMetadata(org.wildfly.extension.metrics.WildFlyMetricMetadata) Metric(org.eclipse.microprofile.metrics.Metric) ExtendedMetadata(io.smallrye.metrics.ExtendedMetadata) Tag(org.eclipse.microprofile.metrics.Tag) OptionalDouble(java.util.OptionalDouble) OptionalDouble(java.util.OptionalDouble)

Example 2 with Tag

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

the class TestUtils method parseMetricID.

/**
 * Parses text form of a {@link MetricID} back to object.
 *
 * This supports both the result of calling {@link MetricID#toString()} as well as a simplified more user friendly form illustrated in the examples below:
 *
 * <pre>
 * my-metric-name
 * my-metric-name[tag=value,tag=value]
 * </pre>
 *
 * @param metric text form of a {@link MetricID}
 * @return
 */
public static MetricID parseMetricID(String metric) {
    int startOfTags = metric.indexOf('[');
    if (startOfTags < 0) {
        // no tags, must be simple format
        return new MetricID(metric);
    }
    int endOfTags = metric.indexOf(']');
    String[] tagNameValues = metric.substring(startOfTags + 1, endOfTags).split(",");
    Tag[] tags = new Tag[tagNameValues.length];
    for (int i = 0; i < tagNameValues.length; i++) {
        String tag = tagNameValues[i];
        int endOfName = tag.indexOf('=');
        String value = tag.substring(endOfName + 1);
        if (value.startsWith("\"") && value.endsWith("\"")) {
            value = value.substring(1, value.length() - 1);
        }
        tags[i] = new Tag(tag.substring(0, endOfName), value);
    }
    if (metric.startsWith("MetricID{")) {
        int startOfName = metric.indexOf('\'');
        return new MetricID(metric.substring(startOfName + 1, metric.indexOf('\'', startOfName + 1)), tags);
    }
    return new MetricID(metric.substring(0, startOfTags), tags);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Tag(org.eclipse.microprofile.metrics.Tag)

Example 3 with Tag

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

the class MethodFaultToleranceMetrics method withMethodTag.

private MetricID withMethodTag(String metric, Tag[] tags) {
    Tag method = new Tag("method", canonicalMethodName);
    if (tags.length == 0) {
        return new MetricID(metric, method);
    }
    Tag[] newTags = new Tag[tags.length + 1];
    newTags[0] = method;
    arraycopy(tags, 0, newTags, 1, tags.length);
    return new MetricID(metric, newTags);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Tag(org.eclipse.microprofile.metrics.Tag)

Example 4 with Tag

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

the class MethodFaultToleranceMetrics method registerPermutations.

private static void registerPermutations(String[][] tags, Consumer<Tag[]> register) {
    if (tags.length == 0) {
        register.accept(NO_TAGS);
        return;
    }
    if (tags.length == 1) {
        String[] tag1 = tags[0];
        for (int i = 1; i < tag1.length; i++) {
            register.accept(new Tag[] { new Tag(tag1[0], tag1[i]) });
        }
        return;
    }
    if (tags.length == 2) {
        String[] tag1 = tags[0];
        String[] tag2 = tags[1];
        for (int i = 1; i < tag1.length; i++) {
            for (int j = 1; j < tag2.length; j++) {
                register.accept(new Tag[] { new Tag(tag1[0], tag1[i]), new Tag(tag2[0], tag2[j]) });
            }
        }
        return;
    }
    throw new UnsupportedOperationException("Only 0 to 2 tags supported but got: " + tags.length);
}
Also used : Tag(org.eclipse.microprofile.metrics.Tag)

Example 5 with Tag

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

the class BulkheadMetricTckTest method bulkheadMetricHistogramTest.

/**
 * Scenario is equivalent to the TCK test of same name but not 100% identical
 */
@Test(timeout = 3000)
public void bulkheadMetricHistogramTest() {
    callMethodWithNewThreadAndWaitFor(commonWaiter);
    callMethodWithNewThreadAndWaitFor(commonWaiter);
    waitUntilPermitsAquired(2, 0);
    assertFurtherThreadThrowsBulkheadException(1);
    waitSome(100);
    commonWaiter.complete(null);
    waitUntilPermitsAquired(0, 0);
    Histogram executionTimes = registry.getHistogram(new MetricID("ft.bulkhead.runningDuration", new Tag("method", "fish.payara.microprofile.faulttolerance.policy.BulkheadMetricTckTest.bulkheadMetricHistogramTest_Method")));
    Snapshot snap = executionTimes.getSnapshot();
    assertNotNull(executionTimes);
    assertEquals(2, executionTimes.getCount());
    assertApproxMillis(100, Math.round(snap.getMedian()));
    assertApproxMillis(100, Math.round(snap.getMean()));
    // Now let's put some quick results through the bulkhead
    callMethodDirectly(null);
    callMethodDirectly(null);
    assertEquals(4, executionTimes.getCount());
    snap = executionTimes.getSnapshot();
    assertApproxMillis(50, Math.round(snap.getMean()));
}
Also used : Snapshot(org.eclipse.microprofile.metrics.Snapshot) Histogram(org.eclipse.microprofile.metrics.Histogram) MetricID(org.eclipse.microprofile.metrics.MetricID) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Aggregations

Tag (org.eclipse.microprofile.metrics.Tag)36 MetricID (org.eclipse.microprofile.metrics.MetricID)25 Test (org.junit.Test)23 Metadata (org.eclipse.microprofile.metrics.Metadata)12 Counter (org.eclipse.microprofile.metrics.Counter)7 Metric (org.eclipse.microprofile.metrics.Metric)5 Snapshot (org.eclipse.microprofile.metrics.Snapshot)5 Histogram (org.eclipse.microprofile.metrics.Histogram)4 ArrayList (java.util.ArrayList)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 ConcurrentGauge (org.eclipse.microprofile.metrics.ConcurrentGauge)2 Gauge (org.eclipse.microprofile.metrics.Gauge)2 Meter (org.eclipse.microprofile.metrics.Meter)2 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)2 Timer (org.eclipse.microprofile.metrics.Timer)2 TimerImpl (fish.payara.microprofile.metrics.impl.TimerImpl)1 ExtendedMetadata (io.smallrye.metrics.ExtendedMetadata)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1