-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |