Skip to content

Commit

Permalink
tomd: support sample rates for counters, for issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
scarytom committed Jul 28, 2014
1 parent 47f83a1 commit 84030ff
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public ConvenienceMethodProvidingStatsDClient() {
super();
}

@Override
public final void count(String aspect, long delta) {
count(aspect, delta, 1.0);
}

/**
* Convenience method equivalent to {@link #count(String, long)} with a value of 1.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/timgroup/statsd/NoOpStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public final class NoOpStatsDClient extends ConvenienceMethodProvidingStatsDClient {
@Override public void stop() { }
@Override public void count(String aspect, long delta) { }
@Override public void count(String aspect, long delta, double sampleRate) { }
@Override public void recordGaugeValue(String aspect, long value) { }
@Override public void recordGaugeDelta(String aspect, long delta) { }
@Override public void recordSetEvent(String aspect, String value) { }
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/timgroup/statsd/NonBlockingStatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ public void stop() {
* the name of the counter to adjust
* @param delta
* the amount to adjust the counter by
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
@Override
public void count(String aspect, long delta) {
send(messageFor(aspect, delta, "c"));
public void count(String aspect, long delta, double sampleRate) {
send(messageFor(aspect, delta, "c", sampleRate));
}

/**
Expand Down Expand Up @@ -201,7 +204,12 @@ public void recordExecutionTime(String aspect, long timeInMs) {
}

private String messageFor(String aspect, Object value, String type) {
return String.format("%s%s:%s|%s", prefix, aspect, value, type);
return messageFor(aspect, value, type, 1.0);
}

private String messageFor(String aspect, Object value, String type, double sampleRate) {
final String messageFormat = (sampleRate == 1.0) ? "%s%s:%s|%s" : "%s%s:%s|%s@%f";
return String.format(messageFormat, prefix, aspect, value, type, sampleRate);
}

private void send(final String message) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/timgroup/statsd/StatsDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ public interface StatsDClient {
*/
void count(String aspect, long delta);

/**
* Adjusts the specified counter by a given delta.
*
* <p>This method is non-blocking and is guaranteed not to throw an exception.</p>
*
* @param aspect
* the name of the counter to adjust
* @param delta
* the amount to adjust the counter by
* @param sampleRate
* the sampling rate being employed. For example, a rate of 0.1 would tell StatsD that this counter is being sent
* sampled every 1/10th of the time.
*/
void count(String aspect, long delta, double sampleRate);

/**
* Increments the specified counter by one.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public void stop() throws Exception {
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c"));
}

@Test(timeout=5000L) public void
sends_counter_value_with_rate_to_statsd() throws Exception {
client.count("mycount", Long.MAX_VALUE, 0.00024);
server.waitForMessage();

assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|[email protected]"));
}

@Test(timeout=5000L) public void
sends_counter_increment_to_statsd() throws Exception {
client.incrementCounter("myinc");
Expand Down

0 comments on commit 84030ff

Please sign in to comment.