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

direct: fix sampling and visibility issues with new direct client #249

Merged
merged 1 commit into from
May 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* A No-Op {@link NonBlockingDirectStatsDClient}, which can be substituted in when metrics are not
* required.
*/
public final class NoOpDirectStatsDClient extends NoOpStatsDClient implements DirectStatsDClient {
public class NoOpDirectStatsDClient extends NoOpStatsDClient implements DirectStatsDClient {
@Override public void recordDistributionValues(String aspect, double[] values, double sampleRate, String... tags) { }

@Override public void recordDistributionValues(String aspect, long[] values, double sampleRate, String... tags) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
package com.timgroup.statsd;

import static java.nio.charset.StandardCharsets.UTF_8;

final class NonBlockingDirectStatsDClient extends NonBlockingStatsDClient implements DirectStatsDClient {
class NonBlockingDirectStatsDClient extends NonBlockingStatsDClient implements DirectStatsDClient {

public NonBlockingDirectStatsDClient(final NonBlockingStatsDClientBuilder builder) throws StatsDClientException {
super(builder);
}

@Override
public void recordDistributionValues(String aspect, double[] values, double sampleRate, String... tags) {
if ((Double.isNaN(sampleRate) || !isInvalidSample(sampleRate)) && values != null && values.length > 0) {
if (values.length == 1) {
recordDistributionValue(aspect, values[0], sampleRate, tags);
} else {
sendMetric(new DoublesStatsDMessage(aspect, Message.Type.DISTRIBUTION, values, sampleRate, 0, tags));
}
if (values != null && values.length > 0) {
sendMetric(new DoublesStatsDMessage(aspect, Message.Type.DISTRIBUTION, values, sampleRate, 0, tags));
}
}

@Override
public void recordDistributionValues(String aspect, long[] values, double sampleRate, String... tags) {
if ((Double.isNaN(sampleRate) || !isInvalidSample(sampleRate)) && values != null && values.length > 0) {
if (values.length == 1) {
recordDistributionValue(aspect, values[0], sampleRate, tags);
} else {
sendMetric(new LongsStatsDMessage(aspect, Message.Type.DISTRIBUTION, values, sampleRate, 0, tags));
}
if (values != null && values.length > 0) {
sendMetric(new LongsStatsDMessage(aspect, Message.Type.DISTRIBUTION, values, sampleRate, 0, tags));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public void sends_multivalued_distribution_to_statsd_with_sampling_rate() {
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:423:234|d|@1.000000")));
}

@Test(timeout = 5000L)
public void sends_multivalued_distribution_to_statsd_with_non_1_sampling_rate() {
client.recordDistributionValues("mydistribution", new long[] { 423L, 234L }, 0.1);
server.waitForMessage("my.prefix");

assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:423:234|d|@0.100000")));
}

@Test(timeout = 5000L)
public void sends_multivalued_distribution_to_statsd_with_tags_and_sampling_rate() {
client.recordDistributionValues("mydistribution", new long[] { 423L, 234L }, 1, "foo:bar", "baz");
Expand All @@ -97,19 +105,19 @@ public void sends_multivalued_distribution_to_statsd_with_tags_and_sampling_rate
@Test(timeout = 5000L)
public void sends_too_long_multivalued_distribution_to_statsd() {
long[] values = {423L, 234L, 456L, 512L, 345L, 898L, 959876543123L, 667L};
client.recordDistributionValues("mydistribution", values, 1, "foo:bar", "baz");
client.recordDistributionValues("mydistribution", values, 0.4, "foo:bar", "baz");

server.waitForMessage("my.prefix");
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:423:234:456|d|@1.000000|#baz,foo:bar")));
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:423:234:456|d|@0.400000|#baz,foo:bar")));

server.waitForMessage("my.prefix");
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:512:345:898|d|@1.000000|#baz,foo:bar")));
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:512:345:898|d|@0.400000|#baz,foo:bar")));

server.waitForMessage("my.prefix");
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:959876543123|d|@1.000000|#baz,foo:bar")));
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:959876543123|d|@0.400000|#baz,foo:bar")));

server.waitForMessage("my.prefix");
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:667|d|@1.000000|#baz,foo:bar")));
assertThat(server.messagesReceived(), hasItem(comparesEqualTo("my.prefix.mydistribution:667|d|@0.400000|#baz,foo:bar")));
}

}
Loading