Skip to content

Commit 6c42252

Browse files
committed
tomd: support sample rates for counters -- closes #10
1 parent 84030ff commit 6c42252

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

src/main/java/com/timgroup/statsd/ConvenienceMethodProvidingStatsDClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public final void time(String aspect, long timeInMs) {
6767
recordExecutionTime(aspect, timeInMs);
6868
}
6969

70+
@Override
71+
public final void recordExecutionTime(String aspect, long timeInMs) {
72+
recordExecutionTime(aspect, timeInMs, 1.0);
73+
}
74+
7075
@Override
7176
public void recordExecutionTimeToNow(String aspect, long systemTimeMillisAtStart) {
7277
time(aspect, Math.max(0, System.currentTimeMillis() - systemTimeMillisAtStart));

src/main/java/com/timgroup/statsd/NoOpStatsDClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public final class NoOpStatsDClient extends ConvenienceMethodProvidingStatsDClie
1313
@Override public void recordGaugeValue(String aspect, long value) { }
1414
@Override public void recordGaugeDelta(String aspect, long delta) { }
1515
@Override public void recordSetEvent(String aspect, String value) { }
16-
@Override public void recordExecutionTime(String aspect, long timeInMs) { }
16+
@Override public void recordExecutionTime(String aspect, long timeInMs, double sampleRate) { }
1717
}

src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public void recordSetEvent(String aspect, String eventName) {
199199
* the time in milliseconds
200200
*/
201201
@Override
202-
public void recordExecutionTime(String aspect, long timeInMs) {
203-
send(messageFor(aspect, timeInMs, "ms"));
202+
public void recordExecutionTime(String aspect, long timeInMs, double sampleRate) {
203+
send(messageFor(aspect, timeInMs, "ms", sampleRate));
204204
}
205205

206206
private String messageFor(String aspect, Object value, String type) {

src/main/java/com/timgroup/statsd/StatsDClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ public interface StatsDClient {
140140
*/
141141
void recordExecutionTime(String aspect, long timeInMs);
142142

143+
/**
144+
* Adjusts the specified counter by a given delta.
145+
*
146+
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
147+
*
148+
* @param aspect
149+
* the name of the counter to adjust
150+
* @param delta
151+
* the amount to adjust the counter by
152+
* @param sampleRate
153+
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this timer is being sent
154+
* sampled every 1/10th of the time, so that it updates its timer_counters appropriately.
155+
*/
156+
void recordExecutionTime(String aspect, long timeInMs, double sampleRate);
157+
143158
/**
144159
* Records an execution time in milliseconds for the specified named operation. The execution
145160
* time is calculated as the delta between the specified start time and the current system

src/test/java/com/timgroup/statsd/NonBlockingStatsDClientTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ public void stop() throws Exception {
119119
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms"));
120120
}
121121

122+
@Test(timeout=5000L) public void
123+
sends_timer_with_rate_to_statsd() throws Exception {
124+
client.recordExecutionTime("mytime", 123L, 0.000123);
125+
server.waitForMessage();
126+
127+
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|[email protected]"));
128+
}
129+
122130
@Test(timeout=5000L) public void
123131
sends_timer_to_statsd_based_on_specified_start_time_to_now() throws Exception {
124132
final long startTime = System.currentTimeMillis() - 1000L;

0 commit comments

Comments
 (0)