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;
}
use of org.eclipse.microprofile.metrics.Metadata in project wildfly-swarm by wildfly-swarm.
the class MetricsRegistryImpl method register.
@Override
public <T extends Metric> T register(String name, T metric) throws IllegalArgumentException {
if (metricMap.keySet().contains(name)) {
throw new IllegalArgumentException("A metric with name " + name + " already exists");
}
MetricType type;
Class<?> metricCls = metric.getClass();
if (metricCls.getName().contains("Lambda")) {
// TODO [0] is brittle
String tname = metricCls.getGenericInterfaces()[0].getTypeName();
tname = tname.substring(tname.lastIndexOf('.') + 1);
tname = tname.toLowerCase();
type = MetricType.from(tname);
} else if (metricCls.isAnonymousClass()) {
type = MetricType.from(metricCls.getInterfaces().length == 0 ? metricCls.getSuperclass().getInterfaces()[0] : metricCls.getInterfaces()[0]);
} else {
if (!metricCls.isInterface()) {
// [0] is ok, as all our Impl classes implement exactly the one matching interface
type = MetricType.from(metricCls.getInterfaces()[0]);
} else {
type = MetricType.from(metricCls);
}
}
Metadata m = new Metadata(name, type);
metricMap.put(name, metric);
metadataMap.put(name, m);
return metric;
}
use of org.eclipse.microprofile.metrics.Metadata in project wildfly-swarm by wildfly-swarm.
the class MetricsRegistryImpl method register.
@Override
public <T extends Metric> T register(Metadata metadata, T metric) throws IllegalArgumentException {
String name = metadata.getName();
if (name == null) {
throw new IllegalArgumentException("Metric name must not be null");
}
Metadata existingMetadata = metadataMap.get(name);
boolean reusableFlag = (existingMetadata == null || existingMetadata.isReusable());
// Gauges are not reusable
if (metadata.getTypeRaw().equals(MetricType.GAUGE)) {
reusableFlag = false;
}
if (metricMap.keySet().contains(metadata.getName()) && !reusableFlag) {
throw new IllegalArgumentException("A metric with name " + metadata.getName() + " already exists");
}
if (existingMetadata != null && !existingMetadata.getTypeRaw().equals(metadata.getTypeRaw())) {
throw new IllegalArgumentException("Passed metric type does not match existing type");
}
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 JsonExporter method writeMetricsForMap.
private void writeMetricsForMap(StringBuilder sb, Map<String, Metric> metricMap, Map<String, Metadata> metadataMap) {
for (Iterator<Map.Entry<String, Metric>> iterator = metricMap.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, Metric> entry = iterator.next();
String key = entry.getKey();
Metric value = entry.getValue();
Metadata metadata = metadataMap.get(key);
if (metadata == null) {
throw new IllegalArgumentException("MD is null for " + key);
}
switch(metadata.getTypeRaw()) {
case GAUGE:
case COUNTER:
Number val = getValueFromMetric(value, key);
sb.append(" ").append('"').append(key).append('"').append(" : ").append(val);
break;
case METERED:
MeterImpl meter = (MeterImpl) value;
writeStartLine(sb, key);
writeMeterValues(sb, meter);
writeEndLine(sb);
break;
case TIMER:
TimerImpl timer = (TimerImpl) value;
writeStartLine(sb, key);
writeTimerValues(sb, timer, metadata.getUnit());
writeEndLine(sb);
break;
case HISTOGRAM:
HistogramImpl hist = (HistogramImpl) value;
writeStartLine(sb, key);
sb.append(" \"count\": ").append(hist.getCount()).append(COMMA_LF);
writeSnapshotValues(sb, hist.getSnapshot());
writeEndLine(sb);
break;
default:
LOG.error("JSE, Not yet supported: " + metadata);
}
if (iterator.hasNext()) {
sb.append(',');
}
sb.append(LF);
}
}
use of org.eclipse.microprofile.metrics.Metadata in project wildfly-swarm by wildfly-swarm.
the class JsonExporter method exportOneMetric.
@Override
public StringBuilder exportOneMetric(MetricRegistry.Type scope, String metricName) {
MetricRegistry registry = MetricRegistryFactory.get(scope);
Map<String, Metric> metricMap = registry.getMetrics();
Map<String, Metadata> metadataMap = registry.getMetadata();
Metric m = metricMap.get(metricName);
Map<String, Metric> outMap = new HashMap<>(1);
outMap.put(metricName, m);
StringBuilder sb = new StringBuilder();
sb.append("{");
writeMetricsForMap(sb, outMap, metadataMap);
sb.append("}");
sb.append(JsonExporter.LF);
return sb;
}
Aggregations