diff --git a/README.md b/README.md index d190717..f4e353c 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,15 @@ $statsd2 = StatsD\Client::instance('server2')->configure(array(...)); The StatsD client wait for `ini_get('default_socket_timeout')` seconds when opening the socket by default. To reduce this timeout, add `'timeout' => ` to your config. +The StatsD client will throw a `ConnectionException` if it is unable to send data to the StatsD server. You may choose +to disable these exceptions and log a PHP warning instead if you wish. To do so, include the following in your config: + +``` + 'throwConnectionExceptions' => false +``` + +If omitted, this option defaults to `true`. + ### Counters ```php diff --git a/src/Client.php b/src/Client.php index 2b2c5ba..197b806 100644 --- a/src/Client.php +++ b/src/Client.php @@ -60,6 +60,11 @@ class Client */ protected $timeout; + /** + * Whether or not an exception should be thrown on failed connections + * @var bool + */ + protected $throwConnectionExceptions = true; /** * Singleton Reference @@ -118,12 +123,19 @@ public function configure(array $options = array()) } $this->port = $port; } + if (isset($options['namespace'])) { $this->namespace = $options['namespace']; } + if (isset($options['timeout'])) { $this->timeout = $options['timeout']; } + + if (isset($options['throwConnectionExceptions'])) { + $this->throwConnectionExceptions = $options['throwConnectionExceptions']; + } + return $this; } @@ -281,7 +293,15 @@ protected function send(array $data) $socket = @fsockopen('udp://' . $this->host, $this->port, $errno, $errstr, $this->timeout); if (! $socket) { - throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); + if ($this->throwConnectionExceptions) { + throw new ConnectionException($this, '(' . $errno . ') ' . $errstr); + } else { + trigger_error( + sprintf('StatsD server connection failed (udp://%s:%d)', $this->host, $this->port), + E_USER_WARNING + ); + return; + } } $this->messages = array(); $prefix = $this->namespace ? $this->namespace . '.' : ''; diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index adf9c74..a8d087d 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -4,7 +4,6 @@ class ConnectionTest extends TestCase { - /** * Non-integer ports are not acceptable * @expectedException League\StatsD\Exception\ConnectionException @@ -23,10 +22,39 @@ public function testTimeoutSettingIsUsedWhenCreatingSocketIfProvided() 'host' => 'localhost', 'timeout' => 123 )); - $this->assertAttributeSame(123, 'timeout', $this->client); } + public function testCanBeConfiguredNotToThrowConnectionExceptions() + { + $this->client->configure(array( + 'host' => 'hostdoesnotexiststalleverlol.stupidtld', + 'throwConnectionExceptions' => false + )); + $handlerInvoked = false; + + $testCase = $this; + + set_error_handler( + function ($errno, $errstr, $errfile, $errline, $errcontext) use ($testCase, &$handlerInvoked) { + $handlerInvoked = true; + + $testCase->assertSame(E_USER_WARNING, $errno); + $testCase->assertSame( + 'StatsD server connection failed (udp://hostdoesnotexiststalleverlol.stupidtld:8125)', + $errstr + ); + $testCase->assertSame(realpath(__DIR__ . '/../src/Client.php'), $errfile); + }, + E_USER_WARNING + ); + + $this->client->increment('test'); + restore_error_handler(); + + $this->assertTrue($handlerInvoked); + } + public function testTimeoutDefaultsToPhpIniDefaultSocketTimeout() { $this->assertAttributeSame(ini_get('default_socket_timeout'), 'timeout', $this->client);