From 180cb2565f14acb081c0f021d698e998143dc16f Mon Sep 17 00:00:00 2001 From: Vladimir Garvardt Date: Wed, 14 Feb 2018 16:29:20 +0100 Subject: [PATCH 1/2] Added opened socket reuse for statsd --- src/Client/StatsD.php | 3 +- src/StatsD/CachingClient.php | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/StatsD/CachingClient.php diff --git a/src/Client/StatsD.php b/src/Client/StatsD.php index 8bde15a..b861c52 100644 --- a/src/Client/StatsD.php +++ b/src/Client/StatsD.php @@ -6,7 +6,7 @@ use HelloFresh\Stats\Incrementer; use HelloFresh\Stats\State; use HelloFresh\Stats\Timer; -use League\StatsD\Client as StatsDClient; +use HelloFresh\Stats\StatsD\CachingClient as StatsDClient; class StatsD extends AbstractClient implements Client { @@ -26,6 +26,7 @@ class StatsD extends AbstractClient implements Client * StatsD constructor. * * @param string $dsn statsd connection dsn + * @throws \League\StatsD\Exception\ConfigurationException */ public function __construct($dsn) { diff --git a/src/StatsD/CachingClient.php b/src/StatsD/CachingClient.php new file mode 100644 index 0000000..bf03906 --- /dev/null +++ b/src/StatsD/CachingClient.php @@ -0,0 +1,68 @@ +socket) { + $this->socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout); + if (!$this->socket) { + throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); + } + } + + return $this->socket; + } + + /** + * Send Data to StatsD Server + * @param array $data A list of messages to send to the server + * @return $this + * @throws ConnectionException If there is a connection problem with the host + */ + protected function send(array $data) + { + try { + $socket = $this->getSocket(); + $messages = []; + $prefix = $this->namespace ? $this->namespace . '.' : ''; + foreach ($data as $key => $value) { + $messages[] = $prefix . $key . ':' . $value; + } + $this->message = implode("\n", $messages); + @fwrite($socket, $this->message); + fflush($socket); + } catch (ConnectionException $e) { + if ($this->throwConnectionExceptions) { + throw $e; + } else { + trigger_error( + sprintf('StatsD server connection failed (udp://%s:%d)', $this->host, $this->port), + E_USER_WARNING + ); + } + } + + return $this; + } + + public function __destruct() + { + if ($this->socket) { + fclose($this->socket); + } + } +} From 0988b54023b2814df1b3aba8a760b06434fd216b Mon Sep 17 00:00:00 2001 From: Vladimir Garvardt Date: Wed, 14 Feb 2018 16:38:59 +0100 Subject: [PATCH 2/2] Fixed code style --- src/Client/StatsD.php | 7 ++++--- src/StatsD/CachingClient.php | 8 +++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Client/StatsD.php b/src/Client/StatsD.php index b861c52..05c9f66 100644 --- a/src/Client/StatsD.php +++ b/src/Client/StatsD.php @@ -5,8 +5,9 @@ use HelloFresh\Stats\HTTPMetricAlterCallback; use HelloFresh\Stats\Incrementer; use HelloFresh\Stats\State; -use HelloFresh\Stats\Timer; use HelloFresh\Stats\StatsD\CachingClient as StatsDClient; +use HelloFresh\Stats\Timer; +use League\StatsD\Exception\ConfigurationException; class StatsD extends AbstractClient implements Client { @@ -25,8 +26,8 @@ class StatsD extends AbstractClient implements Client /** * StatsD constructor. * - * @param string $dsn statsd connection dsn - * @throws \League\StatsD\Exception\ConfigurationException + * @param string $dsn statsd connection dsn + * @throws ConfigurationException */ public function __construct($dsn) { diff --git a/src/StatsD/CachingClient.php b/src/StatsD/CachingClient.php index bf03906..56ba85e 100644 --- a/src/StatsD/CachingClient.php +++ b/src/StatsD/CachingClient.php @@ -1,8 +1,6 @@