-
Notifications
You must be signed in to change notification settings - Fork 296
/
StatsTimer.java
143 lines (127 loc) · 4.19 KB
/
StatsTimer.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
/**
* Copyright 2013 Netflix, Inc.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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 com.netflix.servo.monitor;
import com.netflix.servo.stats.StatsConfig;
import com.netflix.servo.tag.Tags;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* A {@link Timer} that provides statistics.
* <p/>
* The statistics are collected periodically and are published according to the configuration
* specified by the user using a {@link com.netflix.servo.stats.StatsConfig} object. Please
* make sure that the sampleSize corresponds to roughly the number of samples expected in
* a reporting interval. While the statistics collected are accurate for this machine they will not
* be correct if they are aggregated across groups of machines.
* If that is an expected use-case a better
* approach is to use buckets that correspond to different times.
* For example you might have a counter
* that tracks how many calls took < 20ms, one for [ 20ms, 500ms ], and one for > 500ms.
* This bucketing approach can be easily aggregated.
* See {@link com.netflix.servo.monitor.BucketTimer}
*/
public class StatsTimer extends StatsMonitor implements Timer {
private final TimeUnit timeUnit;
private static final String UNIT = "unit";
/**
* Creates a new instance of the timer with a unit of milliseconds, using the default executor.
*/
public StatsTimer(MonitorConfig baseConfig, StatsConfig statsConfig) {
this(baseConfig, statsConfig, TimeUnit.MILLISECONDS, DEFAULT_EXECUTOR);
}
/**
* Creates a new instance of the timer with a given unit, using the default executor.
*/
public StatsTimer(MonitorConfig baseConfig, StatsConfig statsConfig, TimeUnit unit) {
this(baseConfig, statsConfig, unit, DEFAULT_EXECUTOR);
}
/**
* Creates a new instance of the timer with a unit of milliseconds,
* using the {@link ScheduledExecutorService} provided by
* the user.
* To avoid memory leaks the ScheduledExecutorService
* should have the policy to remove tasks from the work queue.
* See {@link java.util.concurrent.ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy(boolean)}
*/
public StatsTimer(MonitorConfig config, StatsConfig statsConfig, TimeUnit unit,
ScheduledExecutorService executor) {
super(config, statsConfig, executor, "totalTime", false, Tags.newTag(UNIT, unit.name()));
this.timeUnit = unit;
startComputingStats();
}
/**
* {@inheritDoc}
*/
@Override
public Stopwatch start() {
Stopwatch s = new TimedStopwatch(this);
s.start();
return s;
}
/**
* {@inheritDoc}
*/
@Override
public TimeUnit getTimeUnit() {
return timeUnit;
}
/**
* {@inheritDoc}
*/
@Override
public void record(long duration, TimeUnit timeUnit) {
record(this.timeUnit.convert(duration, timeUnit));
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "StatsTimer{StatsMonitor=" + super.toString() + ", timeUnit=" + timeUnit + '}';
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof StatsTimer)) {
return false;
}
final StatsTimer m = (StatsTimer) obj;
return super.equals(obj) && timeUnit.equals(m.timeUnit);
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + timeUnit.hashCode();
return result;
}
/**
* Get the number of times this timer has been updated.
*/
public long getCount() {
return count.getValue().longValue();
}
/**
* Get the total time recorded for this timer.
*/
public long getTotalTime() {
return getTotalMeasurement();
}
}