Skip to content

Commit

Permalink
Merge pull request #21 from hellofresh/patch/PO-2958_silent_client
Browse files Browse the repository at this point in the history
[PATCH/PO-2958] creates SilentClient that does not trigger errors when configured to do so
  • Loading branch information
ehkasper authored Mar 14, 2023
2 parents 81db372 + d53d73b commit 697da42
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/Client/StatsD.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class StatsD extends AbstractClient implements Client
/**
* StatsD constructor.
*
* @param string $dsn statsd connection dsn
* @param string $dsn statsd connection dsn
* @param Client $client statsd client
*
* @throws ConfigurationException
*/
public function __construct($dsn)
public function __construct(string $dsn, $client = StatsDClient::class)
{
$this->client = new StatsDClient();
$this->client = new $client();
$this->client->configure($this->buildOptions($dsn));
$this->resetHTTPRequestSection();
}
Expand All @@ -41,7 +43,7 @@ public function __construct($dsn)
*
* @return array
*/
protected function buildOptions($dsn)
protected function buildOptions(string $dsn): array
{
$url = (array)parse_url($dsn);

Expand Down
34 changes: 34 additions & 0 deletions src/StatsD/SilentClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace HelloFresh\Stats\StatsD;

use League\StatsD\Client;
use League\StatsD\Exception\ConnectionException;

class SilentClient extends Client
{
/**
* @inheritdoc
*/
protected function send(array $data, array $tags = []) : Client
{
$tagsData = $this->serializeTags(array_replace($this->tags, $tags));

try {
$socket = $this->getSocket();
$messages = [];
$prefix = $this->namespace ? $this->namespace . '.' : '';
foreach ($data as $key => $value) {
$messages[] = $prefix . $key . ':' . $value . $tagsData;
}
$this->message = implode("\n", $messages);
@fwrite($socket, $this->message);
fflush($socket);
} catch (ConnectionException $e) {
if ($this->throwConnectionExceptions) {
throw $e;
}
}

return $this;
}
}
42 changes: 39 additions & 3 deletions tests/Client/StatsDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@

use HelloFresh\Stats;
use HelloFresh\Stats\Bucket;
use League\StatsD\Client;
use League\StatsD\Exception\ConfigurationException;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use ReflectionClass;

class ExposedClientStatsD extends StatsD
{
public function getClient()
{
return $this->client;
}
}

class StatsDTest extends TestCase
{
Expand All @@ -17,7 +29,7 @@ public function testBuildOptions()
->disableOriginalConstructor()
->getMock();

$reflection = new \ReflectionClass($statsClient);
$reflection = new ReflectionClass($statsClient);
$methodBuildOptions = $reflection->getMethod('buildOptions');
$methodBuildOptions->setAccessible(true);
$this->assertEquals(
Expand All @@ -32,6 +44,30 @@ public function testBuildOptions()
);
}

/**
* @throws ConfigurationException
*/
public function testDefaultClientInstance()
{
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
$statsD = new ExposedClientStatsD($dns);
$instantiatedClient = $statsD->getClient();

$this->assertInstanceOf(Client::class, $instantiatedClient);
}

/**
* @throws ConfigurationException
*/
public function testOptionalClientInstance()
{
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
$statsD = new ExposedClientStatsD($dns, Stats\StatsD\SilentClient::class);
$instantiatedClient = $statsD->getClient();

$this->assertInstanceOf(Stats\StatsD\SilentClient::class, $instantiatedClient);
}

public function testInstances()
{
if (!class_exists('\League\StatsD\Client')) {
Expand All @@ -50,12 +86,12 @@ public function testHTTPRequestSection()

$section = uniqid('section', true);

/** @var \PHPUnit_Framework_MockObject_MockObject|\League\StatsD\Client $statsd */
/** @var PHPUnit_Framework_MockObject_MockObject|Client $statsd */
$statsd = $this->getMockBuilder('\League\StatsD\Client')->setMethods(['gauge'])->getMock();

$statsClient = new StatsD($statsd);

$reflection = new \ReflectionClass($statsClient);
$reflection = new ReflectionClass($statsClient);
$reflectionProperty = $reflection->getProperty('httpRequestSection');
$reflectionProperty->setAccessible(true);

Expand Down
25 changes: 25 additions & 0 deletions tests/StatsD/SilentClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use HelloFresh\Stats\StatsD\SilentClient;
use PHPUnit\Framework\TestCase;

class SilentClientTest extends TestCase
{
public function testNewInstance()
{
$client = new SilentClient();
$this->assertInstanceOf(SilentClient::class, $client);
$this->assertRegExp('/^StatsD\\\Client::\[[a-zA-Z0-9]+\]$/', (string) $client);
}

public function testStaticInstance()
{
$client1 = SilentClient::instance('instance1');
$this->assertInstanceOf(SilentClient::class, $client1);
$client2 = SilentClient::instance('instance2');
$client3 = SilentClient::instance('instance1');
$this->assertEquals('StatsD\Client::[instance2]', (string) $client2);
$this->assertFalse((string) $client1 === (string) $client2);
$this->assertTrue((string) $client1 === (string) $client3);
}
}

0 comments on commit 697da42

Please sign in to comment.