Repository navigation
Expand file tree
/
Copy pathSentryFunctionTimer.java
More file actions
96 lines (84 loc) · 3 KB
/
Copy pathSentryFunctionTimer.java
File metadata and controls
96 lines (84 loc) · 3 KB
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
package io.sentry.micrometer;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.cumulative.CumulativeFunctionTimer;
import io.sentry.util.ExceptionUtils;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;
import java.util.function.ToLongFunction;
import org.jetbrains.annotations.NotNull;
final class SentryFunctionTimer<T> extends CumulativeFunctionTimer<T>
implements SentryRemovableMeter {
private final @NotNull SentryMeterRegistry registry;
private final @NotNull SentryMetricInfo countMetricInfo;
private final @NotNull SentryMetricInfo totalTimeMetricInfo;
private volatile boolean removed;
private boolean countInitialized;
private double previousCount;
private boolean totalTimeInitialized;
private double previousTotalTime;
SentryFunctionTimer(
final @NotNull Meter.Id id,
final @NotNull T obj,
final @NotNull ToLongFunction<T> countFunction,
final @NotNull ToDoubleFunction<T> totalTimeFunction,
final @NotNull TimeUnit totalTimeFunctionUnit,
final @NotNull TimeUnit baseTimeUnit,
final @NotNull SentryMeterRegistry registry,
final @NotNull SentryMetricInfo countMetricInfo,
final @NotNull SentryMetricInfo totalTimeMetricInfo) {
super(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnit, baseTimeUnit);
this.registry = registry;
this.countMetricInfo = countMetricInfo;
this.totalTimeMetricInfo = totalTimeMetricInfo;
}
void poll() {
try {
pollCount();
} catch (Throwable throwable) {
ExceptionUtils.rethrowIfFatal(throwable);
registry.logPollingFailure(throwable, countMetricInfo.getName());
}
try {
pollTotalTime();
} catch (Throwable throwable) {
ExceptionUtils.rethrowIfFatal(throwable);
registry.logPollingFailure(throwable, totalTimeMetricInfo.getName());
}
}
private void pollCount() {
final double currentCount = count();
if (!Double.isFinite(currentCount) || removed || registry.isClosed()) {
return;
}
if (!countInitialized || currentCount < previousCount) {
countInitialized = true;
previousCount = currentCount;
return;
}
final double delta = currentCount - previousCount;
previousCount = currentCount;
if (delta > 0.0 && !removed) {
registry.captureCounter(countMetricInfo, delta);
}
}
private void pollTotalTime() {
final double currentTotalTime = totalTime(TimeUnit.MILLISECONDS);
if (!Double.isFinite(currentTotalTime) || removed || registry.isClosed()) {
return;
}
if (!totalTimeInitialized || currentTotalTime < previousTotalTime) {
totalTimeInitialized = true;
previousTotalTime = currentTotalTime;
return;
}
final double delta = currentTotalTime - previousTotalTime;
previousTotalTime = currentTotalTime;
if (delta > 0.0 && !removed) {
registry.captureCounter(totalTimeMetricInfo, delta);
}
}
@Override
public void markRemoved() {
removed = true;
}
}