Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional disabling of connection exceptions #16

Merged
merged 3 commits into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 . '.' : '';
Expand Down
32 changes: 30 additions & 2 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ConnectionTest extends TestCase
{

/**
* Non-integer ports are not acceptable
* @expectedException League\StatsD\Exception\ConnectionException
Expand All @@ -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);
Expand Down