-
Notifications
You must be signed in to change notification settings - Fork 962
/
AbstractTimer.java
211 lines (184 loc) · 7.92 KB
/
AbstractTimer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright 2017 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument;
import io.micrometer.core.instrument.distribution.*;
import io.micrometer.core.instrument.distribution.pause.ClockDriftPauseDetector;
import io.micrometer.core.instrument.distribution.pause.NoPauseDetector;
import io.micrometer.core.instrument.distribution.pause.PauseDetector;
import io.micrometer.core.lang.Nullable;
import org.LatencyUtils.IntervalEstimator;
import org.LatencyUtils.SimplePauseDetector;
import org.LatencyUtils.TimeCappedMovingAverageIntervalEstimator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public abstract class AbstractTimer extends AbstractMeter implements Timer {
private static final Map<PauseDetector, Object> pauseDetectorCache = new ConcurrentHashMap<>();
protected final Clock clock;
protected final Histogram histogram;
private final TimeUnit baseTimeUnit;
// Only used when pause detection is enabled
@Nullable
private Object intervalEstimator;
@Nullable
private org.LatencyUtils.PauseDetector pauseDetector;
/**
* Creates a new timer.
* @param id The timer's name and tags.
* @param clock The clock used to measure latency.
* @param distributionStatisticConfig Configuration determining which distribution
* statistics are sent.
* @param pauseDetector Compensation for coordinated omission.
* @param baseTimeUnit The time scale of this timer.
* @deprecated Timer implementations should now declare at construction time whether
* they support aggregable percentiles or not. By declaring it up front, Micrometer
* can memory optimize the histogram structure used to store distribution statistics.
*/
@Deprecated
protected AbstractTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetector, TimeUnit baseTimeUnit) {
this(id, clock, distributionStatisticConfig, pauseDetector, baseTimeUnit, false);
}
/**
* Creates a new timer.
* @param id The timer's name and tags.
* @param clock The clock used to measure latency.
* @param distributionStatisticConfig Configuration determining which distribution
* statistics are sent.
* @param pauseDetector Compensation for coordinated omission.
* @param baseTimeUnit The time scale of this timer.
* @param supportsAggregablePercentiles Indicates whether the registry supports
* percentile approximations from histograms.
*/
protected AbstractTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetector, TimeUnit baseTimeUnit, boolean supportsAggregablePercentiles) {
super(id);
this.clock = clock;
this.baseTimeUnit = baseTimeUnit;
initPauseDetector(pauseDetector);
if (distributionStatisticConfig.isPublishingPercentiles()) {
// hdr-based histogram
this.histogram = new TimeWindowPercentileHistogram(clock, distributionStatisticConfig,
supportsAggregablePercentiles);
}
else if (distributionStatisticConfig.isPublishingHistogram()) {
// fixed boundary histograms, which have a slightly better memory footprint
// when we don't need Micrometer-computed percentiles
this.histogram = new TimeWindowFixedBoundaryHistogram(clock, distributionStatisticConfig,
supportsAggregablePercentiles);
}
else {
// noop histogram
this.histogram = NoopHistogram.INSTANCE;
}
}
private void initPauseDetector(PauseDetector pauseDetectorType) {
if (pauseDetectorType instanceof NoPauseDetector) {
return;
}
pauseDetector = (org.LatencyUtils.PauseDetector) pauseDetectorCache.computeIfAbsent(pauseDetectorType,
detector -> {
if (detector instanceof ClockDriftPauseDetector) {
ClockDriftPauseDetector clockDriftPauseDetector = (ClockDriftPauseDetector) detector;
return new SimplePauseDetector(clockDriftPauseDetector.getSleepInterval().toNanos(),
clockDriftPauseDetector.getPauseThreshold().toNanos(), 1, false);
}
return null;
});
if (pauseDetector instanceof SimplePauseDetector) {
this.intervalEstimator = new TimeCappedMovingAverageIntervalEstimator(128, 10000000000L, pauseDetector);
pauseDetector.addListener((pauseLength, pauseEndTime) -> {
if (intervalEstimator != null) {
long estimatedInterval = ((IntervalEstimator) intervalEstimator).getEstimatedInterval(pauseEndTime);
long observedLatencyMinbar = pauseLength - estimatedInterval;
if (observedLatencyMinbar >= estimatedInterval) {
recordValueWithExpectedInterval(observedLatencyMinbar, estimatedInterval);
}
}
});
}
}
private void recordValueWithExpectedInterval(long nanoValue, long expectedIntervalBetweenValueSamples) {
record(nanoValue, TimeUnit.NANOSECONDS);
if (expectedIntervalBetweenValueSamples <= 0)
return;
for (long missingValue = nanoValue
- expectedIntervalBetweenValueSamples; missingValue >= expectedIntervalBetweenValueSamples; missingValue -= expectedIntervalBetweenValueSamples) {
record(missingValue, TimeUnit.NANOSECONDS);
}
}
@Override
public <T> T recordCallable(Callable<T> f) throws Exception {
final long s = clock.monotonicTime();
try {
return f.call();
}
finally {
final long e = clock.monotonicTime();
record(e - s, TimeUnit.NANOSECONDS);
}
}
@Override
public <T> T record(Supplier<T> f) {
final long s = clock.monotonicTime();
try {
return f.get();
}
finally {
final long e = clock.monotonicTime();
record(e - s, TimeUnit.NANOSECONDS);
}
}
@Override
public void record(Runnable f) {
final long s = clock.monotonicTime();
try {
f.run();
}
finally {
final long e = clock.monotonicTime();
record(e - s, TimeUnit.NANOSECONDS);
}
}
@Override
public final void record(long amount, TimeUnit unit) {
if (amount >= 0) {
histogram.recordLong(TimeUnit.NANOSECONDS.convert(amount, unit));
recordNonNegative(amount, unit);
if (intervalEstimator != null) {
((IntervalEstimator) intervalEstimator).recordInterval(clock.monotonicTime());
}
}
}
protected abstract void recordNonNegative(long amount, TimeUnit unit);
@Override
public HistogramSnapshot takeSnapshot() {
return histogram.takeSnapshot(count(), totalTime(TimeUnit.NANOSECONDS), max(TimeUnit.NANOSECONDS));
}
@Override
public TimeUnit baseTimeUnit() {
return baseTimeUnit;
}
@Override
public void close() {
histogram.close();
if (pauseDetector != null) {
pauseDetector.shutdown();
}
}
}