diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/CompositeRegistry.java b/spectator-api/src/main/java/com/netflix/spectator/api/CompositeRegistry.java index 2e0a3b1a8..a6ed3be5c 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/CompositeRegistry.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/CompositeRegistry.java @@ -39,6 +39,7 @@ final class CompositeRegistry implements Registry { * Find the first registry in the composite that is an instance of {@code c}. If no match is * found then null will be returned. */ + @SuppressWarnings("unchecked") T find(Class c) { for (Registry r : registries) { if (c.isAssignableFrom(r.getClass())) { diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/ExtendedRegistry.java b/spectator-api/src/main/java/com/netflix/spectator/api/ExtendedRegistry.java index 6643b7724..57cb31336 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/ExtendedRegistry.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/ExtendedRegistry.java @@ -16,22 +16,20 @@ package com.netflix.spectator.api; import com.netflix.spectator.impl.Preconditions; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.lang.reflect.Method; -import java.util.Collection; import java.util.Iterator; -import java.util.Map; /** * Wraps a registry and provides additional helper methods to make it easier to use. + * + * @deprecated This class was used prior to java 8 for adding extension methods to the registry + * without breaking all classes implementing the interface. The extension methods have now been + * moved to Registry interface as default methods. */ +@Deprecated public final class ExtendedRegistry implements Registry { - private static final Logger LOGGER = LoggerFactory.getLogger(ExtendedRegistry.class); - private final Registry impl; /** Create a new instance. */ @@ -44,19 +42,6 @@ public Registry underlying() { return impl; } - /** - * Returns the first underlying registry that is an instance of {@code c}. - */ - public T underlying(Class c) { - if (c.isAssignableFrom(impl.getClass())) { - return (T) impl; - } else if (impl instanceof CompositeRegistry) { - return ((CompositeRegistry) impl).find(c); - } else { - return null; - } - } - @Override public Clock clock() { return impl.clock(); } @@ -96,449 +81,6 @@ public T underlying(Class c) { ///////////////////////////////////////////////////////////////// // Additional helper methods below - private Iterable toIterable(String[] tags) { - if (tags.length % 2 == 1) { - throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); - } - TagList ts = TagList.EMPTY; - for (int i = 0; i < tags.length; i += 2) { - ts = new TagList(tags[i], tags[i + 1], ts); - } - return ts; - } - - /** - * Creates an identifier for a meter. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Identifier for a meter. - */ - public Id createId(String name, String... tags) { - return impl.createId(name, toIterable(tags)); - } - - /** - * Creates an identifier for a meter. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Identifier for a meter. - */ - public Id createId(String name, Map tags) { - return impl.createId(name).withTags(TagList.create(tags)); - } - - /** - * Measures the rate of some activity. - * - * @param name - * Description of the measurement that is being collected. - * @return - * Counter instance with the corresponding id. - */ - public Counter counter(String name) { - return impl.counter(impl.createId(name)); - } - - /** - * Measures the rate of some activity. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Counter instance with the corresponding id. - */ - public Counter counter(String name, Iterable tags) { - return impl.counter(impl.createId(name, tags)); - } - - /** - * Measures the rate of some activity. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Counter instance with the corresponding id. - */ - public Counter counter(String name, String... tags) { - return impl.counter(impl.createId(name, toIterable(tags))); - } - - /** - * Measures the sample distribution of events. - * - * @param name - * Description of the measurement that is being collected. - * @return - * Summary instance with the corresponding id. - */ - public DistributionSummary distributionSummary(String name) { - return impl.distributionSummary(impl.createId(name)); - } - - /** - * Measures the sample distribution of events. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Summary instance with the corresponding id. - */ - public DistributionSummary distributionSummary(String name, Iterable tags) { - return impl.distributionSummary(impl.createId(name, tags)); - } - - /** - * Measures the sample distribution of events. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Summary instance with the corresponding id. - */ - public DistributionSummary distributionSummary(String name, String... tags) { - return impl.distributionSummary(impl.createId(name, toIterable(tags))); - } - - /** - * Measures the time taken for short tasks. - * - * @param name - * Description of the measurement that is being collected. - * @return - * Timer instance with the corresponding id. - */ - public Timer timer(String name) { - return impl.timer(impl.createId(name)); - } - - /** - * Measures the time taken for short tasks. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Timer instance with the corresponding id. - */ - public Timer timer(String name, Iterable tags) { - return impl.timer(impl.createId(name, tags)); - } - - /** - * Measures the time taken for short tasks. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Timer instance with the corresponding id. - */ - public Timer timer(String name, String... tags) { - return impl.timer(impl.createId(name, toIterable(tags))); - } - - /** - * Measures the time taken for long tasks. - * - * @param id - * Identifier for the metric being registered. - * @return - * Timer instance with the corresponding id. - */ - public LongTaskTimer longTaskTimer(Id id) { - LongTaskTimer taskTimer = new DefaultLongTaskTimer(clock(), id); - impl.register(taskTimer); // the AggrMeter has the right semantics for these type of timers - return taskTimer; - } - - /** - * Measures the time taken for long tasks. - * - * @param name - * Description of the measurement that is being collected. - * @return - * Timer instance with the corresponding id. - */ - public LongTaskTimer longTaskTimer(String name) { - return longTaskTimer(createId(name)); - } - - /** - * Measures the time taken for long tasks. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Timer instance with the corresponding id. - */ - public LongTaskTimer longTaskTimer(String name, Iterable tags) { - return longTaskTimer(createId(name, tags)); - } - - /** - * Measures the time taken for long tasks. - * - * @param name - * Description of the measurement that is being collected. - * @param tags - * Other dimensions that can be used to classify the measurement. - * @return - * Timer instance with the corresponding id. - */ - public LongTaskTimer longTaskTimer(String name, String... tags) { - return longTaskTimer(createId(name, toIterable(tags))); - } - - /** - * Register a gauge that reports the value of the {@link java.lang.Number}. The registration - * will keep a weak reference to the number so it will not prevent garbage collection. - * The number implementation used should be thread safe. - * - * @param id - * Identifier for the metric being registered. - * @param number - * Thread-safe implementation of {@link Number} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public T gauge(Id id, T number) { - return gauge(id, number, Functions.IDENTITY); - } - - /** - * Register a gauge that reports the value of the {@link java.lang.Number}. See - * {@link #gauge(Id, Number)}. - * - * @param name - * Name of the metric being registered. - * @param number - * Thread-safe implementation of {@link Number} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public T gauge(String name, T number) { - return gauge(impl.createId(name), number); - } - - /** - * Register a gauge that reports the value of the {@link java.lang.Number}. See - * {@link #gauge(Id, Number)}. - * - * @param name - * Name of the metric being registered. - * @param tags - * Sequence of dimensions for breaking down the name. - * @param number - * Thread-safe implementation of {@link Number} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public T gauge(String name, Iterable tags, T number) { - return gauge(impl.createId(name, tags), number); - } - - /** - * Register a gauge that reports the value of the object after the function - * {@code f} is applied. The registration will keep a weak reference to the number so it will - * not prevent garbage collection. The number implementation used should be thread safe. - * - * @param id - * Identifier for the metric being registered. - * @param obj - * Object used to compute a value. - * @param f - * Function that is applied on the value for the number. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public T gauge(Id id, T obj, ValueFunction f) { - impl.register(new ObjectGauge(clock(), id, obj, f)); - return obj; - } - - /** - * Register a gauge that reports the value of the object. See - * {@link #gauge(Id, Object, ValueFunction)}. - * - * @param name - * Name of the metric being registered. - * @param obj - * Object used to compute a value. - * @param f - * Function that is applied on the value for the number. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public T gauge(String name, T obj, ValueFunction f) { - return gauge(impl.createId(name), obj, f); - } - - /** - * Register a gauge that reports the size of the {@link java.util.Collection}. The registration - * will keep a weak reference to the collection so it will not prevent garbage collection. - * The collection implementation used should be thread safe. Note that calling - * {@link java.util.Collection#size()} can be expensive for some collection implementations - * and should be considered before registering. - * - * @param id - * Identifier for the metric being registered. - * @param collection - * Thread-safe implementation of {@link Collection} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public > T collectionSize(Id id, T collection) { - return gauge(id, collection, Functions.COLLECTION_SIZE); - } - - /** - * Register a gauge that reports the size of the {@link java.util.Collection}. The registration - * will keep a weak reference to the collection so it will not prevent garbage collection. - * The collection implementation used should be thread safe. Note that calling - * {@link java.util.Collection#size()} can be expensive for some collection implementations - * and should be considered before registering. - * - * @param name - * Name of the metric being registered. - * @param collection - * Thread-safe implementation of {@link Collection} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public > T collectionSize(String name, T collection) { - return collectionSize(impl.createId(name), collection); - } - - /** - * Register a gauge that reports the size of the {@link java.util.Map}. The registration - * will keep a weak reference to the collection so it will not prevent garbage collection. - * The collection implementation used should be thread safe. Note that calling - * {@link java.util.Map#size()} can be expensive for some collection implementations - * and should be considered before registering. - * - * @param id - * Identifier for the metric being registered. - * @param collection - * Thread-safe implementation of {@link Map} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public > T mapSize(Id id, T collection) { - return gauge(id, collection, Functions.MAP_SIZE); - } - - /** - * Register a gauge that reports the size of the {@link java.util.Map}. The registration - * will keep a weak reference to the collection so it will not prevent garbage collection. - * The collection implementation used should be thread safe. Note that calling - * {@link java.util.Map#size()} can be expensive for some collection implementations - * and should be considered before registering. - * - * @param name - * Name of the metric being registered. - * @param collection - * Thread-safe implementation of {@link Map} used to access the value. - * @return - * The number that was passed in so the registration can be done as part of an assignment - * statement. - */ - public > T mapSize(String name, T collection) { - return mapSize(impl.createId(name), collection); - } - - /** - * Register a gauge that reports the return value of invoking the method on the object. The - * registration will keep a weak reference to the object so it will not prevent garbage - * collection. The registered method should be thread safe and cheap to invoke. Any potentially - * long running or expensive activity such as IO should not be performed inline. - * - * @param id - * Identifier for the metric being registered. - * @param obj - * Object used to compute a value. - * @param method - * Name of the method to invoke on the object. - */ - public void methodValue(Id id, Object obj, String method) { - try { - final Method m = getMethod(obj.getClass(), method); - try { - // Make sure we can cast the response to a Number - final Number n = (Number) m.invoke(obj); - LOGGER.debug("registering gauge {}, using method [{}], with initial value {}", id, m, n); - gauge(id, obj, Functions.invokeMethod(m)); - } catch (Exception e) { - final String msg = "exception thrown invoking method [" + m - + "], skipping registration of gauge " + id; - Throwables.propagate(msg, e); - } - } catch (NoSuchMethodException e) { - final String mname = obj.getClass().getName() + "." + method; - final String msg = "invalid method [" + mname + "], skipping registration of gauge " + id; - Throwables.propagate(msg, e); - } - } - - /** - * Register a gauge that reports the return value of invoking the method on the object. The - * registration will keep a weak reference to the object so it will not prevent garbage - * collection. The registered method should be thread safe and cheap to invoke. Any potentially - * long running or expensive activity such as IO should not be performed inline. - * - * @param name - * Name of the metric being registered. - * @param obj - * Object used to compute a value. - * @param method - * Name of the method to invoke on the object. - */ - public void methodValue(String name, Object obj, String method) { - methodValue(impl.createId(name), obj, method); - } - - /** Search for a method in the class and all superclasses. */ - Method getMethod(Class cls, String name) throws NoSuchMethodException { - NoSuchMethodException firstExc = null; - for (Class c = cls; c != null; c = c.getSuperclass()) { - try { - return c.getDeclaredMethod(name); - } catch (NoSuchMethodException e) { - if (firstExc == null) { - firstExc = e; - } - } - } - throw firstExc; - } - @Override public String toString() { return "ExtendedRegistry(impl=" + impl + ')'; diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/Registry.java b/spectator-api/src/main/java/com/netflix/spectator/api/Registry.java index ffdb720df..af57a0e3a 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/Registry.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/Registry.java @@ -15,7 +15,10 @@ */ package com.netflix.spectator.api; +import java.lang.reflect.Method; +import java.util.Collection; import java.util.Iterator; +import java.util.Map; /** * Registry to manage a set of meters. @@ -90,4 +93,425 @@ public interface Registry extends Iterable { /** Iterator for traversing the set of meters in the registry. */ Iterator iterator(); + + ///////////////////////////////////////////////////////////////// + // Additional helper methods below + + /** + * Returns the first underlying registry that is an instance of {@code c}. + */ + @SuppressWarnings("unchecked") + default T underlying(Class c) { + if (c.isAssignableFrom(getClass())) { + return (T) this; + } else if (this instanceof CompositeRegistry) { + return ((CompositeRegistry) this).find(c); + } else { + return null; + } + } + + /** + * Creates an identifier for a meter. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Identifier for a meter. + */ + default Id createId(String name, String... tags) { + return createId(name, Utils.toIterable(tags)); + } + + /** + * Creates an identifier for a meter. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Identifier for a meter. + */ + default Id createId(String name, Map tags) { + return createId(name).withTags(TagList.create(tags)); + } + + /** + * Measures the rate of some activity. + * + * @param name + * Description of the measurement that is being collected. + * @return + * Counter instance with the corresponding id. + */ + default Counter counter(String name) { + return counter(createId(name)); + } + + /** + * Measures the rate of some activity. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Counter instance with the corresponding id. + */ + default Counter counter(String name, Iterable tags) { + return counter(createId(name, tags)); + } + + /** + * Measures the rate of some activity. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Counter instance with the corresponding id. + */ + default Counter counter(String name, String... tags) { + return counter(createId(name, Utils.toIterable(tags))); + } + + /** + * Measures the sample distribution of events. + * + * @param name + * Description of the measurement that is being collected. + * @return + * Summary instance with the corresponding id. + */ + default DistributionSummary distributionSummary(String name) { + return distributionSummary(createId(name)); + } + + /** + * Measures the sample distribution of events. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Summary instance with the corresponding id. + */ + default DistributionSummary distributionSummary(String name, Iterable tags) { + return distributionSummary(createId(name, tags)); + } + + /** + * Measures the sample distribution of events. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Summary instance with the corresponding id. + */ + default DistributionSummary distributionSummary(String name, String... tags) { + return distributionSummary(createId(name, Utils.toIterable(tags))); + } + + /** + * Measures the time taken for short tasks. + * + * @param name + * Description of the measurement that is being collected. + * @return + * Timer instance with the corresponding id. + */ + default Timer timer(String name) { + return timer(createId(name)); + } + + /** + * Measures the time taken for short tasks. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Timer instance with the corresponding id. + */ + default Timer timer(String name, Iterable tags) { + return timer(createId(name, tags)); + } + + /** + * Measures the time taken for short tasks. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Timer instance with the corresponding id. + */ + default Timer timer(String name, String... tags) { + return timer(createId(name, Utils.toIterable(tags))); + } + + /** + * Measures the time taken for long tasks. + * + * @param id + * Identifier for the metric being registered. + * @return + * Timer instance with the corresponding id. + */ + default LongTaskTimer longTaskTimer(Id id) { + LongTaskTimer taskTimer = new DefaultLongTaskTimer(clock(), id); + register(taskTimer); // the AggrMeter has the right semantics for these type of timers + return taskTimer; + } + + /** + * Measures the time taken for long tasks. + * + * @param name + * Description of the measurement that is being collected. + * @return + * Timer instance with the corresponding id. + */ + default LongTaskTimer longTaskTimer(String name) { + return longTaskTimer(createId(name)); + } + + /** + * Measures the time taken for long tasks. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Timer instance with the corresponding id. + */ + default LongTaskTimer longTaskTimer(String name, Iterable tags) { + return longTaskTimer(createId(name, tags)); + } + + /** + * Measures the time taken for long tasks. + * + * @param name + * Description of the measurement that is being collected. + * @param tags + * Other dimensions that can be used to classify the measurement. + * @return + * Timer instance with the corresponding id. + */ + default LongTaskTimer longTaskTimer(String name, String... tags) { + return longTaskTimer(createId(name, Utils.toIterable(tags))); + } + + /** + * Register a gauge that reports the value of the {@link java.lang.Number}. The registration + * will keep a weak reference to the number so it will not prevent garbage collection. + * The number implementation used should be thread safe. + * + * @param id + * Identifier for the metric being registered. + * @param number + * Thread-safe implementation of {@link Number} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default T gauge(Id id, T number) { + return gauge(id, number, Functions.IDENTITY); + } + + /** + * Register a gauge that reports the value of the {@link java.lang.Number}. See + * {@link #gauge(Id, Number)}. + * + * @param name + * Name of the metric being registered. + * @param number + * Thread-safe implementation of {@link Number} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default T gauge(String name, T number) { + return gauge(createId(name), number); + } + + /** + * Register a gauge that reports the value of the {@link java.lang.Number}. See + * {@link #gauge(Id, Number)}. + * + * @param name + * Name of the metric being registered. + * @param tags + * Sequence of dimensions for breaking down the name. + * @param number + * Thread-safe implementation of {@link Number} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default T gauge(String name, Iterable tags, T number) { + return gauge(createId(name, tags), number); + } + + /** + * Register a gauge that reports the value of the object after the function + * {@code f} is applied. The registration will keep a weak reference to the number so it will + * not prevent garbage collection. The number implementation used should be thread safe. + * + * @param id + * Identifier for the metric being registered. + * @param obj + * Object used to compute a value. + * @param f + * Function that is applied on the value for the number. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default T gauge(Id id, T obj, ValueFunction f) { + register(new ObjectGauge(clock(), id, obj, f)); + return obj; + } + + /** + * Register a gauge that reports the value of the object. See + * {@link #gauge(Id, Object, ValueFunction)}. + * + * @param name + * Name of the metric being registered. + * @param obj + * Object used to compute a value. + * @param f + * Function that is applied on the value for the number. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default T gauge(String name, T obj, ValueFunction f) { + return gauge(createId(name), obj, f); + } + + /** + * Register a gauge that reports the size of the {@link java.util.Collection}. The registration + * will keep a weak reference to the collection so it will not prevent garbage collection. + * The collection implementation used should be thread safe. Note that calling + * {@link java.util.Collection#size()} can be expensive for some collection implementations + * and should be considered before registering. + * + * @param id + * Identifier for the metric being registered. + * @param collection + * Thread-safe implementation of {@link Collection} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default > T collectionSize(Id id, T collection) { + return gauge(id, collection, Functions.COLLECTION_SIZE); + } + + /** + * Register a gauge that reports the size of the {@link java.util.Collection}. The registration + * will keep a weak reference to the collection so it will not prevent garbage collection. + * The collection implementation used should be thread safe. Note that calling + * {@link java.util.Collection#size()} can be expensive for some collection implementations + * and should be considered before registering. + * + * @param name + * Name of the metric being registered. + * @param collection + * Thread-safe implementation of {@link Collection} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default > T collectionSize(String name, T collection) { + return collectionSize(createId(name), collection); + } + + /** + * Register a gauge that reports the size of the {@link java.util.Map}. The registration + * will keep a weak reference to the collection so it will not prevent garbage collection. + * The collection implementation used should be thread safe. Note that calling + * {@link java.util.Map#size()} can be expensive for some collection implementations + * and should be considered before registering. + * + * @param id + * Identifier for the metric being registered. + * @param collection + * Thread-safe implementation of {@link Map} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default > T mapSize(Id id, T collection) { + return gauge(id, collection, Functions.MAP_SIZE); + } + + /** + * Register a gauge that reports the size of the {@link java.util.Map}. The registration + * will keep a weak reference to the collection so it will not prevent garbage collection. + * The collection implementation used should be thread safe. Note that calling + * {@link java.util.Map#size()} can be expensive for some collection implementations + * and should be considered before registering. + * + * @param name + * Name of the metric being registered. + * @param collection + * Thread-safe implementation of {@link Map} used to access the value. + * @return + * The number that was passed in so the registration can be done as part of an assignment + * statement. + */ + default > T mapSize(String name, T collection) { + return mapSize(createId(name), collection); + } + + /** + * Register a gauge that reports the return value of invoking the method on the object. The + * registration will keep a weak reference to the object so it will not prevent garbage + * collection. The registered method should be thread safe and cheap to invoke. Any potentially + * long running or expensive activity such as IO should not be performed inline. + * + * @param id + * Identifier for the metric being registered. + * @param obj + * Object used to compute a value. + * @param method + * Name of the method to invoke on the object. + */ + default void methodValue(Id id, Object obj, String method) { + final Method m = Utils.getGaugeMethod(id, obj, method); + if (m != null) { + gauge(id, obj, Functions.invokeMethod(m)); + } + } + + /** + * Register a gauge that reports the return value of invoking the method on the object. The + * registration will keep a weak reference to the object so it will not prevent garbage + * collection. The registered method should be thread safe and cheap to invoke. Any potentially + * long running or expensive activity such as IO should not be performed inline. + * + * @param name + * Name of the metric being registered. + * @param obj + * Object used to compute a value. + * @param method + * Name of the method to invoke on the object. + */ + default void methodValue(String name, Object obj, String method) { + methodValue(createId(name), obj, method); + } } diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/Utils.java b/spectator-api/src/main/java/com/netflix/spectator/api/Utils.java index a5b61303d..4b59fbb7c 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/Utils.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/Utils.java @@ -16,7 +16,10 @@ package com.netflix.spectator.api; import com.netflix.spectator.impl.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -25,9 +28,50 @@ * Helper functions for working with a sequence of measurements. */ public final class Utils { + + private static final Logger REGISTRY_LOGGER = LoggerFactory.getLogger(Registry.class); + private Utils() { } + /** Search for a method in the class and all superclasses. */ + static Method getMethod(Class cls, String name) throws NoSuchMethodException { + NoSuchMethodException firstExc = null; + for (Class c = cls; c != null; c = c.getSuperclass()) { + try { + return c.getDeclaredMethod(name); + } catch (NoSuchMethodException e) { + if (firstExc == null) { + firstExc = e; + } + } + } + throw firstExc; + } + + /** Return a method supplying a value for a gauge. */ + static Method getGaugeMethod(Id id, Object obj, String method) { + try { + final Method m = Utils.getMethod(obj.getClass(), method); + try { + // Make sure we can cast the response to a Number + final Number n = (Number) m.invoke(obj); + REGISTRY_LOGGER.debug( + "registering gauge {}, using method [{}], with initial value {}", id, m, n); + return m; + } catch (Exception e) { + final String msg = "exception thrown invoking method [" + m + + "], skipping registration of gauge " + id; + Throwables.propagate(msg, e); + } + } catch (NoSuchMethodException e) { + final String mname = obj.getClass().getName() + "." + method; + final String msg = "invalid method [" + mname + "], skipping registration of gauge " + id; + Throwables.propagate(msg, e); + } + return null; + } + /** * Returns a new id with the tag list sorted by key and with no duplicate keys. */ @@ -197,4 +241,18 @@ public static List toList(Iterator iter) { } return buf; } + + /** + * Returns an iterable of tags based on a string array. + */ + static Iterable toIterable(String[] tags) { + if (tags.length % 2 == 1) { + throw new IllegalArgumentException("size must be even, it is a set of key=value pairs"); + } + TagList ts = TagList.EMPTY; + for (int i = 0; i < tags.length; i += 2) { + ts = new TagList(tags[i], tags[i + 1], ts); + } + return ts; + } } diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/DefaultRegistryTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/DefaultRegistryTest.java index d4dab90f5..182b92fb9 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/DefaultRegistryTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/DefaultRegistryTest.java @@ -201,7 +201,7 @@ private double sum(Registry r) { public void testMaxLimitExceededRegister() { System.setProperty("spectator.api.maxNumberOfMeters", "1"); final AtomicInteger one = new AtomicInteger(1); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry(clock)); + Registry r = new DefaultRegistry(clock); Assert.assertEquals(sum(r), 0.0, 1e-12); r.gauge(r.createId("c1"), one); diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/ExtendedRegistryTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/ExtendedRegistryTest.java index 321db2189..dd7189ad0 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/ExtendedRegistryTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/ExtendedRegistryTest.java @@ -38,7 +38,7 @@ public void init() { @Test public void testCreateIdArray() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Id id1 = r.createId("foo", "bar", "baz", "k", "v"); Id id2 = r.createId("foo", new TagList("k", "v", new TagList("bar", "baz"))); Assert.assertEquals(id1, id2); @@ -46,7 +46,7 @@ public void testCreateIdArray() { @Test(expected = IllegalArgumentException.class) public void testCreateIdArrayOdd() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); r.createId("foo", "bar", "baz", "k"); } @@ -55,7 +55,7 @@ public void testCreateIdMap() { Map map = new LinkedHashMap<>(); map.put("k1", "v1"); map.put("k2", "v2"); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Id id1 = r.createId("foo", map); Id id2 = r.createId("foo", "k1", "v1", "k2", "v2"); Assert.assertEquals(id1, id2); @@ -63,7 +63,7 @@ public void testCreateIdMap() { @Test public void testCounterHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Counter c1 = r.counter("foo", "bar", "baz", "k", "v"); Counter c2 = r.counter("foo", new TagList("k", "v", new TagList("bar", "baz"))); Counter c3 = r.counter("foo"); @@ -73,7 +73,7 @@ public void testCounterHelpers() { @Test public void testDistributionSummaryHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); DistributionSummary c1 = r.distributionSummary("foo", "bar", "baz", "k", "v"); DistributionSummary c2 = r.distributionSummary("foo", new TagList("k", "v", new TagList("bar", "baz"))); @@ -84,7 +84,7 @@ public void testDistributionSummaryHelpers() { @Test public void testTimerHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Timer c1 = r.timer("foo", "bar", "baz", "k", "v"); Timer c2 = r.timer("foo", new TagList("k", "v", new TagList("bar", "baz"))); Timer c3 = r.timer("foo"); @@ -95,7 +95,7 @@ public void testTimerHelpers() { @Test public void testLongTaskTimerHelpers() { ManualClock clock = new ManualClock(); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry(clock)); + Registry r = new DefaultRegistry(clock); LongTaskTimer c1 = r.longTaskTimer("foo", "bar", "baz", "k", "v"); Meter m1 = r.get(c1.id()); Assert.assertEquals(c1.id(), m1.id()); // registration @@ -121,7 +121,7 @@ public void testGaugeHelpers() { AtomicLong al1 = new AtomicLong(1L); AtomicLong al2 = new AtomicLong(2L); AtomicLong al4 = new AtomicLong(4L); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); AtomicLong v1 = r.gauge(r.createId("foo", "bar", "baz", "k", "v"), al1); AtomicLong v2 = r.gauge("foo", new TagList("k", "v", new TagList("bar", "baz")), al2); AtomicLong v3 = r.gauge("foo", al4); @@ -137,7 +137,7 @@ public void testGaugeHelpers() { @Test public void testGaugeHelpersWithFunction() { AtomicLong al1 = new AtomicLong(1L); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry(new ManualClock(40, 0))); + Registry r = new DefaultRegistry(new ManualClock(40, 0)); DoubleFunction f = Functions.age(r.clock()); AtomicLong v1 = r.gauge("foo", al1, f); Assert.assertSame(v1, al1); @@ -147,7 +147,7 @@ public void testGaugeHelpersWithFunction() { @Test public void testCollectionSizeHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); LinkedBlockingDeque q1 = new LinkedBlockingDeque<>(); LinkedBlockingDeque q2 = r.collectionSize("queueSize", q1); Assert.assertSame(q1, q2); @@ -159,7 +159,7 @@ public void testCollectionSizeHelpers() { @Test public void testMapSizeHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); ConcurrentHashMap q1 = new ConcurrentHashMap<>(); ConcurrentHashMap q2 = r.mapSize("mapSize", q1); Assert.assertSame(q1, q2); @@ -171,7 +171,7 @@ public void testMapSizeHelpers() { @Test public void testMethodValueHelpers() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); LinkedBlockingDeque q1 = new LinkedBlockingDeque<>(); r.methodValue("queueSize", q1, "size"); Id id = r.createId("queueSize"); @@ -183,14 +183,14 @@ public void testMethodValueHelpers() { @Test(expected = ClassCastException.class) public void methodValueBadReturnType() { System.setProperty("spectator.api.propagateWarnings", "true"); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); r.methodValue("queueSize", this, "toString"); } @Test public void methodValueBadReturnTypeNoPropagate() { System.setProperty("spectator.api.propagateWarnings", "false"); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); r.methodValue("queueSize", this, "toString"); Assert.assertNull(r.get(r.createId("queueSize"))); } @@ -198,14 +198,14 @@ public void methodValueBadReturnTypeNoPropagate() { @Test(expected = RuntimeException.class) public void methodValueUnknown() { System.setProperty("spectator.api.propagateWarnings", "true"); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); r.methodValue("queueSize", this, "unknownMethod"); } @Test public void methodValueUnknownNoPropagate() { System.setProperty("spectator.api.propagateWarnings", "false"); - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); r.methodValue("queueSize", this, "unknownMethod"); Assert.assertNull(r.get(r.createId("queueSize"))); } diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/FunctionsTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/FunctionsTest.java index b5d96d9a5..d0210822c 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/FunctionsTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/FunctionsTest.java @@ -24,7 +24,7 @@ public class FunctionsTest { private final ManualClock clock = new ManualClock(); - private final ExtendedRegistry registry = new ExtendedRegistry(new DefaultRegistry()); + private final Registry registry = new DefaultRegistry(); @Test public void ageFunction() { @@ -39,7 +39,7 @@ private byte byteMethod() { @Test public void invokeMethodByte() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(getClass(), "byteMethod")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(getClass(), "byteMethod")); Assert.assertEquals(f.apply(this), 1.0, 1e-12); } @@ -49,7 +49,7 @@ private short shortMethod() { @Test public void invokeMethodShort() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(getClass(), "shortMethod")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(getClass(), "shortMethod")); Assert.assertEquals(f.apply(this), 2.0, 1e-12); } @@ -59,7 +59,7 @@ private int intMethod() { @Test public void invokeMethodInt() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(getClass(), "intMethod")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(getClass(), "intMethod")); Assert.assertEquals(f.apply(this), 3.0, 1e-12); } @@ -69,7 +69,7 @@ private long longMethod() { @Test public void invokeMethodLong() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(getClass(), "longMethod")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(getClass(), "longMethod")); Assert.assertEquals(f.apply(this), 4.0, 1e-12); } @@ -80,7 +80,7 @@ private Long wrapperLongMethod() { @Test public void invokeMethodWrapperLong() throws Exception { final ValueFunction f = Functions.invokeMethod( - registry.getMethod(getClass(), "wrapperLongMethod")); + Utils.getMethod(getClass(), "wrapperLongMethod")); Assert.assertEquals(f.apply(this), 5.0, 1e-12); } @@ -90,30 +90,30 @@ private Long throwsMethod() { @Test public void invokeBadMethod() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(getClass(), "throwsMethod")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(getClass(), "throwsMethod")); Assert.assertEquals(f.apply(this), Double.NaN, 1e-12); } @Test(expected = NoSuchMethodException.class) public void invokeNoSuchMethod() throws Exception { - Functions.invokeMethod(registry.getMethod(getClass(), "unknownMethod")); + Functions.invokeMethod(Utils.getMethod(getClass(), "unknownMethod")); } @Test public void invokeOnSubclass() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(B.class, "two")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(B.class, "two")); Assert.assertEquals(f.apply(new B()), 2.0, 1e-12); } @Test public void invokeOneA() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(A.class, "one")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(A.class, "one")); Assert.assertEquals(f.apply(new A()), 1.0, 1e-12); } @Test public void invokeOneB() throws Exception { - final ValueFunction f = Functions.invokeMethod(registry.getMethod(B.class, "one")); + final ValueFunction f = Functions.invokeMethod(Utils.getMethod(B.class, "one")); Assert.assertEquals(f.apply(new B()), -1.0, 1e-12); } diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/UtilsTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/UtilsTest.java index 04db8e24b..ad4c40868 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/UtilsTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/UtilsTest.java @@ -55,7 +55,7 @@ public boolean apply(String value) { }; private List newList(int size) { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); List data = new ArrayList<>(); for (int i = 0; i < size; ++i) { data.add(new Measurement(r.createId("foo", "i", "" + i), 0L, i)); @@ -65,14 +65,14 @@ private List newList(int size) { @Test public void getTagValueIdNoTags() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Id id = r.createId("foo"); Assert.assertEquals(null, Utils.getTagValue(id, "abc")); } @Test public void getTagValueId() { - ExtendedRegistry r = new ExtendedRegistry(new DefaultRegistry()); + Registry r = new DefaultRegistry(); Id id = r.createId("foo", "bar", "baz", "abc", "def"); Assert.assertEquals("def", Utils.getTagValue(id, "abc")); Assert.assertEquals("baz", Utils.getTagValue(id, "bar")); diff --git a/spectator-ext-log4j2/src/main/java/com/netflix/spectator/log4j/SpectatorAppender.java b/spectator-ext-log4j2/src/main/java/com/netflix/spectator/log4j/SpectatorAppender.java index 40dfde838..2a33e654f 100644 --- a/spectator-ext-log4j2/src/main/java/com/netflix/spectator/log4j/SpectatorAppender.java +++ b/spectator-ext-log4j2/src/main/java/com/netflix/spectator/log4j/SpectatorAppender.java @@ -15,8 +15,8 @@ */ package com.netflix.spectator.log4j; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Id; +import com.netflix.spectator.api.Registry; import com.netflix.spectator.api.Spectator; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; @@ -65,7 +65,7 @@ private static void addToRootLogger(final Appender appender) { * If set to true then the stack trace metrics are disabled. */ public static void addToRootLogger( - ExtendedRegistry registry, + Registry registry, String name, boolean ignoreExceptions) { final Appender appender = new SpectatorAppender(registry, name, null, null, ignoreExceptions); @@ -84,13 +84,13 @@ public static void addToRootLogger( private static final long serialVersionUID = 42L; - private final transient ExtendedRegistry registry; + private final transient Registry registry; private transient Id[] numMessages; private transient Id[] numStackTraces; /** Create a new instance of the appender. */ SpectatorAppender( - ExtendedRegistry registry, + Registry registry, String name, Filter filter, Layout layout, diff --git a/spectator-ext-log4j2/src/test/java/com/netflix/spectator/log4j/SpectatorAppenderTest.java b/spectator-ext-log4j2/src/test/java/com/netflix/spectator/log4j/SpectatorAppenderTest.java index 465b7b3c3..833f095ce 100644 --- a/spectator-ext-log4j2/src/test/java/com/netflix/spectator/log4j/SpectatorAppenderTest.java +++ b/spectator-ext-log4j2/src/test/java/com/netflix/spectator/log4j/SpectatorAppenderTest.java @@ -17,7 +17,7 @@ import com.netflix.spectator.api.Counter; import com.netflix.spectator.api.DefaultRegistry; -import com.netflix.spectator.api.ExtendedRegistry; +import com.netflix.spectator.api.Registry; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.impl.Log4jLogEvent; @@ -30,7 +30,7 @@ @RunWith(JUnit4.class) public class SpectatorAppenderTest { - private ExtendedRegistry registry; + private Registry registry; private SpectatorAppender appender; private LogEvent newEvent(Level level, Throwable t) { @@ -45,7 +45,7 @@ private LogEvent newEvent(Level level, Throwable t, boolean includeSource) { @Before public void before() { - registry = new ExtendedRegistry(new DefaultRegistry()); + registry = new DefaultRegistry(); appender = new SpectatorAppender(registry, "foo", null, null, false); appender.start(); } diff --git a/spectator-ext-sandbox/src/main/java/com/netflix/spectator/sandbox/HttpLogEntry.java b/spectator-ext-sandbox/src/main/java/com/netflix/spectator/sandbox/HttpLogEntry.java index 2725e49d6..1e630be9f 100644 --- a/spectator-ext-sandbox/src/main/java/com/netflix/spectator/sandbox/HttpLogEntry.java +++ b/spectator-ext-sandbox/src/main/java/com/netflix/spectator/sandbox/HttpLogEntry.java @@ -15,8 +15,8 @@ */ package com.netflix.spectator.sandbox; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Id; +import com.netflix.spectator.api.Registry; import com.netflix.spectator.api.Spectator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +42,7 @@ public class HttpLogEntry { private static final Marker CLIENT = MarkerFactory.getMarker("http-client"); private static final Marker SERVER = MarkerFactory.getMarker("http-server"); - private static final ExtendedRegistry REGISTRY = Spectator.registry(); + private static final Registry REGISTRY = Spectator.registry(); private static final Id COMPLETE = REGISTRY.createId("http.req.complete"); private static final Id ATTEMPT = REGISTRY.createId("http.req.attempt"); private static final Id REQ_HEADER_SIZE = REGISTRY.createId("http.req.headerSize"); diff --git a/spectator-ext-spark/src/main/java/com/netflix/spectator/spark/SpectatorReporter.java b/spectator-ext-spark/src/main/java/com/netflix/spectator/spark/SpectatorReporter.java index 61fc08793..42f1651d2 100644 --- a/spectator-ext-spark/src/main/java/com/netflix/spectator/spark/SpectatorReporter.java +++ b/spectator-ext-spark/src/main/java/com/netflix/spectator/spark/SpectatorReporter.java @@ -24,8 +24,8 @@ import com.codahale.metrics.ScheduledReporter; import com.codahale.metrics.Timer; import com.netflix.spectator.api.DistributionSummary; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Id; +import com.netflix.spectator.api.Registry; import com.netflix.spectator.api.Spectator; import com.netflix.spectator.impl.AtomicDouble; import org.slf4j.Logger; @@ -57,7 +57,7 @@ public static Builder forRegistry(MetricRegistry registry) { */ public static final class Builder { private final MetricRegistry registry; - private ExtendedRegistry spectatorRegistry; + private Registry spectatorRegistry; private NameFunction nameFunction = new NameFunction() { @Override public Id apply(String name) { return spectatorRegistry.createId(name); @@ -81,7 +81,7 @@ public static final class Builder { } /** Set the spectator registry to use. */ - public Builder withSpectatorRegistry(ExtendedRegistry r) { + public Builder withSpectatorRegistry(Registry r) { spectatorRegistry = r; return this; } @@ -114,7 +114,7 @@ public SpectatorReporter build() { } } - private final ExtendedRegistry spectatorRegistry; + private final Registry spectatorRegistry; private final NameFunction nameFunction; private final ValueFunction valueFunction; private final Pattern gaugeCounters; @@ -125,7 +125,7 @@ public SpectatorReporter build() { /** Create a new instance. */ SpectatorReporter( MetricRegistry metricRegistry, - ExtendedRegistry spectatorRegistry, + Registry spectatorRegistry, NameFunction nameFunction, ValueFunction valueFunction, Pattern gaugeCounters) { diff --git a/spectator-ext-spark/src/test/java/com/netflix/spectator/spark/SpectatorReporterTest.java b/spectator-ext-spark/src/test/java/com/netflix/spectator/spark/SpectatorReporterTest.java index 04b0debdb..de10da720 100644 --- a/spectator-ext-spark/src/test/java/com/netflix/spectator/spark/SpectatorReporterTest.java +++ b/spectator-ext-spark/src/test/java/com/netflix/spectator/spark/SpectatorReporterTest.java @@ -18,9 +18,9 @@ import com.codahale.metrics.Counter; import com.codahale.metrics.MetricRegistry; import com.netflix.spectator.api.DefaultRegistry; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Measurement; import com.netflix.spectator.api.Meter; +import com.netflix.spectator.api.Registry; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,7 +32,7 @@ public class SpectatorReporterTest { private final MetricRegistry metricsRegistry = new MetricRegistry(); - private final ExtendedRegistry registry = new ExtendedRegistry(new DefaultRegistry()); + private final Registry registry = new DefaultRegistry(); private final SpectatorReporter reporter = SpectatorReporter.forRegistry(metricsRegistry) .withSpectatorRegistry(registry) .withGaugeCounters(Pattern.compile("^gaugeCounter.*$")) diff --git a/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/ChronosGcEventListener.java b/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/ChronosGcEventListener.java index ff47638e6..e4a8d738f 100644 --- a/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/ChronosGcEventListener.java +++ b/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/ChronosGcEventListener.java @@ -19,8 +19,8 @@ import com.netflix.config.DynamicBooleanProperty; import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicStringProperty; +import com.netflix.spectator.api.Registry; import iep.com.netflix.iep.http.RxHttp; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Id; import com.netflix.spectator.api.Spectator; import com.netflix.spectator.gc.GcEvent; @@ -55,7 +55,7 @@ class ChronosGcEventListener implements GcEventListener { private final Logger logger = LoggerFactory.getLogger(getClass()); - private final ExtendedRegistry registry = Spectator.registry(); + private final Registry registry = Spectator.registry(); private final Id requestCount = registry.createId("spectator.gc.chronosPost"); private final ObjectMapper mapper = new ObjectMapper(); diff --git a/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/TestModule.java b/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/TestModule.java index 16305b852..fe4198afd 100644 --- a/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/TestModule.java +++ b/spectator-nflx-plugin/src/main/java/com/netflix/spectator/nflx/TestModule.java @@ -32,8 +32,8 @@ */ public final class TestModule extends AbstractModule { @Override protected void configure() { - final ExtendedRegistry registry = new ExtendedRegistry(new DefaultRegistry()); - bind(ExtendedRegistry.class).toInstance(registry); + final Registry registry = new DefaultRegistry(); + bind(ExtendedRegistry.class).toInstance(new ExtendedRegistry(registry)); bind(Registry.class).toInstance(registry); // Allows auto-plugin to come up without needing to get DiscoveryClient to work diff --git a/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/ChronosGcEventListenerTest.java b/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/ChronosGcEventListenerTest.java index f1c76fd95..0d4a1ae55 100644 --- a/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/ChronosGcEventListenerTest.java +++ b/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/ChronosGcEventListenerTest.java @@ -16,8 +16,8 @@ package com.netflix.spectator.nflx; import com.netflix.config.ConfigurationManager; +import com.netflix.spectator.api.Registry; import iep.com.netflix.iep.http.RxHttp; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Id; import com.netflix.spectator.api.Spectator; import com.netflix.spectator.gc.GcEvent; @@ -95,13 +95,13 @@ private ChronosGcEventListener newListener() { } private long reqCount(int status) { - ExtendedRegistry r = Spectator.registry(); + Registry r = Spectator.registry(); Id requests = r.createId("spectator.gc.chronosPost", "status", "" + status); return r.timer(requests).count(); } private long reqCount(String status) { - ExtendedRegistry r = Spectator.registry(); + Registry r = Spectator.registry(); Id requests = r.createId("spectator.gc.chronosPost", "status", status); return r.timer(requests).count(); } diff --git a/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/TestModuleTest.java b/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/TestModuleTest.java index a38e1cdcf..286467c84 100644 --- a/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/TestModuleTest.java +++ b/spectator-nflx-plugin/src/test/java/com/netflix/spectator/nflx/TestModuleTest.java @@ -19,7 +19,7 @@ import com.google.inject.Inject; import com.google.inject.Injector; import com.netflix.spectator.api.Counter; -import com.netflix.spectator.api.ExtendedRegistry; +import com.netflix.spectator.api.Registry; import com.netflix.spectator.api.Spectator; import org.junit.Assert; import org.junit.Before; @@ -34,7 +34,7 @@ public static class Foo { private final Counter counter; @Inject - public Foo(ExtendedRegistry registry) { + public Foo(Registry registry) { counter = registry.counter("foo.doSomething"); } @@ -52,7 +52,7 @@ public void setup() { @Test public void checkCount() { - ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class); + Registry r = injector.getInstance(Registry.class); Counter c = r.counter("foo.doSomething"); Assert.assertEquals(0, c.count()); @@ -63,7 +63,7 @@ public void checkCount() { @Test public void notGlobal() { - ExtendedRegistry r = injector.getInstance(ExtendedRegistry.class); + Registry r = injector.getInstance(Registry.class); Assert.assertNotSame(Spectator.registry(), r); } } diff --git a/spectator-reg-tdigest/src/main/java/com/netflix/spectator/tdigest/TDigestModule.java b/spectator-reg-tdigest/src/main/java/com/netflix/spectator/tdigest/TDigestModule.java index b09e99949..bf6e30b87 100644 --- a/spectator-reg-tdigest/src/main/java/com/netflix/spectator/tdigest/TDigestModule.java +++ b/spectator-reg-tdigest/src/main/java/com/netflix/spectator/tdigest/TDigestModule.java @@ -19,7 +19,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Inject; import com.google.inject.Provides; -import com.netflix.spectator.api.ExtendedRegistry; import com.netflix.spectator.api.Registry; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; @@ -43,7 +42,7 @@ private TDigestConfig providesDigestConfig(OptionalInjections opts) { @Provides @Singleton - private TDigestRegistry providesRegistry(ExtendedRegistry registry, TDigestConfig config) { + private TDigestRegistry providesRegistry(Registry registry, TDigestConfig config) { return new TDigestRegistry(registry, config); }