Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements sampling #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<pathelement location="${build.dir}/${ant.project.name}-test-${version.label}.jar"/>
</classpath>
<formatter type="xml"/>
<formatter type="brief" usefile="false" />
<batchtest fork="yes" todir="${build.dir}/testreport">
<zipfileset src="${build.dir}/${ant.project.name}-test-${version.label}.jar">
<include name="com/timgroup/statsd/**/*Test.class"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.timgroup.statsd;

import java.util.concurrent.ThreadLocalRandom;


public abstract class ConvenienceMethodProvidingStatsDClient implements StatsDClient {

public ConvenienceMethodProvidingStatsDClient() {
Expand Down Expand Up @@ -84,4 +87,11 @@ public final void recordExecutionTime(String aspect, long timeInMs) {
public void recordExecutionTimeToNow(String aspect, long systemTimeMillisAtStart) {
time(aspect, Math.max(0, System.currentTimeMillis() - systemTimeMillisAtStart));
}
}

/**
* Returns true when the client should send a message, given a sample rate.
*/
protected Boolean shouldSend(double sampleRate) {
return ThreadLocalRandom.current().nextDouble() <= sampleRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public void stop() {
*/
@Override
public void count(String aspect, long delta, double sampleRate) {
if (!shouldSend(sampleRate)) {
return;
}
send(messageFor(aspect, Long.toString(delta), "c", sampleRate));
}

Expand Down Expand Up @@ -190,14 +193,17 @@ public void recordSetEvent(String aspect, String eventName) {
*/
@Override
public void recordExecutionTime(String aspect, long timeInMs, double sampleRate) {
if (!shouldSend(sampleRate)) {
return;
}
send(messageFor(aspect, Long.toString(timeInMs), "ms", sampleRate));
}

private String messageFor(String aspect, String value, String type) {
return messageFor(aspect, value, type, 1.0);
}

private String messageFor(String aspect, String value, String type, double sampleRate) {
private String messageFor(String aspect, String value, String type, double sampleRate) {
final String message = prefix + aspect + ':' + value + '|' + type;
return (sampleRate == 1.0)
? message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public void stop() throws Exception {

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

assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c|@0.00024"));
assertThat(server.messagesReceived(), contains("my.prefix.mycount:9223372036854775807|c|@0.999999"));
}

@Test(timeout=5000L) public void
Expand Down Expand Up @@ -146,10 +146,10 @@ public void stop() throws Exception {

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

assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms|@0.000123"));
assertThat(server.messagesReceived(), contains("my.prefix.mytime:123|ms|@0.999999"));
}

@Test(timeout=5000L) public void
Expand Down Expand Up @@ -240,4 +240,4 @@ public List<String> messagesReceived() {
return new ArrayList<String>(messagesReceived);
}
}
}
}