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

Catch exceptions raised whilst sending metadata #134

Merged
merged 2 commits into from
Dec 9, 2019
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
20 changes: 12 additions & 8 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Scoutapm\Extension\ExtentionCapabilities;
use Scoutapm\Extension\PotentiallyAvailableExtensionCapabilities;
use Scoutapm\Logger\FilteredLogLevelDecorator;
use Throwable;
use function is_string;
use function json_encode;
use function sprintf;
Expand Down Expand Up @@ -353,16 +354,19 @@ private function sendMetadataIfRequired() : void
return;
}

if (! $this->connector->sendCommand(new Metadata(
new DateTimeImmutable('now', new DateTimeZone('UTC')),
$this->config
))) {
$this->logger->debug('Send command returned false for Metadata');
try {
$this->connector->sendCommand(new Metadata(
new DateTimeImmutable('now', new DateTimeZone('UTC')),
$this->config
));

return;
$this->markMetadataSent();
} catch (Throwable $exception) {
$this->logger->notice(
sprintf('Sending metadata raised an exception: %s', $exception->getMessage()),
['exception' => $exception]
);
}

$this->markMetadataSent();
}

private function metadataWasSent() : bool
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@

namespace Scoutapm\UnitTests;

use OutOfBoundsException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Psr\Log\Test\TestLogger;
use Scoutapm\Agent;
use Scoutapm\Cache\DevNullCache;
use Scoutapm\Config;
use Scoutapm\Config\ConfigKey;
use Scoutapm\Connector\Connector;
use Scoutapm\Events\Metadata;
use Scoutapm\Events\RegisterMessage;
use Scoutapm\Events\Request\Request;
use Scoutapm\Events\Span\Span;
use Scoutapm\Events\Tag\TagRequest;
use function array_map;
use function count;
use function end;
use function sprintf;
use function uniqid;

/** @covers \Scoutapm\Agent */
final class AgentTest extends TestCase
Expand Down Expand Up @@ -271,6 +279,43 @@ public function testIgnoredEndpoints() : void
self::assertFalse($agent->ignored('/bar'));
}

public function testMetadataExceptionsAreLogged() : void
{
$logger = new TestLogger();
$connector = $this->createMock(Connector::class);

$agent = Agent::fromConfig(
Config::fromArray([
ConfigKey::APPLICATION_NAME => 'My test app',
ConfigKey::APPLICATION_KEY => uniqid('applicationKey', true),
ConfigKey::MONITORING_ENABLED => true,
ConfigKey::LOG_LEVEL => LogLevel::NOTICE,
]),
$logger,
new DevNullCache(),
$connector
);

$connector->method('connected')->willReturn(true);

$connector->expects(self::at(1))
->method('sendCommand')
->with(self::isInstanceOf(RegisterMessage::class))
->willReturn('{"Register":"success"}');
$connector->expects(self::at(2))
->method('sendCommand')
->with(self::isInstanceOf(Metadata::class))
->willThrowException(new OutOfBoundsException('Some obscure exception happened'));
$connector->expects(self::at(3))
->method('sendCommand')
->with(self::isInstanceOf(Request::class))
->willReturn('{"Request":"success"}');

$agent->send();

self::assertTrue($logger->hasNoticeThatContains('Sending metadata raised an exception: Some obscure exception happened'));
}

/**
* Many instrumentation calls are NOOPs when ignore is called. Make sure the sequence works as expected
*/
Expand Down