Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Socket madness problem fix (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-nochevnyi authored Jun 22, 2023
1 parent c97a240 commit 044aa73
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/BatchedDogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class BatchedDogStatsd extends DogStatsd
{
public static $maxCountMessagesOnSocketSession = 0;
public static $maxBufferLength = 50;
private static $buffer = [];
private static $bufferLength = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/DogStatsd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion tests/UnitTests/BatchedDogStatsdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ 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(
1,
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,
Expand Down
72 changes: 72 additions & 0 deletions tests/UnitTests/DogStatsd/CountSentMessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace DataDog\UnitTests\DogStatsd;

use DataDog\DogStatsd;
use DataDog\TestHelpers\SocketSpyTestCase;

class CountSentMessagesTest extends SocketSpyTestCase
{
public function testReportDoesNotSendIfBufferNotFilled()
{
$batchedDog = new DogStatsd();

$batchedDog->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'
);
}
}

0 comments on commit 044aa73

Please sign in to comment.