All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.functions.Metrics Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.functions;

import rx.functions.Func0;
import netflix.ocelli.stats.CKMSQuantiles;
import netflix.ocelli.stats.Quantiles;
import netflix.ocelli.util.SingleMetric;

/**
 * Utility class for creating common strategies for tracking specific types of metrics
 * 
 * @author elandau
 *
 */
public class Metrics {
    public static  Func0> memoizeFactory(final T value) {
        return new Func0>() {
            @Override
            public SingleMetric call() {
                return memoize(value);
            }
        };
    }
    
    /**
     * Return a predetermine constant value regardless of samples added.
     * @param value
     * @return
     */
    public static  SingleMetric memoize(final T value) {
        return new SingleMetric() {
            @Override
            public void add(T sample) {
            }

            @Override
            public void reset() {
            }

            @Override
            public T get() {
                return value;
            }
        };
    }
    
    public static Func0> quantileFactory(final double percentile) {
        return new Func0>() {
            @Override
            public SingleMetric call() {
                return quantile(percentile);
            }
        };
    }

    /**
     * Use the default CKMSQuantiles algorithm to track a specific percentile
     * @param percentile
     * @return
     */
    public static SingleMetric quantile(final double percentile) {
        return quantile(new CKMSQuantiles(new CKMSQuantiles.Quantile[]{new CKMSQuantiles.Quantile(percentile, 1)}), percentile);
    }
    
    /**
     * Use an externally provided Quantiles algorithm to track a single percentile.  Note that
     * quantiles may be shared and should track homogeneous operations.
     * 
     * @param quantiles
     * @param percentile
     * @return
     */
    public static SingleMetric quantile(final Quantiles quantiles, final double percentile) {
        return new SingleMetric() {
            @Override
            public void add(Long sample) {
                quantiles.insert(sample.intValue());
            }

            @Override
            public void reset() {
            }

            @Override
            public Long get() {
                return (long)quantiles.get(percentile);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy