use of org.eclipse.microprofile.metrics.MetricID 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.MetricID in project Payara by payara.
the class JsonExporterOptionsTest method singleMetricOption.
@Test
public void singleMetricOption() {
Gauge<Long> fooVal = () -> 1L;
MetricID fooValID = new MetricID("fooVal", new Tag("store", "webshop"));
Metadata fooValMeta = Metadata.builder().withName("fooVal").withDescription("The size of foo after each request").withUnit(MetricUnits.MILLISECONDS).withDisplayName("Size of foo").withType(MetricType.GAUGE).build();
export(fooValID, fooVal, fooValMeta);
assertOutputEqualsFile("Options1.json");
}
use of org.eclipse.microprofile.metrics.MetricID in project Payara by payara.
the class JsonExporterOptionsTest method gaugesWithNonNumberValuesDoExportMetadata.
@Test
public void gaugesWithNonNumberValuesDoExportMetadata() {
Gauge<String> gauge = () -> "hello world";
MetricID metricID = new MetricID("test3");
Metadata metadata = Metadata.builder().withName(metricID.getName()).withType(MetricType.GAUGE).build();
assertOutputEquals("\n" + "{\n" + " \"test3\": {\n" + " \"unit\": \"none\",\n" + " \"type\": \"gauge\"\n" + " }\n" + "}", metricID, gauge, metadata);
}
use of org.eclipse.microprofile.metrics.MetricID in project Payara by payara.
the class MetricRegistryImpl method findMetricOrCreate.
@SuppressWarnings("unchecked")
private <T extends Metric> T findMetricOrCreate(Metadata metadata, boolean useExistingMetadata, T metric, Tag... tags) {
MetricID metricID = new MetricID(metadata.getName(), tags);
MetricFamily<?> family = metricsFamiliesByName.get(metricID.getName());
if (family == null) {
return register(metadata, useExistingMetadata, metric, tags);
}
Metric existing = family.get(metricID);
if (existing == null) {
checkSameType(metricID.getName(), metadata, family.metadata);
return register(metadata, useExistingMetadata, metric, tags);
}
if (useExistingMetadata && metadata.getType() != family.metadata.getType() || !useExistingMetadata && !metadata.equals(family.metadata)) {
throw new IllegalArgumentException(String.format("Tried to lookup a metric with conflicting metadata, looup is %s, existing is %s", metadata.toString(), family.metadata.toString()));
}
return (T) existing;
}
use of org.eclipse.microprofile.metrics.MetricID 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;
}
Aggregations