Skip to content

Commit

Permalink
Merge pull request #287 from scoutapp/fix-mongo-db-ignoring-monitorin…
Browse files Browse the repository at this point in the history
…g-disabled-config

Only enable MongoDB collector if monitoring is enabled
  • Loading branch information
asgrim authored Sep 26, 2022
2 parents 0bdd3ab + e147cbb commit b6c96e1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 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
6 changes: 3 additions & 3 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ private function __construct(
if ($this->config->get(ConfigKey::MONITORING_ENABLED)) {
$this->warnIfConfigValueIsNotSet(ConfigKey::APPLICATION_NAME);
$this->warnIfConfigValueIsNotSet(ConfigKey::APPLICATION_KEY);
}

if (extension_loaded('mongodb')) {
QueryTimeCollector::register($this);
if (extension_loaded('mongodb')) {
QueryTimeCollector::register($this);
}
}

$this->startNewRequest();
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 b6c96e1

Please sign in to comment.