Skip to content

Commit

Permalink
Merge pull request #178 from DataDog/olivierg/metric-exceeds-buffer
Browse files Browse the repository at this point in the history
Don't throw an exception when the metric is too long
  • Loading branch information
ogaca-dd authored Jun 23, 2022
2 parents 9a8dab4 + 54f2002 commit 5099f21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/StatsdClient/Bufferize/BufferBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Text;

namespace StatsdClient.Bufferize
Expand Down Expand Up @@ -47,7 +48,8 @@ public void Add(SerializedMetric serializedMetric)

if (length < 0)
{
throw new InvalidOperationException($"The metric size exceeds the internal buffer capacity {_charsBuffers.Length}: {serializedMetric.ToString()}");
Debug.WriteLine($"The metric size exceeds the internal buffer capacity {_charsBuffers.Length}: {serializedMetric.ToString()}");
return;
}

// Heuristic to know if there is enough space without calling `GetByteCount`.
Expand All @@ -59,7 +61,8 @@ public void Add(SerializedMetric serializedMetric)

if (byteCount > Capacity)
{
throw new InvalidOperationException($"The metric size exceeds the buffer capacity {Capacity}: {serializedMetric.ToString()}");
Debug.WriteLine($"The metric size exceeds the buffer capacity {Capacity}: {serializedMetric.ToString()}");
return;
}

// For separator
Expand Down
3 changes: 0 additions & 3 deletions tests/StatsdClient.Tests/Bufferize/BufferBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public void HandleBufferAndReset()
[Test]
public void AddReturnedValue()
{
Assert.Throws<InvalidOperationException>(() => _bufferBuilder.Add(CreateSerializedMetric('1', _bufferBuilder.Capacity + 1)));
Assert.AreEqual(0, _bufferBuilder.Length);

_bufferBuilder.Add(CreateSerializedMetric('1', _bufferBuilder.Capacity - 1)); // -1 for separator
Assert.AreEqual(_bufferBuilder.Capacity, _bufferBuilder.Length);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/StatsdClient.Tests/DogStatsdServiceConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,32 @@ public void Setting_tag_with_env_arg(string envVar, string tag)
}
}

[Test]
[Timeout(5000)]
public void Test_message_too_long()
{
using (var service = new DogStatsdService())
{
var metricsConfig = new StatsdConfig
{
StatsdServerName = "127.0.0.1",
StatsdMaxUDPPacketSize = 10,
};
service.Configure(metricsConfig);

var receivedData = ReceiveData(
service,
metricsConfig.StatsdServerName,
8125,
() =>
{
service.Increment("test");
service.Increment("too_long_message_which_will_be_dropped");
});
Assert.AreEqual(new List<string> { "test:1|c\n" }, receivedData);
}
}

private List<string> ReceiveData(DogStatsdService dogstasdService, string testServerName, int testPort, Action sendData)
{
using (var udpListener = new UdpListener(testServerName, testPort))
Expand Down

0 comments on commit 5099f21

Please sign in to comment.