use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.
the class MetricsSourceBuilder method initRegistry.
private MetricsRegistry initRegistry(Object source) {
Class<?> cls = source.getClass();
MetricsRegistry r = null;
// Get the registry if it already exists.
for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
if (field.getType() != MetricsRegistry.class)
continue;
try {
field.setAccessible(true);
r = (MetricsRegistry) field.get(source);
hasRegistry = r != null;
break;
} catch (Exception e) {
LOG.warn("Error accessing field " + field, e);
continue;
}
}
// Create a new registry according to annotation
for (Annotation annotation : cls.getAnnotations()) {
if (annotation instanceof Metrics) {
Metrics ma = (Metrics) annotation;
info = factory.getInfo(cls, ma);
if (r == null) {
r = new MetricsRegistry(info);
}
r.setContext(ma.context());
}
}
if (r == null)
return new MetricsRegistry(cls.getSimpleName());
return r;
}
use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.
the class MutableMetricsFactory method newForMethod.
MutableMetric newForMethod(Object source, Method method, Metric annotation, MetricsRegistry registry) {
if (LOG.isDebugEnabled()) {
LOG.debug("method " + method + " with annotation " + annotation);
}
MetricsInfo info = getInfo(annotation, method);
MutableMetric metric = newForMethod(source, method, annotation);
metric = metric != null ? metric : new MethodMetric(source, method, info, annotation.type());
registry.add(info.name(), metric);
return metric;
}
use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.
the class MutableMetricsFactory method newForField.
MutableMetric newForField(Field field, Metric annotation, MetricsRegistry registry) {
if (LOG.isDebugEnabled()) {
LOG.debug("field " + field + " with annotation " + annotation);
}
MetricsInfo info = getInfo(annotation, field);
MutableMetric metric = newForField(field, annotation);
if (metric != null) {
registry.add(info.name(), metric);
return metric;
}
final Class<?> cls = field.getType();
if (cls == MutableCounterInt.class) {
return registry.newCounter(info, 0);
}
if (cls == MutableCounterLong.class) {
return registry.newCounter(info, 0L);
}
if (cls == MutableGaugeInt.class) {
return registry.newGauge(info, 0);
}
if (cls == MutableGaugeLong.class) {
return registry.newGauge(info, 0L);
}
if (cls == MutableRate.class) {
return registry.newRate(info.name(), info.description(), annotation.always());
}
if (cls == MutableRates.class) {
return new MutableRates(registry);
}
if (cls == MutableRatesWithAggregation.class) {
return registry.newRatesWithAggregation(info.name());
}
if (cls == MutableStat.class) {
return registry.newStat(info.name(), info.description(), annotation.sampleName(), annotation.valueName(), annotation.always());
}
throw new MetricsException("Unsupported metric field " + field.getName() + " of type " + field.getType().getName());
}
Aggregations