diff --git a/README.md b/README.md index f173af7..ab94439 100644 --- a/README.md +++ b/README.md @@ -589,6 +589,62 @@ will match the type so if you define both a LongCounter and a DoubleCounter it w know to grab the LongCounter as it inherits from LongCollector. The following collectors are all in the `org.kairosdb.metrics4j.collectors.impl` package. +Here is an example of defining a LongCounter: +```hocon +metrics4j { + ... + collectors: { + myLongCounter: { #This is your name for the collector that you can reference when assigning the collector + _class: "org.kairosdb.metrics4j.collectors.impl.LongCounter" + reset: true #Many collectors have options that you can set to change behavior + } + } + ... +} +``` + +#### BagCollector +This collector does not do any aggregation. Whatever value was put into the collector +is reported using the time of the put or the Instant if one was provided. +BagCollector can collect Long, Double and String values. + +#### Chained Collectors +There is a chain collector for each type of data: ChainedDoubleCollector, +ChainedDurationCollector, ChainedLongCollector, ChainedStringCollector, ChainedTimeCollector + +The chain collector lets you put data into multiple collectors where each one is +reported. + +For example lets say you have a value that you want to both count and report +the max value from +```hocon +metrics4j { + ... + collectors: { + myLongCounter: { + _class: "org.kairosdb.metrics4j.collectors.impl.LongCounter" + } + myMax: { + _class: "org.kairosdb.metrics4j.collectors.impl.MaxLongGuage" + } + myChainCollector: { + _class: "org.kairosdb.metrics4j.collectors.impl.ChainedLongCollector" + collectors: ["myLongCounter", "myMax"] + prefixes: ["count.", "max."] #prefix to identify each metric + } + } + ... +} +``` + +The prefixes are applied to the collectors that are defined respectively. If the +metric being reported was `myMetric.value`, the chain collector would report two +values `myMetric.count.value` and `myMetric.max.value`. The prefix is applied +to the last part of the metric. + +* _collectors:_ List of collectors to chain together +* _prefixes:_ Prefix to add to each collector respectively + #### DoubleCounter Counts up double values to be reported. The counter can be reset when values are reported by setting reset: true in the conf @@ -596,6 +652,19 @@ by setting reset: true in the conf * _reset:_ (true/false), when true the counter resets after reporting * _report-zero:_ (true/false), when set to false will not report zero values +#### DoubleGauge +Simple gauge that reports the most recently received value. + +* _reset:_ (true/false), when true the gauge sets to zero after reporting + +#### LastTime +LastTime collects Duration metrics and when reporting it simply reports the last +Duration it received. The Duration is cleared once it is reported so it is +only reported once. + +* _report-unit:_ (NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, DAYS), set + the unites values are reported in + #### LongCounter Counts up long values to be reported. The counter can be reset when values are reported by setting reset: true in the conf @@ -613,10 +682,16 @@ Extends LongGauge and only stores and reports the max value over the reporting p * _reset:_ (true/false), when true the gauge sets to zero after reporting -#### DoubleGauge -Simple gauge that reports the most recently received value. +#### NullCollector +If you are familiar with /dev/null, this is the same concept. A way of turning +off certain metrics. -* _reset:_ (true/false), when true the gauge sets to zero after reporting +#### PutCounter +Counts the number of times the put method is called on a collector. Can be used +with any data type. + +* _reset:_ (true/false), when true the counter resets after reporting +* _report-zero:_ (true/false), when set to false will not report zero values #### SimpleStats This reports the min, max, sum, count and avg for the set of values received since @@ -638,6 +713,29 @@ the unites values are reported in No aggregation is done in this collector. All strings are reported with the time they were received. +#### TimeDelta +This records the difference between now (computers local clock) and the timestamp +provided. The deltas are recorded as a SimpleTimerMetric + +Takes the same parameters as SimpleTimerMetric + +#### TimestampCounter +This collector records a count of timestamps during a configurable amount of time. +The initial use of this collector was to count events going in and out of an event +system. The idea is to count all events that occur in a bucket of time and report +them using a timestamp within that bucket. + +The default configuration can handle the same timestamp (rounded to the minute) +being reported for just short of 2 weeks (13 ish days) before potentially overwriting +the same timestamp when reporting data. + +Basically a timestamp (rounded to the minute) can be counted and reported for about 2 weeks +each time it reports it will use a slightly different timestamp so as to not overwrite +previous counts + +For a detail description of how this works see: +https://github.com/kairosdb/metrics4j/wiki/TimestampCounter + ### Formatters A formatter can change the name to your liking ie. underscore vs period in the name. diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/MetricSourceManager.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/MetricSourceManager.java index 1c51774..0789483 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/MetricSourceManager.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/MetricSourceManager.java @@ -24,6 +24,8 @@ import java.util.function.DoubleSupplier; import java.util.function.LongSupplier; +import static java.util.Objects.requireNonNull; + /** Need to put methods to set various components programmatically. @@ -76,12 +78,12 @@ public static MetricConfig getMetricConfig() return s_metricConfig; } - public static void registerMetricCollector(MetricCollector collector) + /*public static void registerMetricCollector(MetricCollector collector) { ArgKey key = new CustomArgKey(collector); - //getMetricConfig().getContext().assignCollector(key, collector, new HashMap<>(), new HashMap<>(), null); - } + getMetricConfig().getContext().assignCollector(key, collector, new HashMap<>(), new HashMap<>(), null); + }*/ public static T getSource(Class tClass) { @@ -102,6 +104,7 @@ public static T getSource(Class tClass) //todo need to do some validation on tClass, makes ure all methods only take strings and are annotated with Keys + requireNonNull(tClass, "Source class object cannot be null"); InvocationHandler handler = s_invocationMap.computeIfAbsent(tClass, (klass) -> { MetricConfig metricConfig = getMetricConfig(); if (metricConfig.isDumpMetrics()) @@ -165,8 +168,22 @@ private static TagKey buildTagKey(Map tags) return builder.build(); } + /** + In some cases it is more helpful to have the metrics code call into your code + to gather the metric or state of the system at the time of collection. This + method allows you to artificially create a MetricCollector that will be called + when metrics are collected to be reported + @param className The class name to identify this collector for configuration purposes. + @param methodName The method name to identify this collector for configuration purposes. + @param tags Tags associated with this collector + @param help Help text for this collector + @param collector A instance that implements MetricCollector to be called when collecting metrics. + */ public static void addSource(String className, String methodName, Map tags, String help, MetricCollector collector) { + requireNonNull(className, "className cannot be null"); + requireNonNull(methodName, "methodName cannot be null"); + requireNonNull(collector, "collector cannot be null"); ArgKey key = new LambdaArgKey(className, methodName); MetricConfig metricConfig = getMetricConfig(); @@ -195,11 +212,27 @@ public static void addSource(String className, String methodName, Map tags, String help, LongSupplier supplier) { addSource(className, methodName, tags, help, new LongLambdaCollectorAdaptor(supplier)); } + /** + A helper method that lets you pass a lambda function as the MetricCollector see {@link #addSource(String, String, Map, String, MetricCollector) addSource} method. + @param className The class name to identify this collector for configuration purposes. + @param methodName The method name to identify this collector for configuration purposes. + @param tags Tags associated with this collector + @param help Help text for this collector + @param supplier Lambda function that returns a double value. + */ public static void addSource(String className, String methodName, Map tags, String help, DoubleSupplier supplier) { addSource(className, methodName, tags, help, new DoubleLambdaCollectorAdaptor(supplier)); diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DoubleCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DoubleCollector.java index 7161208..71a9161 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DoubleCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DoubleCollector.java @@ -1,6 +1,23 @@ package org.kairosdb.metrics4j.collectors; +import java.time.Instant; + public interface DoubleCollector extends Collector { + /** + Place a double value into the collector to be reported. + The actual value reported is determined by the collector implementation + that is configured for this source. + @param value Value to be reported. + */ void put(double value); + + /** + Place a double value and associated timestamp into the collector to be reported. + The actual value and time reported is determined by the collector implementation + that is configured for this source. + @param time Suggested timestamp to use when reporting. + @param value Value to be reported. + */ + void put(Instant time, double value); } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DurationCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DurationCollector.java index 2269539..07dabed 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DurationCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/DurationCollector.java @@ -3,6 +3,7 @@ import org.kairosdb.metrics4j.collectors.helpers.BlockTimer; import java.time.Duration; +import java.time.Instant; import java.util.concurrent.Callable; /** @@ -11,7 +12,22 @@ */ public interface DurationCollector extends Collector { + /** + Place a Duration value into the collector to be reported. + The actual value reported is determined by the collector implementation + that is configured for this source. + @param duration Value to be reported. + */ void put(Duration duration); + + /** + Place a Duration value and associated timestamp into the collector to be reported. + The actual value and time reported is determined by the collector implementation + that is configured for this source. + @param time Suggested timestamp to use when reporting. + @param duration Value to be reported. + */ + void put(Instant time, Duration duration); T timeEx(Callable callable) throws Exception; T time(TimeCallable callable); BlockTimer time(); diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/LongCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/LongCollector.java index 64875b4..03771f6 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/LongCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/LongCollector.java @@ -1,6 +1,23 @@ package org.kairosdb.metrics4j.collectors; +import java.time.Instant; + public interface LongCollector extends Collector { + /** + Place a long value into the collector to be reported. + The actual value reported is determined by the collector implementation + that is configured for this source. + @param value Value to be reported. + */ void put(long value); + + /** + Place a long value and associated timestamp into the collector to be reported. + The actual value and time reported is determined by the collector implementation + that is configured for this source. + @param time Suggested timestamp to use when reporting. + @param value Value to be reported. + */ + void put(Instant time, long value); } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/StringCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/StringCollector.java index 4ad150e..7989c24 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/StringCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/StringCollector.java @@ -1,6 +1,23 @@ package org.kairosdb.metrics4j.collectors; +import java.time.Instant; + public interface StringCollector extends Collector { + /** + Place a String value into the collector to be reported. + The actual value reported is determined by the collector implementation + that is configured for this source. + @param value Value to be reported. + */ void put(String value); + + /** + Place a double value and associated timestamp into the collector to be reported. + The actual value and time reported is determined by the collector implementation + that is configured for this source. + @param time Suggested timestamp to use when reporting. + @param value Value to be reported. + */ + void put(Instant time, String value); } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/TimeCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/TimeCollector.java index de586a6..784bd84 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/TimeCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/TimeCollector.java @@ -8,5 +8,20 @@ */ public interface TimeCollector extends Collector { - void put(Instant time); + /** + Place a double value into the collector to be reported. + The actual value reported is determined by the collector implementation + that is configured for this source. + @param value Value to be reported. + */ + void put(Instant value); + + /** + Place a double value and associated timestamp into the collector to be reported. + The actual value and time reported is determined by the collector implementation + that is configured for this source. + @param time Suggested timestamp to use when reporting. + @param value Value to be reported. + */ + void put(Instant time, Instant value); } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/helpers/ChainedCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/helpers/ChainedCollector.java index 28007f0..a3de994 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/helpers/ChainedCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/helpers/ChainedCollector.java @@ -13,6 +13,10 @@ import java.util.ArrayList; import java.util.List; +/** + ChainedCollector lets you report metrics to more than one collector. + @param + */ @ToString @EqualsAndHashCode public abstract class ChainedCollector extends Cloneable implements Collector diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDoubleCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDoubleCollector.java index dabab16..68f685d 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDoubleCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDoubleCollector.java @@ -5,6 +5,9 @@ import org.kairosdb.metrics4j.collectors.helpers.ChainedCollector; import org.kairosdb.metrics4j.configuration.ConfigurationException; +import java.time.Instant; + + public class ChainedDoubleCollector extends ChainedCollector implements DoubleCollector { @Override @@ -16,6 +19,15 @@ public void put(double value) } } + @Override + public void put(Instant time, double value) + { + for (PrefixMetricReporter chainedCollector : m_chainedCollectors) + { + chainedCollector.getCollector().put(time, value); + } + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDurationCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDurationCollector.java index b474e31..bd85a48 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDurationCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedDurationCollector.java @@ -9,6 +9,7 @@ import org.kairosdb.metrics4j.configuration.ConfigurationException; import java.time.Duration; +import java.time.Instant; import java.util.concurrent.Callable; public class ChainedDurationCollector extends ChainedCollector implements DurationCollector @@ -24,6 +25,15 @@ public void put(Duration duration) } } + @Override + public void put(Instant time, Duration duration) + { + for (PrefixMetricReporter chainedCollector : m_chainedCollectors) + { + chainedCollector.getCollector().put(time, duration); + } + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedLongCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedLongCollector.java index 4b4abfb..1cab9ac 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedLongCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedLongCollector.java @@ -5,6 +5,8 @@ import org.kairosdb.metrics4j.collectors.helpers.ChainedCollector; import org.kairosdb.metrics4j.configuration.ConfigurationException; +import java.time.Instant; + public class ChainedLongCollector extends ChainedCollector implements LongCollector { @@ -17,6 +19,15 @@ public void put(long value) } } + @Override + public void put(Instant time, long value) + { + for (PrefixMetricReporter chainedCollector : m_chainedCollectors) + { + chainedCollector.getCollector().put(time, value); + } + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedStringCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedStringCollector.java index f840c59..c435f4b 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedStringCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedStringCollector.java @@ -5,6 +5,8 @@ import org.kairosdb.metrics4j.collectors.helpers.ChainedCollector; import org.kairosdb.metrics4j.configuration.ConfigurationException; +import java.time.Instant; + public class ChainedStringCollector extends ChainedCollector implements StringCollector { @Override @@ -16,6 +18,15 @@ public void put(String value) } } + @Override + public void put(Instant time, String value) + { + for (PrefixMetricReporter chainedCollector : m_chainedCollectors) + { + chainedCollector.getCollector().put(time, value); + } + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedTimeCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedTimeCollector.java index a233e26..31576d2 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedTimeCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/ChainedTimeCollector.java @@ -10,11 +10,20 @@ public class ChainedTimeCollector extends ChainedCollector implements TimeCollector { @Override - public void put(Instant time) + public void put(Instant value) { for (PrefixMetricReporter chainedCollector : m_chainedCollectors) { - chainedCollector.getCollector().put(time); + chainedCollector.getCollector().put(value); + } + } + + @Override + public void put(Instant time, Instant value) + { + for (PrefixMetricReporter chainedCollector : m_chainedCollectors) + { + chainedCollector.getCollector().put(time, value); } } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleCounter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleCounter.java index 4d3f285..02c2a3a 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleCounter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleCounter.java @@ -9,6 +9,8 @@ import org.kairosdb.metrics4j.reporting.DoubleValue; import org.kairosdb.metrics4j.reporting.MetricReporter; +import java.time.Instant; + @ToString @EqualsAndHashCode public class DoubleCounter implements DoubleCollector @@ -44,6 +46,12 @@ public void put(double value) } } + @Override + public void put(Instant time, double value) + { + put(value); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleGauge.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleGauge.java index 8b8b21c..162bfff 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleGauge.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/DoubleGauge.java @@ -9,6 +9,8 @@ import org.kairosdb.metrics4j.reporting.DoubleValue; import org.kairosdb.metrics4j.reporting.MetricReporter; +import java.time.Instant; + @ToString @EqualsAndHashCode @@ -41,6 +43,12 @@ public void put(double value) } } + @Override + public void put(Instant time, double value) + { + put(value); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LastTime.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LastTime.java index dd3428f..85b0050 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LastTime.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LastTime.java @@ -11,6 +11,7 @@ import org.kairosdb.metrics4j.reporting.MetricReporter; import java.time.Duration; +import java.time.Instant; import java.util.concurrent.atomic.AtomicReference; @ToString @@ -51,5 +52,11 @@ public void put(Duration duration) m_lastTime.set(duration); } + @Override + public void put(Instant time, Duration duration) + { + put(duration); + } + } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongCounter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongCounter.java index dcdc2af..32b3d0d 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongCounter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongCounter.java @@ -9,6 +9,7 @@ import org.kairosdb.metrics4j.reporting.LongValue; import org.kairosdb.metrics4j.reporting.MetricReporter; +import java.time.Instant; import java.util.concurrent.atomic.AtomicLong; @@ -37,11 +38,17 @@ public LongCounter() this(false, true); } + @Override public void put(long count) { m_count.addAndGet(count); } + @Override + public void put(Instant time, long count) + { + put(count); + } @Override public void reportMetric(MetricReporter metricReporter) diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongGauge.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongGauge.java index b5f9be3..01020c8 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongGauge.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/LongGauge.java @@ -9,6 +9,7 @@ import org.kairosdb.metrics4j.reporting.LongValue; import org.kairosdb.metrics4j.reporting.MetricReporter; +import java.time.Instant; import java.util.concurrent.atomic.AtomicLong; @ToString @@ -39,6 +40,12 @@ public void put(long value) m_gauge.set(value); } + @Override + public void put(Instant time, long count) + { + put(count); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/MaxLongGauge.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/MaxLongGauge.java index 464d001..13d94e9 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/MaxLongGauge.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/MaxLongGauge.java @@ -4,6 +4,8 @@ import lombok.ToString; import org.kairosdb.metrics4j.collectors.Collector; +import java.time.Instant; + @ToString @EqualsAndHashCode public class MaxLongGauge extends LongGauge @@ -29,4 +31,10 @@ public void put(long value) { m_gauge.accumulateAndGet(value, Long::max); } + + @Override + public void put(Instant time, long count) + { + put(count); + } } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/NullCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/NullCollector.java index 193ba7e..fdabfd9 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/NullCollector.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/NullCollector.java @@ -3,12 +3,14 @@ import org.kairosdb.metrics4j.MetricsContext; import org.kairosdb.metrics4j.collectors.Collector; import org.kairosdb.metrics4j.collectors.DoubleCollector; +import org.kairosdb.metrics4j.collectors.DurationCollector; import org.kairosdb.metrics4j.collectors.LongCollector; import org.kairosdb.metrics4j.collectors.StringCollector; import org.kairosdb.metrics4j.collectors.helpers.TimerCollector; import org.kairosdb.metrics4j.reporting.MetricReporter; import java.time.Duration; +import java.time.Instant; /** Collector to use if you want to effectively turn off a source. @@ -20,11 +22,21 @@ public void put(double value) { } + @Override + public void put(Instant time, double count) + { + } + @Override public void put(long value) { } + @Override + public void put(Instant time, long count) + { + } + @Override public Collector clone() { @@ -47,8 +59,18 @@ public void put(String value) { } + @Override + public void put(Instant time, String value) + { + } + @Override public void put(Duration duration) { } + + @Override + public void put(Instant time, Duration duration) + { + } } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/PutCounter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/PutCounter.java index 689d7b8..c0fb832 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/PutCounter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/PutCounter.java @@ -45,11 +45,17 @@ public PutCounter() this(false, true); } + @Override public void put(long count) { m_count.incrementAndGet(); } + @Override + public void put(Instant time, long value) + { + m_count.incrementAndGet(); + } @Override public void reportMetric(MetricReporter metricReporter) @@ -83,21 +89,45 @@ public void put(double value) m_count.incrementAndGet(); } + @Override + public void put(Instant time, double value) + { + m_count.incrementAndGet(); + } + @Override public void put(Duration duration) { m_count.incrementAndGet(); } + @Override + public void put(Instant time, Duration duration) + { + m_count.incrementAndGet(); + } + @Override public void put(String value) { m_count.incrementAndGet(); } + @Override + public void put(Instant time, String value) + { + m_count.incrementAndGet(); + } + @Override public void put(Instant time) { m_count.incrementAndGet(); } + + @Override + public void put(Instant time, Instant value) + { + m_count.incrementAndGet(); + } } diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleStats.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleStats.java index 1470366..c02e950 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleStats.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleStats.java @@ -12,6 +12,7 @@ import org.kairosdb.metrics4j.reporting.MetricReporter; import org.kairosdb.metrics4j.reporting.MetricValue; +import java.time.Instant; import java.util.HashMap; import java.util.Map; @@ -64,6 +65,12 @@ public void put(long value) } } + @Override + public void put(Instant time, long value) + { + put(value); + } + public void reset() { m_min = Long.MAX_VALUE; diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleTimerMetric.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleTimerMetric.java index e3d8a44..d19f2f5 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleTimerMetric.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/SimpleTimerMetric.java @@ -13,6 +13,7 @@ import org.kairosdb.metrics4j.reporting.MetricReporter; import java.time.Duration; +import java.time.Instant; import java.time.temporal.ChronoUnit; @ToString @@ -115,6 +116,12 @@ public void put(Duration duration) } } + @Override + public void put(Instant time, Duration duration) + { + put(duration); + } + @Override public Collector clone() diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/StringReporter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/StringReporter.java index 3308024..9ed24af 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/StringReporter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/StringReporter.java @@ -30,6 +30,12 @@ public void put(String value) } } + @Override + public void put(Instant time, String value) + { + put(value); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimeDelta.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimeDelta.java index c87cb5d..be1eb48 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimeDelta.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimeDelta.java @@ -32,12 +32,18 @@ public TimeDelta() @Override - public void put(Instant time) + public void put(Instant value) { - Duration between = Duration.between(time, Instant.ofEpochMilli(m_clock.now())).abs(); + Duration between = Duration.between(value, Instant.ofEpochMilli(m_clock.now())).abs(); super.put(between); } + @Override + public void put(Instant time, Instant value) + { + put(value); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimestampCounter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimestampCounter.java index 7921c6e..96b7b4d 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimestampCounter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/collectors/impl/TimestampCounter.java @@ -59,9 +59,9 @@ public TimestampCounter(Clock clock) @Override - public void put(Instant time) + public void put(Instant value) { - Instant bucketTime = time.truncatedTo(ChronoUnit.MINUTES); + Instant bucketTime = value.truncatedTo(ChronoUnit.MINUTES); synchronized (m_mapLock) { @@ -71,6 +71,12 @@ public void put(Instant time) } } + @Override + public void put(Instant time, Instant value) + { + put(value); + } + @Override public Collector clone() { diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollector.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollector.java deleted file mode 100644 index 5caca02..0000000 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollector.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.kairosdb.metrics4j.internal; - -import org.kairosdb.metrics4j.MetricsContext; -import org.kairosdb.metrics4j.collectors.Collector; -import org.kairosdb.metrics4j.collectors.DoubleCollector; -import org.kairosdb.metrics4j.collectors.DurationCollector; -import org.kairosdb.metrics4j.collectors.LongCollector; -import org.kairosdb.metrics4j.collectors.MetricCollector; -import org.kairosdb.metrics4j.collectors.StringCollector; -import org.kairosdb.metrics4j.collectors.TimeCollector; -import org.kairosdb.metrics4j.collectors.helpers.TimerCollector; -import org.kairosdb.metrics4j.reporting.MetricReporter; - -import java.time.Duration; -import java.time.Instant; - -public class DevNullCollector extends TimerCollector implements LongCollector, - DoubleCollector, DurationCollector, MetricCollector, TimeCollector, StringCollector -{ - @Override - public void put(double value) - { - } - - @Override - public void put(Duration duration) - { - } - - @Override - public void put(long value) - { - } - - @Override - public void reportMetric(MetricReporter reportedMetric) - { - - } - - @Override - public void init(MetricsContext context) - { - } - - @Override - public Collector clone() - { - return this; - } - - @Override - public void put(Instant time) - { - } - - @Override - public void put(String value) - { - } -} diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollectorCollection.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollectorCollection.java index 80167bf..3ee368a 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollectorCollection.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/DevNullCollectorCollection.java @@ -1,6 +1,7 @@ package org.kairosdb.metrics4j.internal; import org.kairosdb.metrics4j.collectors.Collector; +import org.kairosdb.metrics4j.collectors.impl.NullCollector; import org.kairosdb.metrics4j.reporting.ReportedMetric; import java.time.Instant; @@ -8,7 +9,7 @@ public class DevNullCollectorCollection implements CollectorCollection { - private static final Collector COLLECTOR = new DevNullCollector(); + private static final Collector COLLECTOR = new NullCollector(); @Override public Collector getCollector(TagKey tagKey) diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/SourceInvocationHandler.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/SourceInvocationHandler.java index 144e2bb..4881767 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/SourceInvocationHandler.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/internal/SourceInvocationHandler.java @@ -3,6 +3,7 @@ import org.kairosdb.metrics4j.annotation.Help; import org.kairosdb.metrics4j.collectors.Collector; import org.kairosdb.metrics4j.collectors.MetricCollector; +import org.kairosdb.metrics4j.collectors.impl.NullCollector; import org.kairosdb.metrics4j.configuration.ImplementationException; import org.kairosdb.metrics4j.configuration.MetricConfig; import org.slf4j.Logger; @@ -63,7 +64,7 @@ public void setCollector(MethodArgKey key, MetricCollector statsObject) CollectorContext context = m_statsMap.get(key); if ((context == null) || (!(context.getCollection() instanceof CollectorCollectionAdapter))) { - CollectorCollection collection = new CollectorCollectionAdapter(new DevNullCollector(), key); + CollectorCollection collection = new CollectorCollectionAdapter(new NullCollector(), key); context = new TestingCollectorContext(collection); m_statsMap.put(key, context); diff --git a/metrics4j/src/main/java/org/kairosdb/metrics4j/plugins/JMXReporter.java b/metrics4j/src/main/java/org/kairosdb/metrics4j/plugins/JMXReporter.java index a58b748..9af2f69 100644 --- a/metrics4j/src/main/java/org/kairosdb/metrics4j/plugins/JMXReporter.java +++ b/metrics4j/src/main/java/org/kairosdb/metrics4j/plugins/JMXReporter.java @@ -157,7 +157,7 @@ private void unregisterMBean(ObjectName beanName) public void handleNotification(Notification notification, Object handback) { if (!(notification instanceof MBeanServerNotification)) { - System.out.println("Ignored notification of class " + notification.getClass().getName()); + logger.debug("Ignored notification of class " + notification.getClass().getName()); return; } MBeanServerNotification mbsn = (MBeanServerNotification) notification; diff --git a/metrics4j/src/test/java/org/kairosdb/metrics4j/collectors/DurationCollectorTest.java b/metrics4j/src/test/java/org/kairosdb/metrics4j/collectors/DurationCollectorTest.java index 6fe4530..7c3d419 100644 --- a/metrics4j/src/test/java/org/kairosdb/metrics4j/collectors/DurationCollectorTest.java +++ b/metrics4j/src/test/java/org/kairosdb/metrics4j/collectors/DurationCollectorTest.java @@ -5,7 +5,7 @@ import org.kairosdb.metrics4j.TestMetricSource; import org.kairosdb.metrics4j.collectors.helpers.BlockTimer; import org.kairosdb.metrics4j.collectors.impl.LongCounter; -import org.kairosdb.metrics4j.internal.DevNullCollector; +import org.kairosdb.metrics4j.collectors.impl.NullCollector; import java.time.Duration; @@ -34,7 +34,7 @@ public void testLambda() TestMetricSource reporter = MetricSourceManager.getSource(TestMetricSource.class); LongCounter myCounter = new LongCounter(); - DurationCollector collector = spy(new DevNullCollector()); + DurationCollector collector = spy(new NullCollector()); MetricSourceManager.setCollectorForSource(collector, TestMetricSource.class).reportTime("localhost"); @@ -51,7 +51,7 @@ public void testBlockTimer() TestMetricSource reporter = MetricSourceManager.getSource(TestMetricSource.class); LongCounter myCounter = new LongCounter(); - DurationCollector collector = spy(new DevNullCollector()); + DurationCollector collector = spy(new NullCollector()); MetricSourceManager.setCollectorForSource(collector, TestMetricSource.class).reportTime("localhost"); String response;