Skip to content

Commit 8c0c6a1

Browse files
committed
Adjust for changes to TimedMetric API and add time() method for Runnable and Supplier
1 parent 7bb0a3f commit 8c0c6a1

14 files changed

+304
-134
lines changed

src/main/java/io/avaje/metrics/core/BaseTimedMetric.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class BaseTimedMetric implements TimedMetric {
2727
* timing collected for.
2828
*/
2929
@Override
30-
public int getRequestTimingCollection() {
30+
public int getRequestTiming() {
3131
return requestCollection.get();
3232
}
3333

@@ -40,7 +40,7 @@ public int getRequestTimingCollection() {
4040
* @param collectionCount the number of requests we want 'request' timing for.
4141
*/
4242
@Override
43-
public void setRequestTimingCollection(int collectionCount) {
43+
public void setRequestTiming(int collectionCount) {
4444

4545
// synchronized here ... rare call to set the collectionCount
4646
// so being safe wrt decrementCollectionCount()
@@ -60,7 +60,7 @@ public void setRequestTimingCollection(int collectionCount) {
6060
* </p>
6161
*/
6262
@Override
63-
public void decrementCollectionCount() {
63+
public void decrementRequestTiming() {
6464

6565
// synchronized here but this method is only called when 'per request'
6666
// timing is actively being collected and the timing information is being
@@ -87,7 +87,7 @@ public void decrementCollectionCount() {
8787
* </p>
8888
*/
8989
@Override
90-
public boolean isActiveThreadContext() {
90+
public boolean isRequestTiming() {
9191

9292
// volatile read for requestTiming boolean flag
9393
if (requestTiming) {

src/main/java/io/avaje/metrics/core/DefaultBucketTimedMetric.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.avaje.metrics.TimedMetric;
66
import io.avaje.metrics.statistics.MetricStatisticsVisitor;
77

8+
import java.util.function.Supplier;
9+
810
/**
911
* Default implementation of BucketTimedMetric.
1012
*/
@@ -39,6 +41,31 @@ public String getBucketRange() {
3941
return null;
4042
}
4143

44+
@Override
45+
public void time(Runnable event) {
46+
long start = System.nanoTime();
47+
try {
48+
event.run();
49+
add(start);
50+
} catch (RuntimeException e) {
51+
addErr(start);
52+
throw e;
53+
}
54+
}
55+
56+
@Override
57+
public <T> T time(Supplier<T> event) {
58+
long start = System.nanoTime();
59+
try {
60+
final T result = event.get();
61+
add(start);
62+
return result;
63+
} catch (Exception e) {
64+
addErr(start);
65+
throw e;
66+
}
67+
}
68+
4269
@Override
4370
public TimedEvent startEvent() {
4471
return new DefaultTimedMetricEvent(this);
@@ -75,25 +102,25 @@ public void addEventSince(boolean success, long startNanos) {
75102
}
76103

77104
@Override
78-
public void operationEnd(long startNanos) {
105+
public void add(long startNanos) {
79106
addEventSince(true, startNanos);
80107
}
81108

82109
@Override
83-
public void operationEnd(long startNanos, boolean activeThreadContext) {
110+
public void add(long startNanos, boolean activeThreadContext) {
84111
addEventSince(true, startNanos);
85112
if (activeThreadContext) {
86113
NestedContext.pop();
87114
}
88115
}
89116

90117
@Override
91-
public void operationErr(long startNanos) {
118+
public void addErr(long startNanos) {
92119
addEventSince(false, startNanos);
93120
}
94121

95122
@Override
96-
public void operationErr(long startNanos, boolean activeThreadContext) {
123+
public void addErr(long startNanos, boolean activeThreadContext) {
97124
addEventSince(false, startNanos);
98125
if (activeThreadContext) {
99126
NestedContext.pop();
@@ -150,7 +177,7 @@ public void end(boolean withSuccess) {
150177
* Operation or SQL execution).
151178
*/
152179
@Override
153-
public void endWithSuccess() {
180+
public void end() {
154181
end(true);
155182
}
156183

src/main/java/io/avaje/metrics/core/DefaultMetricManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ protected boolean setRequestTimingCollection(MetricName metricName, int collecti
442442
Metric metric = getMetricWithoutCreate(metricName);
443443
if (metric instanceof TimedMetric) {
444444
TimedMetric timed = (TimedMetric) metric;
445-
timed.setRequestTimingCollection(collectionCount);
445+
timed.setRequestTiming(collectionCount);
446446
return true;
447447
}
448448

@@ -472,7 +472,7 @@ public List<TimingMetricInfo> setRequestTimingCollectionUsingMatch(String nameMa
472472
if (metric instanceof TimedMetric) {
473473
TimedMetric timed = (TimedMetric) metric;
474474
if (like.matches(timed.getName().getSimpleName())) {
475-
timed.setRequestTimingCollection(collectionCount);
475+
timed.setRequestTiming(collectionCount);
476476
logger.debug("setRequestTimingCollection({}) on {}", collectionCount, timed.getName().getSimpleName());
477477
changes.add(new TimingMetricInfo(timed.getName().getSimpleName(), collectionCount));
478478
}
@@ -517,11 +517,11 @@ protected List<TimingMetricInfo> getRequestTimingMetrics(boolean activeOnly, Str
517517
for (Metric metric : metricsCache.values()) {
518518
if (metric instanceof TimedMetric) {
519519
TimedMetric timed = (TimedMetric) metric;
520-
if (!activeOnly || timed.getRequestTimingCollection() >= 1) {
520+
if (!activeOnly || timed.getRequestTiming() >= 1) {
521521
// actively collection or doing the all search
522522
if (like.matches(timed.getName().getSimpleName())) {
523523
// metric name matches our expression
524-
list.add(new TimingMetricInfo(timed.getName().getSimpleName(), timed.getRequestTimingCollection()));
524+
list.add(new TimingMetricInfo(timed.getName().getSimpleName(), timed.getRequestTiming()));
525525
}
526526
}
527527
}

src/main/java/io/avaje/metrics/core/DefaultTimedMetric.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.avaje.metrics.statistics.TimedStatistics;
88

99
import java.util.concurrent.TimeUnit;
10+
import java.util.function.Supplier;
1011

1112
/**
1213
* Designed to capture the duration of timed events.
@@ -82,10 +83,35 @@ public MetricName getName() {
8283
return name;
8384
}
8485

86+
@Override
87+
public void time(Runnable event) {
88+
long start = System.nanoTime();
89+
try {
90+
event.run();
91+
add(start);
92+
} catch (RuntimeException e) {
93+
addErr(start);
94+
throw e;
95+
}
96+
}
97+
98+
@Override
99+
public <T> T time(Supplier<T> event) {
100+
long start = System.nanoTime();
101+
try {
102+
final T result = event.get();
103+
add(start);
104+
return result;
105+
} catch (Exception e) {
106+
addErr(start);
107+
throw e;
108+
}
109+
}
110+
85111
/**
86112
* Start an event.
87113
* <p>
88-
* The {@link TimedEvent#endWithSuccess()} or {@link TimedEvent#endWithError()} are called at the
114+
* The {@link TimedEvent#end()} or {@link TimedEvent#endWithError()} are called at the
89115
* completion of the timed event.
90116
* </p>
91117
*/
@@ -118,25 +144,25 @@ public void addEventSince(boolean success, long startNanos) {
118144
}
119145

120146
@Override
121-
public void operationEnd(long startNanos) {
147+
public void add(long startNanos) {
122148
successCounter.add(TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos));
123149
}
124150

125151
@Override
126-
public void operationEnd(long startNanos, boolean activeThreadContext) {
152+
public void add(long startNanos, boolean activeThreadContext) {
127153
successCounter.add(TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos));
128154
if (activeThreadContext) {
129155
NestedContext.pop();
130156
}
131157
}
132158

133159
@Override
134-
public void operationErr(long startNanos) {
160+
public void addErr(long startNanos) {
135161
errorCounter.add(TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos));
136162
}
137163

138164
@Override
139-
public void operationErr(long startNanos, boolean activeThreadContext) {
165+
public void addErr(long startNanos, boolean activeThreadContext) {
140166
errorCounter.add(TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanos));
141167
if (activeThreadContext) {
142168
NestedContext.pop();

src/main/java/io/avaje/metrics/core/DefaultTimedMetricEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void end(boolean withSuccess) {
4242
* Operation or SQL execution).
4343
*/
4444
@Override
45-
public void endWithSuccess() {
45+
public void end() {
4646
end(true);
4747
}
4848

src/main/java/io/avaje/metrics/core/NestedContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void popMetric() {
156156
entries.add(pop);
157157
}
158158
// (if required) decrement the collection count on the metric
159-
pop.getMetric().decrementCollectionCount();
159+
pop.getMetric().decrementRequestTiming();
160160
if (stack.isEmpty()) {
161161
report(entries);
162162
entries = new ArrayList<>();

src/main/java/io/avaje/metrics/core/noop/NoopBucketTimedMetric.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.avaje.metrics.statistics.MetricStatisticsVisitor;
77

88
import java.util.Map;
9+
import java.util.function.Supplier;
910

1011
class NoopBucketTimedMetric implements TimedMetric {
1112

@@ -47,6 +48,16 @@ public void clear() {
4748
// do nothing
4849
}
4950

51+
@Override
52+
public void time(Runnable event) {
53+
event.run();
54+
}
55+
56+
@Override
57+
public <T> T time(Supplier<T> event) {
58+
return event.get();
59+
}
60+
5061
@Override
5162
public TimedEvent startEvent() {
5263
return NOOP_TIMED_EVENT;
@@ -63,42 +74,42 @@ public void addEventDuration(boolean success, long durationNanos) {
6374
}
6475

6576
@Override
66-
public void operationEnd(long startNanos) {
77+
public void add(long startNanos) {
6778
// do nothing
6879
}
6980

7081
@Override
71-
public void operationEnd(long startNanos, boolean activeThreadContext) {
82+
public void add(long startNanos, boolean activeThreadContext) {
7283
// do nothing
7384
}
7485

7586
@Override
76-
public void operationErr(long startNanos) {
87+
public void addErr(long startNanos) {
7788
// do nothing
7889
}
7990

8091
@Override
81-
public void operationErr(long startNanos, boolean activeThreadContext) {
92+
public void addErr(long startNanos, boolean activeThreadContext) {
8293
// do nothing
8394
}
8495

8596
@Override
86-
public void setRequestTimingCollection(int collectionCount) {
97+
public void setRequestTiming(int collectionCount) {
8798
// do nothing
8899
}
89100

90101
@Override
91-
public int getRequestTimingCollection() {
102+
public int getRequestTiming() {
92103
return 0;
93104
}
94105

95106
@Override
96-
public void decrementCollectionCount() {
107+
public void decrementRequestTiming() {
97108

98109
}
99110

100111
@Override
101-
public boolean isActiveThreadContext() {
112+
public boolean isRequestTiming() {
102113
return false;
103114
}
104115

src/main/java/io/avaje/metrics/core/noop/NoopTimedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void end(boolean withSuccess) {
1010
}
1111

1212
@Override
13-
public void endWithSuccess() {
13+
public void end() {
1414
// do nothing
1515
}
1616

0 commit comments

Comments
 (0)