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

com.nike.riposte.metrics.MetricsCollector Maven / Gradle / Ivy

There is a newer version: 0.20.0
Show newest version
package com.nike.riposte.metrics;

import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * This interface allows code to be instrumented with timers, meters, and counters which are managed by a Metric
 * collection tool such as Dropwizard metrics.  This Interface is simpler than it looks.  It seems complicated because
 * There are so many different forms of closures in Java at which must be explicityly supported. At the end of the day
 * these calls all boil down to something like the following:  timed((arg) -> {...code that operates on 'arg'},
 * timerName);  or  timed(someObject:someMethod,arg,timerName)  while those patterns all look the
 * same in code there are many compile time permutations due to the number of arguments and return values.
 *
 * @author pevans
 */
public interface MetricsCollector {

    /**
     * Execute some code and note how long it ran
     *
     * @param r
     *     A runnable or closure with no arguments and no returns
     * @param timerName
     *     The name of the timer to update with the measurements
     */
    void timed(Runnable r, String timerName);

    /**
     * Execute some code and note how long it ran
     *
     * @param c
     *     A Callable to run
     * @param timerName
     *     The name of the timer to update with the measurements
     *
     * @return an instance of V
     */
     V timed(Callable c, String timerName) throws Exception;

    /**
     * Execute some code and note how long it ran
     *
     * @param f
     *     a Function to run.  aka a closure with one argument and a return value
     * @param arg
     *     The argument to the function
     * @param timerName
     *     The name of the timer to update
     *
     * @return The results of the function
     */
     R timed(Function f, T arg, String timerName);

    /**
     * Execute some code and note how long it ran
     *
     * @param c
     *     a Consumer to run.  aka a closure with one argument and no return value
     * @param arg
     *     The argument to the function
     * @param timerName
     *     The name of the timer to update
     */
     void timed(Consumer c, T arg, String timerName);

    /**
     * Execute some code and note how long it ran
     *
     * @param bc
     *     A BiConsumer to run.  aka a closure with two arguments and no return value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param timerName
     *     The name of the timer to update
     */
     void timed(BiConsumer bc, T arg1, U arg2, String timerName);

    /**
     * Execute some code and note how long it ran
     *
     * @param bf
     *     A BiFunction to run.  aka a closure with two arguments and a return value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param timerName
     *     The name of the timer to update
     *
     * @return The resutls of processing
     */
     R timed(BiFunction bf, T arg1, U arg2, String timerName);

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event.
     *
     * @param r
     *     A Runnable to run or a closure with no arguments and no returns
     * @param meterName
     *     The meter to update
     */
    default void metered(Runnable r, String meterName) {
        metered(r, meterName, 1L);
    }

    /**
     * Execute some code and update the specified event rate metric.
     *
     * @param r
     *     A Runnable to run or a closure with no arguments and no returns
     * @param meterName
     *     The meter to update
     * @param events
     *     The number of events that this invocation represents
     */
    void metered(Runnable r, String meterName, long events);

    /**
     * Execute some code and update the specified event rate metric
     *
     * @param c
     *     A Callable to execute
     * @param meterName
     *     The meter to update
     * @param events
     *     The number of events that this invocation represents
     *
     * @return The result of processing
     */
     V metered(Callable c, String meterName, long events) throws Exception;

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event
     *
     * @param c
     *     a Callable to execute
     * @param meterName
     *     The meter to update
     *
     * @return The result of processing
     */
    default  V metered(Callable c, String meterName) throws Exception {
        return metered(c, meterName, 1L);
    }

    /**
     * Execute some code and update the specified event rate metric
     *
     * @param f
     *     A Function to execute.  aka a closure with one argument and a returned value
     * @param arg
     *     The argument to the function
     * @param meterName
     *     The meter to update
     * @param events
     *     The number of events this invocation represents
     *
     * @return The result of processing
     */
     R metered(Function f, T arg, String meterName, long events);

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event
     *
     * @param f
     *     A Function to execute.  aka a closure with one argument and a returned value
     * @param arg
     *     The argument to the function
     * @param meterName
     *     The meter to update
     *
     * @return The result of processing
     */
    default  R metered(Function f, T arg, String meterName) {
        return metered(f, arg, meterName, 1L);
    }

    /**
     * Execute some code and update the specified event rate metric.
     *
     * @param c
     *     A Consumer to execute.  aka a closure with one argument and no returned value
     * @param arg
     *     The argument to the function
     * @param meterName
     *     The meter to update
     * @param events
     *     the number of events this invocation represents
     */
     void metered(Consumer c, T arg, String meterName, long events);

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event
     *
     * @param c
     *     A Consumer to execute.  aka a closure with one argument and no returned value
     * @param arg
     *     The argument to the function
     * @param meterName
     *     The meter to update
     */
    default  void metered(Consumer c, T arg, String meterName) {
        metered(c, arg, meterName, 1L);
    }

