Skip to content

Commit 84030ff

Browse files
committed
tomd: support sample rates for counters, for issue #10
1 parent 47f83a1 commit 84030ff

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public ConvenienceMethodProvidingStatsDClient() {
66
super();
77
}
88

9+
@Override
10+
public final void count(String aspect, long delta) {
11+
count(aspect, delta, 1.0);
12+
}
13+
914
/**
1015
* Convenience method equivalent to {@link #count(String, long)} with a value of 1.
1116
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public final class NoOpStatsDClient extends ConvenienceMethodProvidingStatsDClient {
1111
@Override public void stop() { }
12-
@Override public void count(String aspect, long delta) { }
12+
@Override public void count(String aspect, long delta, double sampleRate) { }
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) { }

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ public void stop() {
140140
* the name of the counter to adjust
141141
* @param delta
142142
* the amount to adjust the counter by
143+
* @param sampleRate
144+
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
145+
* sampled every 1/10th of the time.
143146
*/
144147
@Override
145-
public void count(String aspect, long delta) {
146-
send(messageFor(aspect, delta, "c"));
148+
public void count(String aspect, long delta, double sampleRate) {
149+
send(messageFor(aspect, delta, "c", sampleRate));
147150
}
148151

149152
/**
@@ -201,7 +204,12 @@ public void recordExecutionTime(String aspect, long timeInMs) {
201204
}
202205

203206
private String messageFor(String aspect, Object value, String type) {
204-
return String.format("%s%s:%s|%s", prefix, aspect, value, type);
207+
return messageFor(aspect, value, type, 1.0);
208+
}
209+
210+
private String messageFor(String aspect, Object value, String type, double sampleRate) {
211+
final String messageFormat = (sampleRate == 1.0) ? "%s%s:%s|%s" : "%s%s:%s|%s@%f";
212+
return String.format(messageFormat, prefix, aspect, value, type, sampleRate);
205213
}
206214

207215
private void send(final String message) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public interface StatsDClient {
3636
*/
3737
void count(String aspect, long delta);
3838

39+
/**
40+
* Adjusts the specified counter by a given delta.
41+
*
42+
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
43+
*
44+
* @param aspect
45+
* the name of the counter to adjust
46+
* @param delta
47+
* the amount to adjust the counter by
48+
* @param sampleRate
49+
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
50+
* sampled every 1/10th of the time.
51+
*/
52+
void count(String aspect, long delta, double sampleRate);
53+
3954
/**
4055
* Increments the specified counter by one.
4156
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public void stop() throws Exception {
3939
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c"));
4040
}
4141

42+
@Test(timeout=5000L) public void
43+
sends_counter_value_with_rate_to_statsd() throws Exception {
44+
client.count("mycount", Long.MAX_VALUE, 0.00024);
45+
server.waitForMessage();
46+
47+
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|[email protected]"));
48+
}
49+
4250
@Test(timeout=5000L) public void
4351
sends_counter_increment_to_statsd() throws Exception {
4452
client.incrementCounter("myinc");

0 commit comments

Comments
 (0)