-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from avast/group-monitor
Remove MultiMonitor, add Summary and Group monitors
- Loading branch information
Showing
7 changed files
with
384 additions
and
185 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
core/src/main/java/com/avast/metrics/core/multi/GroupMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Monitor> 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<Monitor> monitors, Naming naming) { | ||
if (monitors.size() < 2) { | ||
throw new IllegalArgumentException("Group monitor from less than 2 counters makes no sense"); | ||
} | ||
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<Monitor> 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<Monitor> 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<Meter> meters = monitors.stream() | ||
.map(m -> m.newMeter(name)) | ||
.collect(Collectors.toList()); | ||
|
||
return new MultiMeter(meters); | ||
} | ||
|
||
@Override | ||
public Counter newCounter(String name) { | ||
List<Counter> counters = monitors.stream() | ||
.map(m -> m.newCounter(name)) | ||
.collect(Collectors.toList()); | ||
|
||
return new MultiCounter(counters); | ||
} | ||
|
||
@Override | ||
public Timer newTimer(String name) { | ||
List<Timer> 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 <T> Gauge<T> newGauge(String name, Supplier<T> gauge) { | ||
return newGauge(name, false, gauge); | ||
} | ||
|
||
@Override | ||
public <T> Gauge<T> newGauge(String name, boolean replaceExisting, Supplier<T> gauge) { | ||
List<Gauge<T>> gauges = monitors.stream() | ||
.map(monitor -> monitor.newGauge(name, replaceExisting, gauge)) | ||
.collect(Collectors.toList()); | ||
return gauges.get(0); | ||
} | ||
|
||
@Override | ||
public Histogram newHistogram(String name) { | ||
List<Histogram> 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.