Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 697da42

Browse files
authored
Merge pull request #21 from hellofresh/patch/PO-2958_silent_client
[PATCH/PO-2958] creates SilentClient that does not trigger errors when configured to do so
2 parents 81db372 + d53d73b commit 697da42

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

src/Client/StatsD.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ class StatsD extends AbstractClient implements Client
2626
/**
2727
* StatsD constructor.
2828
*
29-
* @param string $dsn statsd connection dsn
29+
* @param string $dsn statsd connection dsn
30+
* @param Client $client statsd client
31+
*
3032
* @throws ConfigurationException
3133
*/
32-
public function __construct($dsn)
34+
public function __construct(string $dsn, $client = StatsDClient::class)
3335
{
34-
$this->client = new StatsDClient();
36+
$this->client = new $client();
3537
$this->client->configure($this->buildOptions($dsn));
3638
$this->resetHTTPRequestSection();
3739
}
@@ -41,7 +43,7 @@ public function __construct($dsn)
4143
*
4244
* @return array
4345
*/
44-
protected function buildOptions($dsn)
46+
protected function buildOptions(string $dsn): array
4547
{
4648
$url = (array)parse_url($dsn);
4749

src/StatsD/SilentClient.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace HelloFresh\Stats\StatsD;
3+
4+
use League\StatsD\Client;
5+
use League\StatsD\Exception\ConnectionException;
6+
7+
class SilentClient extends Client
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
protected function send(array $data, array $tags = []) : Client
13+
{
14+
$tagsData = $this->serializeTags(array_replace($this->tags, $tags));
15+
16+
try {
17+
$socket = $this->getSocket();
18+
$messages = [];
19+
$prefix = $this->namespace ? $this->namespace . '.' : '';
20+
foreach ($data as $key => $value) {
21+
$messages[] = $prefix . $key . ':' . $value . $tagsData;
22+
}
23+
$this->message = implode("\n", $messages);
24+
@fwrite($socket, $this->message);
25+
fflush($socket);
26+
} catch (ConnectionException $e) {
27+
if ($this->throwConnectionExceptions) {
28+
throw $e;
29+
}
30+
}
31+
32+
return $this;
33+
}
34+
}

tests/Client/StatsDTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@
33

44
use HelloFresh\Stats;
55
use HelloFresh\Stats\Bucket;
6+
use League\StatsD\Client;
7+
use League\StatsD\Exception\ConfigurationException;
68
use PHPUnit\Framework\TestCase;
9+
use PHPUnit_Framework_MockObject_MockObject;
10+
use ReflectionClass;
11+
12+
class ExposedClientStatsD extends StatsD
13+
{
14+
public function getClient()
15+
{
16+
return $this->client;
17+
}
18+
}
719

820
class StatsDTest extends TestCase
921
{
@@ -17,7 +29,7 @@ public function testBuildOptions()
1729
->disableOriginalConstructor()
1830
->getMock();
1931

20-
$reflection = new \ReflectionClass($statsClient);
32+
$reflection = new ReflectionClass($statsClient);
2133
$methodBuildOptions = $reflection->getMethod('buildOptions');
2234
$methodBuildOptions->setAccessible(true);
2335
$this->assertEquals(
@@ -32,6 +44,30 @@ public function testBuildOptions()
3244
);
3345
}
3446

47+
/**
48+
* @throws ConfigurationException
49+
*/
50+
public function testDefaultClientInstance()
51+
{
52+
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
53+
$statsD = new ExposedClientStatsD($dns);
54+
$instantiatedClient = $statsD->getClient();
55+
56+
$this->assertInstanceOf(Client::class, $instantiatedClient);
57+
}
58+
59+
/**
60+
* @throws ConfigurationException
61+
*/
62+
public function testOptionalClientInstance()
63+
{
64+
$dns = 'statsd://stats.local:1234/prefix.ns?timeout=2.5&error=0';
65+
$statsD = new ExposedClientStatsD($dns, Stats\StatsD\SilentClient::class);
66+
$instantiatedClient = $statsD->getClient();
67+
68+
$this->assertInstanceOf(Stats\StatsD\SilentClient::class, $instantiatedClient);
69+
}
70+
3571
public function testInstances()
3672
{
3773
if (!class_exists('\League\StatsD\Client')) {
@@ -50,12 +86,12 @@ public function testHTTPRequestSection()
5086

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

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

5692
$statsClient = new StatsD($statsd);
5793

58-
$reflection = new \ReflectionClass($statsClient);
94+
$reflection = new ReflectionClass($statsClient);
5995
$reflectionProperty = $reflection->getProperty('httpRequestSection');
6096
$reflectionProperty->setAccessible(true);
6197

tests/StatsD/SilentClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use HelloFresh\Stats\StatsD\SilentClient;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class SilentClientTest extends TestCase
7+
{
8+
public function testNewInstance()
9+
{
10+
$client = new SilentClient();
11+
$this->assertInstanceOf(SilentClient::class, $client);
12+
$this->assertRegExp('/^StatsD\\\Client::\[[a-zA-Z0-9]+\]$/', (string) $client);
13+
}
14+
15+
public function testStaticInstance()
16+
{
17+
$client1 = SilentClient::instance('instance1');
18+
$this->assertInstanceOf(SilentClient::class, $client1);
19+
$client2 = SilentClient::instance('instance2');
20+
$client3 = SilentClient::instance('instance1');
21+
$this->assertEquals('StatsD\Client::[instance2]', (string) $client2);
22+
$this->assertFalse((string) $client1 === (string) $client2);
23+
$this->assertTrue((string) $client1 === (string) $client3);
24+
}
25+
}

0 commit comments

Comments
 (0)