From 0c122ac639d67cc00869f72c872556b56752b6aa Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Mon, 26 Sep 2022 09:41:37 +0100 Subject: [PATCH] Failing test to show MongoDB is instrumenting even when monitoring is disabled --- known-issues.xml | 6 +++ src/MongoDB/QueryTimeCollector.php | 11 ++--- tests/Integration/AgentTest.php | 65 ++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/known-issues.xml b/known-issues.xml index 3772eb57..326e11e1 100644 --- a/known-issues.xml +++ b/known-issues.xml @@ -150,6 +150,12 @@ + + + getEvents + getRequest + + CheckScoutApmKeyListener diff --git a/src/MongoDB/QueryTimeCollector.php b/src/MongoDB/QueryTimeCollector.php index 768bd4ea..dfd06111 100644 --- a/src/MongoDB/QueryTimeCollector.php +++ b/src/MongoDB/QueryTimeCollector.php @@ -47,12 +47,13 @@ public function commandStarted(CommandStartedEvent $event): void { $activeSpan = $this->agent->startSpan('Mongo/Query/' . $event->getCommandName()); - if ($activeSpan !== null) { - $activeSpan->tag('db', $event->getDatabaseName()); - $activeSpan->tag('operationId', $event->getOperationId()); - $activeSpan->tag('requestId', $event->getRequestId()); + if ($activeSpan === null) { + return; } -// echo serialize($event); + + $activeSpan->tag('db', $event->getDatabaseName()); + $activeSpan->tag('operationId', $event->getOperationId()); + $activeSpan->tag('requestId', $event->getRequestId()); } public function commandSucceeded(CommandSucceededEvent $event): void diff --git a/tests/Integration/AgentTest.php b/tests/Integration/AgentTest.php index 81a70b2a..801abd60 100644 --- a/tests/Integration/AgentTest.php +++ b/tests/Integration/AgentTest.php @@ -122,15 +122,17 @@ private function setUpWithConfiguration(Config $config): void $this->agent = Agent::fromConfig($config, $this->logger, null, $this->connector); - $retryCount = 0; - while ($retryCount < 5 && ! $this->connector->connected()) { - $this->agent->connect(); - sleep(1); - $retryCount++; - } + if ($config->get(ConfigKey::MONITORING_ENABLED)) { + $retryCount = 0; + while ($retryCount < 5 && ! $this->connector->connected()) { + $this->agent->connect(); + sleep(1); + $retryCount++; + } - if (! $this->connector->connected()) { - self::fail('Could not connect to core agent in test harness. ' . $this->formatCapturedLogMessages()); + if (! $this->connector->connected()) { + self::fail('Could not connect to core agent in test harness. ' . $this->formatCapturedLogMessages()); + } } (new PotentiallyAvailableExtensionCapabilities())->clearRecordedCalls(); @@ -426,6 +428,53 @@ static function (array $commands): bool { ); } + /** + * Run Mongo with: + * + * ``` + * docker run --rm --name some-mongo -p 27017:27017 -d mongo:latest + * ``` + * + * @group mongo + */ + public function testMongoDbDoesNotStartSpansWhenMonitoringIsDisabled(): void + { + if (! extension_loaded('mongodb')) { + self::markTestSkipped('MongoDB extension required for this test - mongodb is not loaded'); + } + + $this->setUpWithConfiguration(Config::fromArray([ + ConfigKey::APPLICATION_NAME => self::APPLICATION_NAME, + ConfigKey::MONITORING_ENABLED => false, + ])); + + $mongo = new Manager('mongodb://localhost:27017'); + + try { + $mongo->startSession(); + } catch (ConnectionTimeoutException $timeoutException) { + self::markTestSkipped('Could not connect to mongodb server, is it running?'); + } + + $db = 'scout-apm-test-db'; + $collection = uniqid('scout-apm-test-', true); + + $mongo->executeCommand($db, new Command(['create' => $collection])); + + $request = $this->agent->getRequest(); + self::assertNotNull($request); + self::assertEmpty($request->getEvents()); + } + + /** + * Run Mongo with: + * + * ``` + * docker run --rm --name some-mongo -p 27017:27017 -d mongo:latest + * ``` + * + * @group mongo + */ public function testMongoDbInstrumentation(): void { if (! extension_loaded('mongodb')) {