use of org.eclipse.microprofile.metrics.Metadata in project Payara by payara.
the class MetricsResolver method getMetadata.
public <T extends Annotation> Metadata getMetadata(String name, T annotation) {
Metadata metadata;
String[] tags;
if (Counted.class.isInstance(annotation)) {
Counted counted = (Counted) annotation;
metadata = new Metadata(name, counted.displayName(), counted.description(), COUNTER, counted.unit());
tags = counted.tags();
} else if (Gauge.class.isInstance(annotation)) {
Gauge gauge = (Gauge) annotation;
metadata = new Metadata(name, gauge.displayName(), gauge.description(), GAUGE, gauge.unit());
tags = gauge.tags();
} else if (Metered.class.isInstance(annotation)) {
Metered metered = (Metered) annotation;
metadata = new Metadata(name, metered.displayName(), metered.description(), METERED, metered.unit());
tags = metered.tags();
} else if (Timed.class.isInstance(annotation)) {
Timed timed = (Timed) annotation;
metadata = new Metadata(name, timed.displayName(), timed.description(), TIMER, timed.unit());
tags = timed.tags();
} else {
throw new IllegalArgumentException("Unsupported Metrics [" + annotation.getClass().getName() + "]");
}
for (String tag : tags) {
metadata.addTag(tag);
}
return metadata;
}
use of org.eclipse.microprofile.metrics.Metadata in project Payara by payara.
the class MetricRegistryImpl method getOrAdd.
private <T extends Metric> T getOrAdd(Metadata metadata, MetricType metricType) {
String name = metadata.getName();
Metadata existingMetadata = metadataMap.get(name);
if (existingMetadata == null) {
Metric metric;
switch(metricType) {
case COUNTER:
metric = new CounterImpl();
break;
case GAUGE:
throw new IllegalArgumentException(String.format("Unsupported operation for Gauge ['%s']", name));
case METERED:
metric = new MeterImpl();
break;
case HISTOGRAM:
metric = new HistogramImpl();
break;
case TIMER:
metric = new TimerImpl();
break;
case INVALID:
default:
throw new IllegalStateException("Invalid metric type : " + metricType);
}
register(metadata, metric);
} else if (!existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
throw new IllegalArgumentException(String.format("Metric ['%s'] type['%s'] does not match with existing type['%s']", name, metadata.getType(), existingMetadata.getType()));
}
return (T) metricMap.get(name);
}
use of org.eclipse.microprofile.metrics.Metadata in project Payara by payara.
the class MetricRegistryImpl method register.
@Override
public <T extends Metric> T register(Metadata metadata, T metric) throws IllegalArgumentException {
String name = metadata.getName();
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Metric name must not be null or empty");
}
Metadata existingMetadata = metadataMap.get(name);
if (existingMetadata != null) {
// if existing metric declared not reusable
if (!existingMetadata.isReusable()) {
throw new IllegalArgumentException(String.format("Metric ['%s'] already exists and declared not reusable", name));
}
// Only metrics of the same type can be reused under the same name
if (!existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
throw new IllegalArgumentException(String.format("Metric ['%s'] type['%s'] does not match with existing type['%s']", name, metadata.getType(), existingMetadata.getType()));
}
// reusable does not apply to gauges
if (GAUGE.equals(metadata.getTypeRaw())) {
throw new IllegalArgumentException(String.format("Gauge type metric['%s'] is not reusable", name));
}
}
metricMap.put(name, metric);
metadataMap.put(name, metadata);
return metric;
}
use of org.eclipse.microprofile.metrics.Metadata in project wildfly-swarm by wildfly-swarm.
the class MetricsInterceptor method metrics.
@AroundConstruct
private Object metrics(InvocationContext context) throws Exception {
Class<?> bean = context.getConstructor().getDeclaringClass();
LOGGER.infof("MetricsInterceptor, bean=%s\n", bean);
// Registers the bean constructor metrics
registerMetrics(bean, context.getConstructor());
// Registers the methods metrics over the bean type hierarchy
Class<?> type = bean;
do {
// TODO: discover annotations declared on implemented interfaces
for (Method method : type.getDeclaredMethods()) {
if (!method.isSynthetic() && !Modifier.isPrivate(method.getModifiers())) {
registerMetrics(bean, method);
}
}
type = type.getSuperclass();
} while (!Object.class.equals(type));
Object target = context.proceed();
// Registers the gauges over the bean type hierarchy after the target is constructed as it is required for the gauge invocations
type = bean;
do {
// TODO: discover annotations declared on implemented interfaces
for (Method method : type.getDeclaredMethods()) {
MetricResolver.Of<Gauge> gauge = resolver.gauge(bean, method);
if (gauge.isPresent()) {
Gauge g = gauge.metricAnnotation();
Metadata metadata = getMetadata(gauge.metricName(), g.unit(), g.description(), g.displayName(), MetricType.GAUGE, g.tags());
registry.register(metadata, new ForwardingGauge(method, context.getTarget()));
}
}
type = type.getSuperclass();
} while (!Object.class.equals(type));
return target;
}
use of org.eclipse.microprofile.metrics.Metadata in project wildfly-swarm by wildfly-swarm.
the class JmxWorker method getMetrics.
public Map<String, Double> getMetrics(MetricRegistry.Type scope) {
Map<String, Metadata> metadataMap = MetricRegistryFactory.get(scope).getMetadata();
Map<String, Double> outcome = new HashMap<>();
for (Metadata m : metadataMap.values()) {
if (!(m instanceof ExtendedMetadata)) {
throw new IllegalStateException("Not extended Metadata " + m);
}
ExtendedMetadata em = (ExtendedMetadata) m;
Double val = getValue(em.getMbean()).doubleValue();
outcome.put(em.getName(), val);
}
return outcome;
}
Aggregations