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"));
}
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);
}
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);
}
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;
}
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;
}
Aggregations