Search in sources :

Example 46 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 47 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)

Example 48 with Metadata

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

the class AnnotationReaderGetOrRegisterTest method assertGetsOrRegisters.

private <T extends Metric, A extends Annotation> void assertGetsOrRegisters(AnnotationReader<A> reader, Class<T> metric, int expectedTags) {
    InjectionPoint point = testMethodAsInjectionPoint();
    T c1 = reader.getOrRegister(point, metric, registry);
    assertNotNull(c1);
    T c2 = reader.getOrRegister(point, metric, registry);
    assertNotNull(c2);
    assertSame(c1, c2);
    Set<MetricID> metricIDs = registry.getMetricIDs();
    assertEquals(1, metricIDs.size());
    MetricID metricID = metricIDs.iterator().next();
    assertEquals(expectedTags, metricID.getTagsAsArray().length);
    if (expectedTags == 2) {
        assertArrayEquals(new Tag[] { new Tag("a", "b"), new Tag("c", "d") }, metricID.getTagsAsArray());
    }
    Metadata metadata = registry.getMetadata(metricID.getName());
    assertNotNull(metadata);
    A annotation = null;
    try {
        annotation = reader.annotation(point);
    } catch (IllegalArgumentException ex) {
        // with no annotation, done
        return;
    }
    assertEquals(reader.type(), metadata.getTypeRaw());
    String displayName = reader.displayName(annotation);
    if (displayName.isEmpty()) {
        assertEquals(metricID.getName(), metadata.getDisplayName());
    } else {
        assertEquals(displayName, metadata.getDisplayName());
    }
    String description = reader.description(annotation);
    if (description.isEmpty()) {
        assertFalse(metadata.description().isPresent());
    } else {
        assertEquals(description, metadata.getDescription());
    }
    String unit = reader.unit(annotation);
    if (unit.isEmpty()) {
        assertFalse(metadata.unit().isPresent());
    } else {
        assertEquals(unit, metadata.getUnit());
    }
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Metadata(org.eclipse.microprofile.metrics.Metadata) Tag(org.eclipse.microprofile.metrics.Tag)

Example 49 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
 * @return the list of unresolved MBean Metadata
 */
public List<MBeanMetadata> resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
    List<MBeanMetadata> unresolvedMetadataList = new ArrayList<>();
    List<MBeanMetadata> resolvedMetadataList = new ArrayList<>();
    List<Metadata> removedMetadataList = new ArrayList<>(metadataList.size());
    for (MBeanMetadata metadata : metadataList) {
        if (!metadata.isValid()) {
            removedMetadataList.add(metadata);
            continue;
        }
        if (metadata.getMBean().contains(SPECIFIER) || metadata.getMBean().contains(KEY) || metadata.getMBean().contains(ATTRIBUTE) || metadata.getMBean().contains(SUB_ATTRIBUTE) || metadata.getMBean().contains(INSTANCE)) {
            try {
                String instanceName = serverEnv.getInstanceName();
                // set (optional) instance the query expression
                String queryExpression = metadata.getMBean().replace(INSTANCE, instanceName);
                if (metadata.getMBean().contains(SPECIFIER) || metadata.getMBean().contains(KEY)) {
                    MBeanExpression mBeanExpression = new MBeanExpression(queryExpression.replace(SPECIFIER, "*").replace(KEY, "*"));
                    String dynamicKey = mBeanExpression.findDynamicKey();
                    Set<ObjectName> mBeanObjects = mBeanExpression.queryNames(null);
                    if (mBeanObjects.isEmpty()) {
                        unresolvedMetadataList.add(metadata);
                        LOGGER.log(INFO, "{0} does not correspond to any MBeans", metadata.getMBean());
                    } else if (metadata.isDynamic()) {
                        unresolvedMetadataList.add(metadata);
                    }
                    for (ObjectName objName : mBeanObjects) {
                        String dynamicValue = objName.getKeyPropertyList().get(dynamicKey);
                        resolvedMetadataList.addAll(loadAttribute(objName, mBeanExpression, metadata, dynamicValue, instanceName));
                    }
                } else {
                    MBeanExpression mBeanExpression = new MBeanExpression(queryExpression);
                    ObjectName objName = mBeanExpression.getObjectName();
                    if (objName == null) {
                        unresolvedMetadataList.add(metadata);
                        LOGGER.log(INFO, "{0} does not correspond to any MBeans", metadata.getMBean());
                    } else if (metadata.isDynamic()) {
                        unresolvedMetadataList.add(metadata);
                    }
                    resolvedMetadataList.addAll(loadAttribute(objName, mBeanExpression, metadata, null, instanceName));
                }
            } catch (IllegalArgumentException ex) {
                LOGGER.log(SEVERE, ex, () -> metadata.getMBean() + " is invalid");
            } finally {
                removedMetadataList.add(metadata);
            }
        }
    }
    metadataList.removeAll(removedMetadataList);
    metadataList.addAll(resolvedMetadataList);
    return unresolvedMetadataList;
}
Also used : ArrayList(java.util.ArrayList) Metadata(org.eclipse.microprofile.metrics.Metadata) ObjectName(javax.management.ObjectName)

Example 50 with Metadata

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

the class MetricsWriterImpl method writeMetricFamily.

private void writeMetricFamily(MetricExporter exporter, String contextName, String metricName, MetricRegistryImpl registry) {
    Metadata metadata = registry.getMetadata(metricName);
    for (Entry<MetricID, Metric> metric : registry.getMetrics(metricName).entrySet()) {
        MetricID metricID = metric.getKey();
        if (globalTags.length > 0) {
            Tag[] tagsWithoutGlobal = metricID.getTagsAsArray();
            Tag[] tags = new Tag[tagsWithoutGlobal.length + globalTags.length];
            arraycopy(globalTags, 0, tags, 0, globalTags.length);
            arraycopy(tagsWithoutGlobal, 0, tags, globalTags.length, tagsWithoutGlobal.length);
            metricID = new MetricID(metricName, tags);
        }
        if (!contextName.isEmpty()) {
            Tag[] tagsWithoutApp = metricID.getTagsAsArray();
            Tag[] tags = Arrays.copyOf(tagsWithoutApp, tagsWithoutApp.length + 1);
            tags[tagsWithoutApp.length] = new Tag("_app", contextName);
            metricID = new MetricID(metricName, tags);
        }
        exporter.export(metricID, metric.getValue(), metadata);
    }
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Metadata(org.eclipse.microprofile.metrics.Metadata) Metric(org.eclipse.microprofile.metrics.Metric) Tag(org.eclipse.microprofile.metrics.Tag)

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