Search in sources :

Example 41 with Metadata

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

Example 42 with Metadata

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

the class OpenMetricsExporterTest method eachTypeAndHelpLineOccursOnlyOnceForEachOpenMetricsName.

/*
     * Below tests are no examples from the specification
     */
@Test
public void eachTypeAndHelpLineOccursOnlyOnceForEachOpenMetricsName() {
    Gauge<Long> g1 = () -> 1L;
    MetricID g1ID = new MetricID("common", new Tag("a", "b"));
    Gauge<Long> g2 = () -> 2L;
    MetricID g2ID = new MetricID("common", new Tag("some", "other"));
    Metadata metadata = Metadata.builder().withName("common").withDescription("description").build();
    exporter.export(g1ID, g1, metadata);
    exporter.export(g2ID, g2, metadata);
    assertEquals("# TYPE application_common gauge\n" + "# HELP application_common description\n" + "application_common{a=\"b\"} 1\n" + "application_common{some=\"other\"} 2\n", actual.getBuffer().toString());
}
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 43 with Metadata

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

the class OpenMetricsExporterTest method gaugesWithNonNumberValuesAreNotExported.

@Test
public void gaugesWithNonNumberValuesAreNotExported() {
    Gauge<String> gauge = () -> "hello world";
    MetricID metricID = new MetricID("test3");
    Metadata metadata = Metadata.builder().withName(metricID.getName()).build();
    assertOutputEquals("", metricID, gauge, metadata);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata) Test(org.junit.Test)

Example 44 with Metadata

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

the class OpenMetricsExporterTest method exportTags.

@Test
@SuppressWarnings("unchecked")
public void exportTags() {
    Gauge<Long> fooVal = mock(Gauge.class);
    when(fooVal.getValue()).thenReturn(12345L);
    MetricID fooValID = new MetricID("fooVal", new Tag("store", "webshop"));
    Metadata fooValMetadata = Metadata.builder().withName(fooValID.getName()).withDescription("The average duration of foo requests during last 5 minutes").withUnit(MetricUnits.MILLISECONDS).build();
    MetricExporter base = exporter.in(Type.BASE);
    base.export(fooValID, fooVal, fooValMetadata);
    Gauge<Long> barVal = mock(Gauge.class);
    when(barVal.getValue()).thenReturn(42L);
    MetricID barValID = new MetricID("barVal", new Tag("component", "backend"), new Tag("store", "webshop"));
    Metadata barValMetadata = Metadata.builder().withName(barValID.getName()).withUnit(MetricUnits.KILOBYTES).build();
    base.export(barValID, barVal, barValMetadata);
    Gauge<Long> barVal2 = mock(Gauge.class);
    when(barVal2.getValue()).thenReturn(63L);
    MetricID barVal2ID = new MetricID("barVal", new Tag("component", "frontend"), new Tag("store", "webshop"));
    base.export(barVal2ID, barVal2, barValMetadata);
    assertOutputEqualsFile("GaugeTags.txt");
}
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 45 with Metadata

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

the class OpenMetricsExporterTest method exportGauge.

@Test
public void exportGauge() {
    @SuppressWarnings("unchecked") Gauge<Long> gauge = mock(Gauge.class);
    when(gauge.getValue()).thenReturn(80L);
    MetricID metricID = new MetricID("cost");
    Metadata metadata = Metadata.builder().withName(metricID.getName()).withDescription("The running cost of the server in dollars.").withUnit("dollars").build();
    assertOutputEqualsFile("Gauge.txt", metricID, gauge, metadata);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata) Test(org.junit.Test)

Aggregations

Metadata (org.eclipse.microprofile.metrics.Metadata)65 MetricID (org.eclipse.microprofile.metrics.MetricID)31 Test (org.junit.Test)30 Tag (org.eclipse.microprofile.metrics.Tag)12 Counter (org.eclipse.microprofile.metrics.Counter)9 Metric (org.eclipse.microprofile.metrics.Metric)9 MetricRegistry (org.eclipse.microprofile.metrics.MetricRegistry)8 HashMap (java.util.HashMap)6 Map (java.util.Map)4 Histogram (org.eclipse.microprofile.metrics.Histogram)4 ArrayList (java.util.ArrayList)3 Meter (org.eclipse.microprofile.metrics.Meter)3 Gauge (org.eclipse.microprofile.metrics.annotation.Gauge)3 ObjectName (javax.management.ObjectName)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Gauge (org.eclipse.microprofile.metrics.Gauge)2 MetricType (org.eclipse.microprofile.metrics.MetricType)2 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)2 Snapshot (org.eclipse.microprofile.metrics.Snapshot)2