Skip to content

Commit

Permalink
Failing test to show MongoDB is instrumenting even when monitoring is…
Browse files Browse the repository at this point in the history
… disabled
  • Loading branch information
asgrim committed Sep 26, 2022
1 parent 0bdd3ab commit 0c122ac
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
6 changes: 6 additions & 0 deletions known-issues.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1"/>
</file>
<file src="tests/Integration/AgentTest.php">
<DeprecatedMethod occurrences="2">
<code>getEvents</code>
<code>getRequest</code>
</DeprecatedMethod>
</file>
<file src="tests/Integration/CheckScoutApmKeyListener.php">
<DeprecatedInterface occurrences="1">
<code>CheckScoutApmKeyListener</code>
Expand Down
11 changes: 6 additions & 5 deletions src/MongoDB/QueryTimeCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 57 additions & 8 deletions tests/Integration/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit 0c122ac

Please sign in to comment.