Skip to content

Commit

Permalink
tomd: support sample rates for counters -- closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
scarytom committed Jul 28, 2014
1 parent 84030ff commit 6c42252
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public final void time(String aspect, long timeInMs) {
recordExecutionTime(aspect, timeInMs);
}

@Override
public final void recordExecutionTime(String aspect, long timeInMs) {
recordExecutionTime(aspect, timeInMs, 1.0);
}

@Override
public void recordExecutionTimeToNow(String aspect, long systemTimeMillisAtStart) {
time(aspect, Math.max(0, System.currentTimeMillis() - systemTimeMillisAtStart));
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 @@ -13,5 +13,5 @@ public final class NoOpStatsDClient extends ConvenienceMethodProvidingStatsDClie
@Override public void recordGaugeValue(String aspect, long value) { }
@Override public void recordGaugeDelta(String aspect, long delta) { }
@Override public void recordSetEvent(String aspect, String value) { }
@Override public void recordExecutionTime(String aspect, long timeInMs) { }
@Override public void recordExecutionTime(String aspect, long timeInMs, double sampleRate) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ public void recordSetEvent(String aspect, String eventName) {
* the time in milliseconds
*/
@Override
public void recordExecutionTime(String aspect, long timeInMs) {
send(messageFor(aspect, timeInMs, "ms"));
public void recordExecutionTime(String aspect, long timeInMs, double sampleRate) {
send(messageFor(aspect, timeInMs, "ms", sampleRate));
}

private String messageFor(String aspect, Object value, String type) {
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 @@ -140,6 +140,21 @@ public interface StatsDClient {
*/
void recordExecutionTime(String aspect, long timeInMs);

/**
* 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 timer is being sent
* sampled every 1/10th of the time, so that it updates its timer_counters appropriately.
*/
void recordExecutionTime(String aspect, long timeInMs, double sampleRate);

/**
* Records an execution time in milliseconds for the specified named operation. The execution
* time is calculated as the delta between the specified start time and the current system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public void stop() throws Exception {
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms"));
}

@Test(timeout=5000L) public void
sends_timer_with_rate_to_statsd() throws Exception {
client.recordExecutionTime("mytime", 123L, 0.000123);
server.waitForMessage();

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

@Test(timeout=5000L) public void
sends_timer_to_statsd_based_on_specified_start_time_to_now() throws Exception {
final long startTime = System.currentTimeMillis() - 1000L;
Expand Down

0 comments on commit 6c42252

Please sign in to comment.