Search in sources :

Example 11 with MetricsRegistry

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;
}
Also used : Field(java.lang.reflect.Field) Metrics(org.apache.hadoop.metrics2.annotation.Metrics) MetricsException(org.apache.hadoop.metrics2.MetricsException) Annotation(java.lang.annotation.Annotation)

Example 12 with MetricsRegistry

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;
}
Also used : MetricsInfo(org.apache.hadoop.metrics2.MetricsInfo)

Example 13 with MetricsRegistry

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());
}
Also used : MetricsInfo(org.apache.hadoop.metrics2.MetricsInfo) MetricsException(org.apache.hadoop.metrics2.MetricsException)

Aggregations

Test (org.junit.Test)9 MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)8 MetricsRegistry (org.apache.hadoop.metrics2.lib.MetricsRegistry)3 MetricsException (org.apache.hadoop.metrics2.MetricsException)2 MetricsInfo (org.apache.hadoop.metrics2.MetricsInfo)2 Quantile (org.apache.hadoop.metrics2.util.Quantile)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Configuration (org.apache.hadoop.conf.Configuration)1 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)1 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)1 MetricsVisitor (org.apache.hadoop.metrics2.MetricsVisitor)1 Metrics (org.apache.hadoop.metrics2.annotation.Metrics)1 DefaultMetricsSystem (org.apache.hadoop.metrics2.lib.DefaultMetricsSystem)1 MutableRatesWithAggregation (org.apache.hadoop.metrics2.lib.MutableRatesWithAggregation)1 FakeTimer (org.apache.hadoop.util.FakeTimer)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1