Skip to content

Commit a761da5

Browse files
committed
Add GraphiteReporter.Builder.timedThresholdMicros()
This can be used typically when using enhancement to add metrics to all spring components or all avaje-inject components to filter out timing metrics and not report them when method total execution time does not exceed the threshold specified.
1 parent 5bb01dd commit a761da5

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

metrics-graphite/src/main/java/io/avaje/metrics/graphite/DGraphiteBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class DGraphiteBuilder implements GraphiteReporter.Builder {
1717
private String prefix;
1818
private String hostname;
1919
private int port;
20+
private long timedThresholdMicros;
2021
private SocketFactory socketFactory = SSLSocketFactory.getDefault();
2122

2223
private final List<GraphiteSender.Reporter> reporters = new ArrayList<>();
@@ -57,7 +58,14 @@ public DGraphiteBuilder excludeDefaultRegistry() {
5758
this.excludeDefaultRegistry = true;
5859
return this;
5960
}
60-
@Override
61+
62+
@Override
63+
public DGraphiteBuilder timedThresholdMicros(int timedThresholdMicros) {
64+
this.timedThresholdMicros = timedThresholdMicros;
65+
return this;
66+
}
67+
68+
@Override
6169
public DGraphiteBuilder database(Database database) {
6270
reporters.add(DatabaseReporter.reporter(database));
6371
return this;
@@ -84,11 +92,11 @@ private GraphiteSender buildSender() {
8492
throw new IllegalStateException("Unknown host " + address.getHostName());
8593
}
8694

87-
return new DGraphiteSender(address, socketFactory, batchSize, prefix);
95+
return new DGraphiteSender(address, socketFactory, batchSize, prefix, timedThresholdMicros);
8896
}
8997

9098
public GraphiteReporter build() {
91-
if (!excludeDefaultRegistry){
99+
if (!excludeDefaultRegistry) {
92100
reporters.add(new DRegistryReporter(Metrics.registry()));
93101
}
94102
return new DGraphiteReporter(buildSender(), reporters);

metrics-graphite/src/main/java/io/avaje/metrics/graphite/DGraphiteSender.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ final class DGraphiteSender implements GraphiteSender {
3838
private final InetSocketAddress address;
3939
private final SocketFactory socketFactory;
4040
private final String prefix;
41+
private final long timedThreshold;
4142
private Socket socket;
4243
private Writer writer;
4344

44-
DGraphiteSender(InetSocketAddress address, SocketFactory socketFactory, int batchSize, String prefix) {
45+
DGraphiteSender(InetSocketAddress address, SocketFactory socketFactory, int batchSize, String prefix, long timedThreshold) {
4546
this.address = address;
4647
this.socketFactory = socketFactory;
4748
this.batchSize = batchSize;
4849
this.prefix = prefix;
50+
this.timedThreshold = timedThreshold;
4951
}
5052

5153
@Override
@@ -80,11 +82,13 @@ public void send(List<Metric.Statistics> metrics) {
8082

8183
private class MetricsVisitor implements Metric.Visitor {
8284

83-
final long epochSecs = System.currentTimeMillis() / 1000;
85+
private final long epochSecs = System.currentTimeMillis() / 1000;
8486

8587
@Override
8688
public void visit(Timer.Stats timed) {
87-
sendValues(timed);
89+
if (timedThreshold == 0 || timedThreshold < timed.total()) {
90+
sendValues(timed);
91+
}
8892
}
8993

9094
@Override
@@ -162,7 +166,7 @@ public void close() throws IOException {
162166
* 3. Clear out the list of metrics
163167
*/
164168
private void writeMetrics() throws IOException {
165-
if (metrics.size() > 0) {
169+
if (!metrics.isEmpty()) {
166170
try {
167171
byte[] payload = pickleMetrics(metrics);
168172
byte[] header = ByteBuffer.allocate(4).putInt(payload.length).array();

metrics-graphite/src/main/java/io/avaje/metrics/graphite/GraphiteReporter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ interface Builder {
9494
*/
9595
Builder excludeDefaultRegistry();
9696

97+
/**
98+
* Set a threshold where timed metrics with total time less than the
99+
* threshold are not reported.
100+
* <p>
101+
* This can use used to when metrics are applied fairly globally (for
102+
* example on all Spring components or all avaje-inject components)
103+
* and hence can include methods that are not that interesting for
104+
* collecting timing on.
105+
* <p>
106+
* Setting this to 1000 (which is 1 millisecond) when reporting every 1 minute
107+
* means that methods with total execution time less than 1ms in a one-minute period
108+
* will not be reported.
109+
*/
110+
Builder timedThresholdMicros(int timedThresholdMicros);
111+
97112
/**
98113
* Include Ebean Database metrics in the reporting.
99114
* <p>

0 commit comments

Comments
 (0)