From 51564753daba8cb657c6395b7d36c1b3c5208a98 Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Mon, 18 Sep 2017 10:48:08 +0200 Subject: [PATCH 1/5] Remove MultiMonitor, add Summary and Group monitors --- .../metrics/core/multi/GroupMonitor.java | 150 ++++++++++++++++++ .../metrics/core/multi/MultiCounter.java | 2 +- .../metrics/core/multi/MultiHistogram.java | 2 +- .../avast/metrics/core/multi/MultiMeter.java | 2 +- .../avast/metrics/core/multi/MultiTimer.java | 2 +- ...{MultiMonitor.java => SummaryMonitor.java} | 32 ++-- 6 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java rename core/src/main/java/com/avast/metrics/core/multi/{MultiMonitor.java => SummaryMonitor.java} (83%) diff --git a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java new file mode 100644 index 0000000..a17f203 --- /dev/null +++ b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java @@ -0,0 +1,150 @@ +package com.avast.metrics.core.multi; + +import com.avast.metrics.TimerPairImpl; +import com.avast.metrics.api.*; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +/** + * Wrapper around a group of monitors. + */ +public class GroupMonitor implements Monitor { + + private final List monitors; + private final Naming naming; + + /** + * Factory method. + * + * @param monitors list of monitors + * @return multiple monitor containing all passed monitors + */ + public static Monitor of(Monitor... monitors) { + return new GroupMonitor(Arrays.asList(monitors),Naming.defaultNaming()); + } + + /** + * Factory method. + * + * @param monitors list of monitors + * @param naming naming conventions for TimerPairs + * @return multiple monitor containing all passed monitors + */ + public static Monitor of(Naming naming, Monitor... monitors) { + return new GroupMonitor(Arrays.asList(monitors), naming); + } + + private GroupMonitor(List monitors, Naming naming) { + if (monitors.size() == 0) { + throw new IllegalArgumentException("Group monitor from requires at least single instance of Monitor."); + } + this.monitors = monitors; + this.naming = naming; + } + + /** + * {@inheritDoc} + * + * @param name name of the next sub-level + * @return new group monitor with name applied to all instances + */ + @Override + public Monitor named(String name) { + List newMonitors = monitors.stream() + .map(monitor -> monitor.named(name)) + .collect(Collectors.toList()); + return new GroupMonitor(newMonitors, naming); + } + + /** + * {@inheritDoc} + * + * @param name1 name of the next sub-level + * @param name2 name of the next sub-level + * @param restOfNames name of the next sub-level + * @return new group monitor with name applied to all instances + */ + @Override + public Monitor named(String name1, String name2, String... restOfNames) { + List newMonitors = monitors.stream() + .map(monitor -> monitor.named(name1, name2, restOfNames)) + .collect(Collectors.toList()); + return new GroupMonitor(newMonitors, naming); + } + + @Override + public String getName() { + return monitors.get(0).getName(); + } + + @Override + public Meter newMeter(String name) { + List meters = monitors.stream() + .map(m -> m.newMeter(name)) + .collect(Collectors.toList()); + + return new MultiMeter(meters); + } + + @Override + public Counter newCounter(String name) { + List counters = monitors.stream() + .map(m -> m.newCounter(name)) + .collect(Collectors.toList()); + + return new MultiCounter(counters); + } + + @Override + public Timer newTimer(String name) { + List timers = monitors.stream() + .map(m -> m.newTimer(name)) + .collect(Collectors.toList()); + + return new MultiTimer(timers); + } + + @Override + public TimerPair newTimerPair(String name) { + return new TimerPairImpl( + newTimer(naming.successTimerName(name)), + newTimer(naming.failureTimerName(name)) + ); + } + + @Override + public Gauge newGauge(String name, Supplier gauge) { + return newGauge(name, false, gauge); + } + + @Override + public Gauge newGauge(String name, boolean replaceExisting, Supplier gauge) { + List> gauges = monitors.stream() + .map(monitor -> monitor.newGauge(name, replaceExisting, gauge)) + .collect(Collectors.toList()); + return gauges.get(0); + } + + @Override + public Histogram newHistogram(String name) { + List histograms = monitors.stream() + .map(m -> m.newHistogram(name)) + .collect(Collectors.toList()); + + return new MultiHistogram(histograms); + } + + @Override + public void remove(Metric metric) { + monitors.forEach(monitor -> monitor.remove(metric)); + } + + @Override + public void close() { + monitors.forEach(Monitor::close); + } + +} diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiCounter.java b/core/src/main/java/com/avast/metrics/core/multi/MultiCounter.java index d432357..b39cbd4 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/MultiCounter.java +++ b/core/src/main/java/com/avast/metrics/core/multi/MultiCounter.java @@ -5,7 +5,7 @@ import java.util.List; /** - * Counter used by {@link MultiMonitor}. + * Counter used by {@link GroupMonitor} and {@link SummaryMonitor}. */ class MultiCounter implements Counter { private final List counters; diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiHistogram.java b/core/src/main/java/com/avast/metrics/core/multi/MultiHistogram.java index 6bf03d1..10068d7 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/MultiHistogram.java +++ b/core/src/main/java/com/avast/metrics/core/multi/MultiHistogram.java @@ -5,7 +5,7 @@ import java.util.List; /** - * Histogram used by {@link MultiMonitor}. + * Histogram used by {@link GroupMonitor} and {@link SummaryMonitor}. */ class MultiHistogram implements Histogram { private final List histograms; diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiMeter.java b/core/src/main/java/com/avast/metrics/core/multi/MultiMeter.java index 6bf6518..188d781 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/MultiMeter.java +++ b/core/src/main/java/com/avast/metrics/core/multi/MultiMeter.java @@ -5,7 +5,7 @@ import java.util.List; /** - * Meter used by {@link MultiMonitor}. + * Meter used by {@link GroupMonitor} and {@link SummaryMonitor}. */ class MultiMeter implements Meter { private final List meters; diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiTimer.java b/core/src/main/java/com/avast/metrics/core/multi/MultiTimer.java index a5dc011..6c7f051 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/MultiTimer.java +++ b/core/src/main/java/com/avast/metrics/core/multi/MultiTimer.java @@ -11,7 +11,7 @@ import java.util.stream.Collectors; /** - * Timer used by {@link MultiMonitor}. + * Timer used by {@link GroupMonitor} and {@link SummaryMonitor}. */ class MultiTimer implements Timer { private final List timers; diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java similarity index 83% rename from core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java rename to core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java index 28d4443..c70eb72 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java +++ b/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java @@ -22,7 +22,7 @@ * on the first one which represents the monitored instance. Note it is impossible to return two ints at place of one int, * adding them together would typically produce a value that doesn't make any sense at all. *

- * {@link #named(String)} creates a new multi monitor with the name applied only to the instance monitor, the summary + * {@link #named(String)} creates a new summary monitor with the name applied only to the instance monitor, the summary * one is untouched and only copied. This allows to dynamically create new sub-monitors while summary is preserved. *

* {@link #remove(Metric)} removes the metric only from the first wrapped monitor. The summary monitor is shared by @@ -37,7 +37,7 @@ * Monitor summaryMonitor = monitor.named("path"); * Monitor instanceMonitor = summaryMonitor.named("instance"); * - * Monitor monitor = MultiMonitor.of(instanceMonitor, summaryMonitor); + * Monitor monitor = SummaryMonitor.of(instanceMonitor, summaryMonitor); * * this.errors = monitor.newMeter("errors"); * this.requests = monitor.newMeter("requests"); @@ -45,40 +45,40 @@ *

*


  *     Monitor summaryMonitor = monitor.named("path");
- *     Monitor monitor = MultiMonitor.of(summaryMonitor, summaryMonitor);
+ *     Monitor monitor = SummaryMonitor.of(summaryMonitor, summaryMonitor);
  *     ConcurrentMap<String, Meter> requests = new ConcurrentHashMap<>()
  *
  *     // Dynamically create a new meter if not present in the cache and increment its value
  *     requests.computeIfAbsent(topic, t -> monitor.named(t).newMeter("requests")).mark();
  * 
*/ -public class MultiMonitor implements Monitor { +public class SummaryMonitor implements Monitor { private final List monitors; private final Naming naming; /** - * Factory method. Read {@link MultiMonitor} limitations! + * Factory method. Read {@link SummaryMonitor} limitations! * * @param instanceMonitor non-shared main monitor for data from a single instance * @param summaryMonitor shared summary monitor counting sums per all instances - * @return multi monitor containing all passed monitors + * @return summary monitor containing all passed monitors */ public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor) { List allMonitors = new ArrayList<>(2); allMonitors.add(instanceMonitor); allMonitors.add(summaryMonitor); - return new MultiMonitor(allMonitors, Naming.defaultNaming()); + return new SummaryMonitor(allMonitors, Naming.defaultNaming()); } /** - * Factory method. Read {@link MultiMonitor} limitations! + * Factory method. Read {@link SummaryMonitor} limitations! * * @param instanceMonitor non-shared main monitor for data from a single instance * @param summaryMonitor shared summary monitor counting sums per all instances * @param naming naming conventions for TimerPair - * @return multi monitor containing all passed monitors + * @return summary monitor containing all passed monitors */ public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor, Naming naming) { @@ -86,12 +86,12 @@ public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor, Naming allMonitors.add(instanceMonitor); allMonitors.add(summaryMonitor); - return new MultiMonitor(allMonitors, naming); + return new SummaryMonitor(allMonitors, naming); } - private MultiMonitor(List monitors, Naming naming) { + private SummaryMonitor(List monitors, Naming naming) { if (monitors.size() < 2) { - throw new IllegalArgumentException("Multi monitor from less than 2 monitors makes no sense"); + throw new IllegalArgumentException("Summary monitor from less than 2 monitors makes no sense"); } this.monitors = monitors; @@ -102,11 +102,11 @@ private MultiMonitor(List monitors, Naming naming) { * {@inheritDoc} * * @param name name of the next sub-level - * @return new multi monitor with name applied only on the instance one, the summary one is left untouched + * @return new summary monitor with name applied only on the instance one, the summary one is left untouched */ @Override public Monitor named(String name) { - return MultiMonitor.of(monitors.get(0).named(name), monitors.get(1)); + return SummaryMonitor.of(monitors.get(0).named(name), monitors.get(1)); } /** @@ -115,11 +115,11 @@ public Monitor named(String name) { * @param name1 name of the next sub-level * @param name2 name of the next sub-level * @param restOfNames name of the next sub-level - * @return new multi monitor with names applied only on the instance one, the summary one is left untouched + * @return new summary monitor with names applied only on the instance one, the summary one is left untouched */ @Override public Monitor named(String name1, String name2, String... restOfNames) { - return MultiMonitor.of(monitors.get(0).named(name1, name2, restOfNames), monitors.get(1)); + return SummaryMonitor.of(monitors.get(0).named(name1, name2, restOfNames), monitors.get(1)); } @Override From 45f46c5c206e241742b429e02c63c9a63d206cb7 Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Mon, 18 Sep 2017 14:31:20 +0200 Subject: [PATCH 2/5] Keep binary compatibility --- .../metrics/core/multi/MultiMonitor.java | 49 +++++++++++++++++++ .../metrics/core/multi/SummaryMonitor.java | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java diff --git a/core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java new file mode 100644 index 0000000..d4f4a80 --- /dev/null +++ b/core/src/main/java/com/avast/metrics/core/multi/MultiMonitor.java @@ -0,0 +1,49 @@ +package com.avast.metrics.core.multi; + +import com.avast.metrics.api.Monitor; +import com.avast.metrics.api.Naming; + +import java.util.ArrayList; +import java.util.List; + +/** + * Use SummaryMonitor instead. + */ +@Deprecated() +public class MultiMonitor extends SummaryMonitor { + /** + * Factory method. Read {@link MultiMonitor} limitations! + * + * @param instanceMonitor non-shared main monitor for data from a single instance + * @param summaryMonitor shared summary monitor counting sums per all instances + * @return summary monitor containing all passed monitors + */ + public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor) { + List allMonitors = new ArrayList<>(2); + allMonitors.add(instanceMonitor); + allMonitors.add(summaryMonitor); + + return new MultiMonitor(allMonitors, Naming.defaultNaming()); + } + + /** + * Factory method. Read {@link MultiMonitor} limitations! + * + * @param instanceMonitor non-shared main monitor for data from a single instance + * @param summaryMonitor shared summary monitor counting sums per all instances + * @param naming naming conventions for TimerPair + * @return summary monitor containing all passed monitors + */ + + public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor, Naming naming) { + List allMonitors = new ArrayList<>(2); + allMonitors.add(instanceMonitor); + allMonitors.add(summaryMonitor); + + return new MultiMonitor(allMonitors, naming); + } + + private MultiMonitor(List monitors, Naming naming) { + super(monitors, naming); + } +} diff --git a/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java index c70eb72..d3fa5c4 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java +++ b/core/src/main/java/com/avast/metrics/core/multi/SummaryMonitor.java @@ -89,7 +89,7 @@ public static Monitor of(Monitor instanceMonitor, Monitor summaryMonitor, Naming return new SummaryMonitor(allMonitors, naming); } - private SummaryMonitor(List monitors, Naming naming) { + SummaryMonitor(List monitors, Naming naming) { if (monitors.size() < 2) { throw new IllegalArgumentException("Summary monitor from less than 2 monitors makes no sense"); } From b002bac9efe7a0393957c09cbde786abcb74982c Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Mon, 18 Sep 2017 14:31:33 +0200 Subject: [PATCH 3/5] Fixed typo --- .../main/java/com/avast/metrics/core/multi/GroupMonitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java index a17f203..a1f56fa 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java +++ b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java @@ -39,7 +39,7 @@ public static Monitor of(Naming naming, Monitor... monitors) { private GroupMonitor(List monitors, Naming naming) { if (monitors.size() == 0) { - throw new IllegalArgumentException("Group monitor from requires at least single instance of Monitor."); + throw new IllegalArgumentException("Group monitor requires at least single instance of Monitor."); } this.monitors = monitors; this.naming = naming; From eef3f3bc3b1b3ee4f2ccfc8ef2ffbaf18ac7c8db Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Mon, 18 Sep 2017 14:32:58 +0200 Subject: [PATCH 4/5] Formatting --- .../main/java/com/avast/metrics/core/multi/GroupMonitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java index a1f56fa..b70bbfa 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java +++ b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java @@ -23,7 +23,7 @@ public class GroupMonitor implements Monitor { * @return multiple monitor containing all passed monitors */ public static Monitor of(Monitor... monitors) { - return new GroupMonitor(Arrays.asList(monitors),Naming.defaultNaming()); + return new GroupMonitor(Arrays.asList(monitors), Naming.defaultNaming()); } /** From 2bf7d8a4f509c518e90fb60a53d37a9046a2b62f Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Mon, 18 Sep 2017 16:32:43 +0200 Subject: [PATCH 5/5] Require at least two monitors for a group --- .../main/java/com/avast/metrics/core/multi/GroupMonitor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java index b70bbfa..45c9ec7 100644 --- a/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java +++ b/core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java @@ -38,8 +38,8 @@ public static Monitor of(Naming naming, Monitor... monitors) { } private GroupMonitor(List monitors, Naming naming) { - if (monitors.size() == 0) { - throw new IllegalArgumentException("Group monitor requires at least single instance of Monitor."); + if (monitors.size() < 2) { + throw new IllegalArgumentException("Group monitor from less than 2 counters makes no sense"); } this.monitors = monitors; this.naming = naming;