Search in sources :

Example 16 with Tag

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

the class MetricRegistryImplTest method registerByMetadataAcceptsGaugeWithSameNameButDifferentTags.

@Test
public void registerByMetadataAcceptsGaugeWithSameNameButDifferentTags() {
    String name = nextName();
    Gauge<Long> gauge1 = () -> 1L;
    registry.register(withNameAnd(name).build(), gauge1, new Tag("a", "b"));
    Gauge<Long> gauge2 = () -> 2L;
    registry.register(withNameAnd(name).build(), gauge2, new Tag("a", "c"));
}
Also used : Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 17 with Tag

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

the class OpenMetricsExporterTest method tagValuesAreEscaped.

@Test
public void tagValuesAreEscaped() {
    Counter counter = mock(Counter.class);
    when(counter.getCount()).thenReturn(13L);
    MetricID metricID = new MetricID("test5", new Tag("key", "escape\\and\"and\n"));
    Metadata metadata = Metadata.builder().withName(metricID.getName()).build();
    assertOutputEquals("# TYPE application_test5_total counter\n" + "application_test5_total{key=\"escape\\\\and\\\"and\\n\"} 13\n", metricID, counter, metadata);
}
Also used : MetricID(org.eclipse.microprofile.metrics.MetricID) Counter(org.eclipse.microprofile.metrics.Counter) Metadata(org.eclipse.microprofile.metrics.Metadata) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test)

Example 18 with Tag

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

the class AnnotationReaderGetOrRegisterTest method gaugeFromMetdadata.

@Test
@Gauge(tags = { "a=b", "c=d" }, unit = MetricUnits.HOURS, description = "description")
public void gaugeFromMetdadata() {
    Metadata annoated = AnnotationReader.GAUGE.metadata(getClass(), TestUtils.getTestMethod());
    String name = getClass().getCanonicalName() + ".gaugeFromMetdadata";
    Metadata expected = Metadata.builder(annoated).withName(name).build();
    org.eclipse.microprofile.metrics.Gauge<Long> gauge = () -> 1L;
    registry.register(expected, gauge, new Tag("a", "b"), new Tag("c", "d"));
    InjectionPoint point = testMethodAsInjectionPoint();
    org.eclipse.microprofile.metrics.Gauge<?> gauge2 = AnnotationReader.GAUGE.getOrRegister(point, org.eclipse.microprofile.metrics.Gauge.class, registry);
    assertNotNull(gauge2);
    assertSame(gauge, gauge2);
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Metadata(org.eclipse.microprofile.metrics.Metadata) Tag(org.eclipse.microprofile.metrics.Tag) Test(org.junit.Test) Gauge(org.eclipse.microprofile.metrics.annotation.Gauge) ConcurrentGauge(org.eclipse.microprofile.metrics.annotation.ConcurrentGauge)

Example 19 with Tag

use of org.eclipse.microprofile.metrics.Tag 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 20 with Tag

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

the class MetricsResource method getGlobalTags.

private static Tag[] getGlobalTags() {
    Config config = ConfigProvider.getConfig();
    Optional<String> globalTagsProperty = config.getOptionalValue(GLOBAL_TAGS_VARIABLE, String.class);
    if (!globalTagsProperty.isPresent()) {
        return new Tag[0];
    }
    String globalTags = globalTagsProperty.get();
    if (globalTags == null || globalTags.length() == 0) {
        return new Tag[0];
    }
    String[] kvPairs = globalTags.split("(?<!\\\\),");
    Tag[] tags = new Tag[kvPairs.length];
    for (int i = 0; i < kvPairs.length; i++) {
        String kvString = kvPairs[i];
        if (kvString.length() == 0) {
            throw new IllegalArgumentException(GLOBAL_TAG_MALFORMED_EXCEPTION);
        }
        String[] keyValueSplit = kvString.split("(?<!\\\\)=");
        if (keyValueSplit.length != 2 || keyValueSplit[0].length() == 0 || keyValueSplit[1].length() == 0) {
            throw new IllegalArgumentException(GLOBAL_TAG_MALFORMED_EXCEPTION);
        }
        String key = keyValueSplit[0];
        String value = keyValueSplit[1];
        value = value.replace("\\,", ",");
        value = value.replace("\\=", "=");
        tags[i] = new Tag(key, value);
    }
    return tags;
}
Also used : Config(org.eclipse.microprofile.config.Config) Tag(org.eclipse.microprofile.metrics.Tag)

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