Permalink
Browse files

Merge pull request #16 from michaelmoussa/disable-exceptions

Allow optional disabling of connection exceptions
  • Loading branch information...
2 parents dd55c72 + 22a2605 commit ee5537838ea69cfd42c6d123fffddec15bf2bba5 @marcqualie marcqualie committed Jun 11, 2015
Showing with 60 additions and 3 deletions.
  1. +9 −0 README.md
  2. +21 −1 src/Client.php
  3. +30 −2 tests/ConnectionTest.php
View
@@ -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' => <float>` 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
View
@@ -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 . '.' : '';
@@ -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);

0 comments on commit ee55378

Please sign in to comment.