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

Fix unbuffered batch metrics send #128

Merged
merged 3 commits into from
Oct 7, 2018
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
89 changes: 89 additions & 0 deletions src/JustEat.StatsD.Tests/StringBasedStatsDPublisherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using Moq;
using Xunit;

namespace JustEat.StatsD
{
public static class StringBasedStatsDPublisherTests
{
[Fact]
public static void Decrement_Sends_Multiple_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();

var config = new StatsDConfiguration
{
Prefix = "red",
};

var publisher = new StringBasedStatsDPublisher(config, mock.Object);

// Act
publisher.Decrement(10, 1, "white", "blue");

// Assert
mock.Verify((p) => p.Send("red.white:-10|c"), Times.Once());
mock.Verify((p) => p.Send("red.blue:-10|c"), Times.Once());
}

[Fact]
public static void Increment_Sends_Multiple_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();

var config = new StatsDConfiguration
{
Prefix = "red",
};

var publisher = new StringBasedStatsDPublisher(config, mock.Object);

// Act
publisher.Increment(10, 1, "white", "blue");

// Assert
mock.Verify((p) => p.Send("red.white:10|c"), Times.Once());
mock.Verify((p) => p.Send("red.blue:10|c"), Times.Once());
}

[Fact]
public static void Metrics_Not_Sent_If_Array_Is_Null_Or_Empty()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var config = new StatsDConfiguration();

var publisher = new StringBasedStatsDPublisher(config, mock.Object);

// Act
publisher.Decrement(1, 1, null as string[]);
publisher.Increment(1, 1, null as string[]);
publisher.Decrement(1, 1, Array.Empty<string>());
publisher.Increment(1, 1, Array.Empty<string>());
publisher.Decrement(1, 1, new[] { string.Empty });
publisher.Increment(1, 1, new[] { string.Empty });

// Assert
mock.Verify((p) => p.Send(It.IsAny<string>()), Times.Never());
}

[Fact]
public static void Metrics_Not_Sent_If_No_Metrics()
{
// Arrange
var mock = new Mock<IStatsDTransport>();
var config = new StatsDConfiguration();

var publisher = new StringBasedStatsDPublisher(config, mock.Object);

// Act
publisher.Decrement(1, 0, new[] { "foo" });
publisher.Increment(1, 0, new[] { "bar" });

// Assert
mock.Verify((p) => p.Send(It.IsAny<string>()), Times.Never());
}
}
}
9 changes: 5 additions & 4 deletions src/JustEat.StatsD/StatsDMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ public class StatsDMessageFormatter
private readonly string _prefix;

public StatsDMessageFormatter()
: this(string.Empty) {}
: this(string.Empty)
{
}

public StatsDMessageFormatter(string prefix)
public StatsDMessageFormatter(string prefix)
{
_prefix = prefix;

if (!string.IsNullOrWhiteSpace(_prefix))
{
_prefix = _prefix + "."; // if we have something, then append a . to it to make concatenations easy.
_prefix = _prefix + "."; // If we have something, then append a '.' to it to make concatenations easy.
}
}

Expand All @@ -51,7 +53,6 @@ public string Decrement(long magnitude, string statBucket)
return Decrement(magnitude, DefaultSampleRate, statBucket);
}


public string Decrement(long magnitude, double sampleRate, string statBucket)
{
magnitude = magnitude < 0 ? magnitude : -magnitude;
Expand Down
31 changes: 29 additions & 2 deletions src/JustEat.StatsD/StringBasedStatsDPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ public void Increment(long value, double sampleRate, string bucket)

public void Increment(long value, double sampleRate, params string[] buckets)
{
Send(_formatter.Increment(value, sampleRate, buckets));
if (buckets == null || buckets.Length == 0)
{
return;
}

foreach (string bucket in buckets)
{
if (!string.IsNullOrEmpty(bucket))
{
Send(_formatter.Increment(value, sampleRate, bucket));
}
}
}

public void Decrement(string bucket)
Expand All @@ -79,7 +90,18 @@ public void Decrement(long value, double sampleRate, string bucket)

public void Decrement(long value, double sampleRate, params string[] buckets)
{
Send(_formatter.Decrement(value, sampleRate, buckets));
if (buckets == null || buckets.Length == 0)
{
return;
}

foreach (string bucket in buckets)
{
if (!string.IsNullOrEmpty(bucket))
{
Send(_formatter.Decrement(value, sampleRate, bucket));
}
}
}

public void Gauge(double value, string bucket)
Expand Down Expand Up @@ -119,6 +141,11 @@ public void MarkEvent(string name)

private void Send(string metric)
{
if (metric.Length == 0)
{
return;
}

try
{
_transport.Send(metric);
Expand Down