diff --git a/CHANGELOG.md b/CHANGELOG.md index e63e261..b71c73e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ CHANGELOG [//]: # (comment: Don't forget to update src/DogStatsd.php:DogStatsd::version when releasing a new version) +# 1.6.1 / 2023-06-22 +* Added message counter +* Close socket after `$maxCountMessagesOnSocketSession` number of messages sent + # 1.6.0 / 2023-05-1 * Lazy creation of sockets * Resend on socket errors. Default max attempts is 2. Can be configured with `max_attempts_to_send` option. diff --git a/src/BatchedDogStatsd.php b/src/BatchedDogStatsd.php index 4da6673..a59c940 100644 --- a/src/BatchedDogStatsd.php +++ b/src/BatchedDogStatsd.php @@ -14,6 +14,7 @@ */ class BatchedDogStatsd extends DogStatsd { + public static $maxCountMessagesOnSocketSession = 0; public static $maxBufferLength = 50; private static $buffer = []; private static $bufferLength = 0; diff --git a/src/DogStatsd.php b/src/DogStatsd.php index fda6af4..9f7acaf 100644 --- a/src/DogStatsd.php +++ b/src/DogStatsd.php @@ -15,7 +15,7 @@ class DogStatsd const DEFAULT_MAX_ATTEMPTS_TO_SEND = 1; // phpcs:enable - public static $version = '1.6.0'; + public static $version = '1.6.1'; private static $eventUrl = '/api/v1/events'; /** * @var bool|resource|\Socket @@ -87,6 +87,9 @@ class DogStatsd // Used for the telemetry tags private $packets_dropped; + private $countSentMessages = 0; + public static $maxCountMessagesOnSocketSession = 50; + /** * DogStatsd constructor, takes a configuration array. The configuration can take any of the following values: * host, @@ -508,6 +511,9 @@ protected function sendMessage($message, $attempt = 0) return $this->sendMessage($message, ++$attempt); } } + } elseif (++$this->countSentMessages > static::$maxCountMessagesOnSocketSession) { + $this->closeSocket(); + $this->countSentMessages = 0; } return $res; diff --git a/tests/UnitTests/BatchedDogStatsdTest.php b/tests/UnitTests/BatchedDogStatsdTest.php index 66c30a8..a96570c 100644 --- a/tests/UnitTests/BatchedDogStatsdTest.php +++ b/tests/UnitTests/BatchedDogStatsdTest.php @@ -38,12 +38,12 @@ public function testReportSendsOnceBufferIsFilled() $batchedDog->gauge($udpMessage . '2', 21); $spy = $this->getSocketSpy(); + $this->assertSame( 0, count($spy->argsFromSocketSendtoCalls), 'Should not have sent any message until the buffer is full' ); - $batchedDog->gauge($udpMessage . '3', 21); $this->assertSame( @@ -51,6 +51,11 @@ public function testReportSendsOnceBufferIsFilled() count($spy->argsFromSocketSendtoCalls), 'Should send all buffered UDP messages once buffer is filled' ); + $this->assertSame( + 1, + count($spy->argsFromSocketCloseCalls), + 'Socket was not closed' + ); $this->assertSameTelemetry( $expectedUdpMessageOnceSent, diff --git a/tests/UnitTests/DogStatsd/CountSentMessagesTest.php b/tests/UnitTests/DogStatsd/CountSentMessagesTest.php new file mode 100644 index 0000000..bdbce22 --- /dev/null +++ b/tests/UnitTests/DogStatsd/CountSentMessagesTest.php @@ -0,0 +1,72 @@ +report('some fake UDP message'); + + $spy = $this->getSocketSpy(); + + $this->assertSame( + 1, + count($spy->argsFromSocketSendtoCalls), + 'Should sent UDP message' + ); + + $this->assertSame( + 0, + count($spy->argsFromSocketCloseCalls), + 'Socket should be not closed' + ); + } + + public function testReportSendsOnceBufferIsFilled() + { + $batchedDog = new DogStatsd(); + + $batchedDog::$maxCountMessagesOnSocketSession = 2; + + $udpMessage = 'some fake UDP message'; + + $spy = $this->getSocketSpy(); + + $batchedDog->gauge($udpMessage . '1', 21); + $batchedDog->gauge($udpMessage . '2', 21); + $batchedDog->gauge($udpMessage . '3', 21); + + $this->assertSame( + 3, + count($spy->argsFromSocketSendtoCalls), + 'Should send all UDP messages' + ); + $this->assertSame( + 1, + count($spy->argsFromSocketCloseCalls), + 'Socket was not closed' + ); + + $this->assertSame( + "some fake UDP message1:21|g\n", + $spy->argsFromSocketSendtoCalls[0][1], + 'Should concatenate UDP messages with newlines' + ); + $this->assertSame( + "some fake UDP message2:21|g\n", + $spy->argsFromSocketSendtoCalls[1][1], + 'Should concatenate UDP messages with newlines' + ); + $this->assertSame( + "some fake UDP message3:21|g\n", + $spy->argsFromSocketSendtoCalls[2][1], + 'Should concatenate UDP messages with newlines' + ); + } +}