    /**
     * Execute some code and update the specified event rate metric.
     *
     * @param bc
     *     A BiConsumer to execute.  aka a closure with two arguments and no returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     the second argument
     * @param meterName
     *     The meter to update
     * @param events
     *     the number of events this invocation represents
     */
     void metered(BiConsumer bc, T arg1, U arg2, String meterName, long events);

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event
     *
     * @param bc
     *     A BiConsumer to execute.  aka a closure with two arguments and no returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     the second argument
     * @param meterName
     *     The meter to update
     */
    default  void metered(BiConsumer bc, T arg1, U arg2, String meterName) {
        metered(bc, arg1, arg2, meterName, 1L);
    }

    /**
     * Execute some code and update the specified event rate metric.
     *
     * @param bf
     *     A BiFunction to execute.  aka a closure with two arguments and one returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param meterName
     *     The meter to update
     * @param events
     *     the number of events this invocation represents
     */
     R metered(BiFunction bf, T arg1, U arg2, String meterName, long events);

    /**
     * Execute some code and update the specified event rate metric.  This invocation represents a single event
     *
     * @param bf
     *     A BiFunction to execute.  aka a closure with two arguments and one returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param meterName
     *     The meter to update
     */
    default  R metered(BiFunction bf, T arg1, U arg2, String meterName) {
        return metered(bf, arg1, arg2, meterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.  This invocation increments the counter by 1
     *
     * @param r
     *     A Runnable or closure with no args and no return values
     * @param counterName
     *     The name of the counter to update
     */
    default void counted(Runnable r, String counterName) {
        counted(r, counterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.
     *
     * @param r
     *     A Runnable or closure with no args and no return values
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     The amount by which to change the counter
     */
    void counted(Runnable r, String counterName, long delta);

    /**
     * Execute some code and upate the specified counter
     *
     * @param c
     *     A Callable to execute
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     The amount by which to change the counter
     *
     * @return The result of processing
     */
     V counted(Callable c, String counterName, long delta) throws Exception;

    /**
     * Execute some code and upate the specified counter.  This invocation increments the counter by 1
     *
     * @param c
     *     A Callable to execute
     * @param meterName
     *     The name of the meter to update
     *
     * @return The result of processing
     */
    default  V counted(Callable c, String meterName) throws Exception {
        return counted(c, meterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.
     *
     * @param f
     *     A Function to execute.  aka a closure with one argument and a returned value
     * @param arg
     *     The argument to the function
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     the amount by which to udpate the counter
     *
     * @return The result of processing
     */
     R counted(Function f, T arg, String counterName, long delta);

    /**
     * Execute some code and update the specified counter.  This invocation increments the counter by 1
     *
     * @param f
     *     A Function to execute.  aka a closure with one argument and a returned value
     * @param arg
     *     The argument to the function
     * @param counterName
     *     The name of the counter to update
     *
     * @return The result of processing
     */
    default  R counted(Function f, T arg, String counterName) {
        return counted(f, arg, counterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.
     *
     * @param c
     *     A Consumer to execute.  aka a closure with one argument and no returned value
     * @param arg
     *     The argument to the function
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     the amount by which to udpate the counter
     */
     void counted(Consumer c, T arg, String counterName, long delta);

    /**
     * Execute some code and update the specified counter.  This invocation increments the counter by 1
     *
     * @param c
     *     A Consumer to execute.  aka a closure with one argument and no returned value
     * @param arg
     *     The argument to the function
     * @param counterName
     *     The name of the counter to update
     */
    default  void counted(Consumer c, T arg, String counterName) {
        counted(c, arg, counterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.
     *
     * @param bc
     *     A BiConsumer to execute.  aka a closure with two arguments and no returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     the amount by which to udpate the counter
     */
     void counted(BiConsumer bc, T arg1, U arg2, String counterName, long delta);

    /**
     * Execute some code and update the specified counter.  The invocation increments the counter by 1
     *
     * @param bc
     *     A BiConsumer to execute.  aka a closure with two arguments and no returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param counterName
     *     The name of the counter to update
     */
    default  void counted(BiConsumer bc, T arg1, U arg2, String counterName) {
        counted(bc, arg1, arg2, counterName, 1L);
    }

    /**
     * Execute some code and update the specified counter.
     *
     * @param bf
     *     A BiFunction to execute.  aka a closure with two arguments and one returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param counterName
     *     The name of the counter to update
     * @param delta
     *     the amount by which to udpate the counter
     *
     * @return The result of processing
     */
     R counted(BiFunction bf, T arg1, U arg2, String counterName, long delta);

    /**
     * Execute some code and update the specified counter. This invocation increments the counter by 1
     *
     * @param bf
     *     A BiFunction to execute.  aka a closure with two arguments and one returned value
     * @param arg1
     *     The first argument
     * @param arg2
     *     The second argument
     * @param counterName
     *     The name of the counter to update
     *
     * @return The result of processing
     */
    default  R counted(BiFunction bf, T arg1, U arg2, String counterName) {
        return counted(bf, arg1, arg2, counterName, 1L);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy