Skip to content

Commit 34c8593

Browse files
committed
#13 - Add ability to easily suppress reporting on small timed metrics via MetricReportConfig.setThresholdMean()
1 parent 0feaf13 commit 34c8593

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

src/main/java/org/avaje/metric/report/CsvReportWriter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ public class CsvReportWriter implements ReportWriter {
2121

2222
protected final String endOfLine;
2323

24+
protected final long thresholdMean;
25+
2426
/**
2527
* Create as comma delimited with newline character at the end of each line.
2628
*/
27-
public CsvReportWriter() {
28-
this("HH:mm:ss", 2, ", ", "\n");
29+
public CsvReportWriter(long thresholdMean) {
30+
this("HH:mm:ss", 2, ", ", "\n", thresholdMean);
2931
}
3032

3133
/**
3234
* Construct with all the format options.
3335
*
34-
* @param timeNowFormat
35-
* The date time format for the collection time. Typically this is HH:mm:ss.
36-
* @param decimalPlaces
37-
* The number of decimal places to format double values. This typically defaults to 2.
38-
* @param delimiter
39-
* A string that separates the columns and typically a comma.
40-
* @param endOfLine
41-
* A string added at the end of each metric and typically a newline character.
36+
* @param timeNowFormat The date time format for the collection time. Typically this is HH:mm:ss.
37+
* @param decimalPlaces The number of decimal places to format double values. This typically defaults to 2.
38+
* @param delimiter A string that separates the columns and typically a comma.
39+
* @param endOfLine A string added at the end of each metric and typically a newline character.
40+
* @param thresholdMean A threshold mean value that can be used to suppress reporting on small metrics.
4241
*/
43-
public CsvReportWriter(String timeNowFormat, int decimalPlaces, String delimiter, String endOfLine) {
42+
public CsvReportWriter(String timeNowFormat, int decimalPlaces, String delimiter, String endOfLine, long thresholdMean) {
4443

4544
this.nowFormatter = new SimpleDateFormat(timeNowFormat);
4645
this.decimalPlaces = decimalPlaces;
4746
this.delimiter = delimiter;
4847
this.endOfLine = endOfLine;
48+
this.thresholdMean = thresholdMean;
4949
}
5050

5151
/**
@@ -57,7 +57,7 @@ public void write(Writer writer, ReportMetrics reportMetrics) throws IOException
5757

5858
String timeNowFormatted = nowFormatter.format(new Date());
5959

60-
CsvWriteVisitor visitor = new CsvWriteVisitor(writer, timeNowFormatted, decimalPlaces, delimiter, endOfLine);
60+
CsvWriteVisitor visitor = new CsvWriteVisitor(writer, timeNowFormatted, decimalPlaces, delimiter, endOfLine, thresholdMean);
6161
visitor.write(reportMetrics);
6262
}
6363

src/main/java/org/avaje/metric/report/CsvWriteVisitor.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public class CsvWriteVisitor implements MetricVisitor {
8585
*/
8686
protected final Writer writer;
8787

88+
/**
89+
* A threshold mean value used to suppress reporting of small timed metrics values.
90+
*/
91+
protected final long thresholdMean;
92+
8893
/**
8994
* Set to true when reporting error metrics (to get the "err." prefix on the metric name).
9095
*/
@@ -97,8 +102,8 @@ public class CsvWriteVisitor implements MetricVisitor {
97102
* @param collectTimeFormatted The time the metrics were collected. Typically in HH:mm:ss format.
98103
* @param decimalPlaces The number of decimal places to format double values. Typically 2.
99104
*/
100-
public CsvWriteVisitor(Writer writer, String collectTimeFormatted, int decimalPlaces) {
101-
this(writer, collectTimeFormatted, decimalPlaces, ", ", "\n");
105+
public CsvWriteVisitor(Writer writer, String collectTimeFormatted, int decimalPlaces, long thresholdMean) {
106+
this(writer, collectTimeFormatted, decimalPlaces, ", ", "\n", thresholdMean);
102107
}
103108

104109
/**
@@ -110,14 +115,16 @@ public CsvWriteVisitor(Writer writer, String collectTimeFormatted, int decimalPl
110115
* @param delimiter The delimiter string that prefixes each column.
111116
* @param endOfLine The character appended after each metric has been output. Typically the newline character.
112117
*/
113-
public CsvWriteVisitor(Writer writer, String collectTimeFormatted, int decimalPlaces, String delimiter, String endOfLine) {
118+
public CsvWriteVisitor(Writer writer, String collectTimeFormatted, int decimalPlaces,
119+
String delimiter, String endOfLine, long thresholdMean) {
114120

115121
this.collectTime = System.currentTimeMillis();
116122
this.writer = writer;
117123
this.collectTimeFormatted = collectTimeFormatted;
118124
this.decimalPlaces = decimalPlaces;
119125
this.delimiter = delimiter;
120126
this.endOfLine = endOfLine;
127+
this.thresholdMean = thresholdMean;
121128
}
122129

123130
/**
@@ -146,9 +153,19 @@ protected void writeMetricEnd(Metric metric) throws IOException {
146153
@Override
147154
public void visit(TimedMetric metric) throws IOException {
148155

156+
ValueStatistics successStats = metric.getCollectedSuccessStatistics();
157+
ValueStatistics errorStats = metric.getCollectedErrorStatistics();
158+
if (thresholdMean > 0) {
159+
if (successStats.getMean() < thresholdMean && errorStats.getMean() < thresholdMean) {
160+
// suppress reporting based on threshold mean (typically when discovering which
161+
// metrics we really want to report on etc).
162+
return;
163+
}
164+
}
165+
149166
writeMetricName(metric, "tm");
150-
writeSummary("", metric.getCollectedSuccessStatistics());
151-
writeSummary("err.", metric.getCollectedErrorStatistics());
167+
writeSummary("", successStats);
168+
writeSummary("err.", errorStats);
152169
writeMetricEnd(metric);
153170
}
154171

src/main/java/org/avaje/metric/report/FileReporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public FileReporter(String baseDirectory, String baseFileName, ReportWriter repo
5353
public FileReporter(String baseDirectory, String baseFileName, int numberOfFilesToKeep, ReportWriter reportWriter) {
5454

5555
super(baseDirectory, baseFileName, numberOfFilesToKeep);
56-
this.reportWriter = (reportWriter != null) ? reportWriter : new CsvReportWriter();
56+
this.reportWriter = (reportWriter != null) ? reportWriter : new CsvReportWriter(0);
5757

5858
cleanup();
5959
}

src/main/java/org/avaje/metric/report/MetricReportConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class MetricReportConfig {
2929

3030
int requestTimingThreshold;
3131

32+
long thresholdMean;
33+
3234
ScheduledExecutorService executor;
3335

3436
List<RequestTimingListener> requestTimingListeners = new ArrayList<>();
@@ -167,6 +169,22 @@ public void setRequestsFreqInSeconds(int requestsFreqInSeconds) {
167169
this.requestsFreqInSeconds = requestsFreqInSeconds;
168170
}
169171

172+
/**
173+
* Return a threshold mean in microseconds that can be used to suppress
174+
* reporting on uninteresting metrics.
175+
*/
176+
public long getThresholdMean() {
177+
return thresholdMean;
178+
}
179+
180+
/**
181+
* Set a threshold mean in microseconds that can be used to suppress
182+
* reporting on uninteresting metrics.
183+
*/
184+
public void setThresholdMean(long thresholdMean) {
185+
this.thresholdMean = thresholdMean;
186+
}
187+
170188
/**
171189
* Return the local reporter to use. This is typically a reporter that writes metrics out to a local file system.
172190
*/

src/main/java/org/avaje/metric/report/MetricReportManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected MetricReporter defaultReporter(MetricReportConfig config) {
100100
if (config.getLocalReporter() != null) {
101101
return config.getLocalReporter();
102102
}
103-
return new FileReporter(config.getDirectory(), config.getMetricsFileName(), new CsvReportWriter());
103+
return new FileReporter(config.getDirectory(), config.getMetricsFileName(), new CsvReportWriter(config.getThresholdMean()));
104104
}
105105

106106
/**

src/test/java/org/avaje/metric/report/CsvFileReporterTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ public class CsvFileReporterTest {
1212

1313
@Test
1414
public void exercise_with_MetricReportManager() throws InterruptedException, IOException {
15-
16-
FileReporter fileReporter = new FileReporter(".", "metric-csv-exercise");
15+
16+
//CsvReportWriter csvReportWriter = new CsvReportWriter(40000);
17+
//FileReporter fileReporter = new FileReporter(".", "metric-csv-exercise", csvReportWriter);
1718

1819
MetricReportConfig config = new MetricReportConfig();
1920
config.setFreqInSeconds(1);
20-
config.setLocalReporter(fileReporter);
21+
config.setMetricsFileName("metric-csv-exercise2");
22+
//config.setThresholdMean(40000);
23+
//config.setLocalReporter(fileReporter);
2124

2225
MetricReportManager report = new MetricReportManager(config);
2326

src/test/java/org/avaje/metric/report/CsvWriteVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testCounter() throws IOException {
4343

4444

4545
private CsvWriteVisitor createVisitor(StringWriter writer) {
46-
return new CsvWriteVisitor(writer, "10:00:00", 2, ",", "\n");
46+
return new CsvWriteVisitor(writer, "10:00:00", 2, ",", "\n", 0);
4747
}
4848

4949

0 commit comments

Comments
 (0)