Skip to content

Commit

Permalink
Use host:port for server logging and add extra fields to EventCollect…
Browse files Browse the repository at this point in the history
…or logs (#1428)

* Track servers by host:port instead of serverConnectionId

The server's connection ID is unique within the context of a single server and is only reported by MongoDB 4.2+. It should not be used to uniquely identify a server.

This reverts recent changes from 50d1db7.

* Rename connectionId and add extra fields to EventCollector logs

This renames connectionId to server, to avoid ambiguity with serverConnectionId.

Additional fields have also been added to the output. Since PHPC 1.19, database name is available on all APM events.
  • Loading branch information
jmikola authored Sep 23, 2024
1 parent 2109214 commit c6c672e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
12 changes: 6 additions & 6 deletions tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testRetryOnDifferentMongos(): void

// Step 4: Enable failed command event monitoring for client
$subscriber = new class implements CommandSubscriber {
/** @var int[] */
/** @var string[] */
public array $commandFailedServers = [];

public function commandStarted(CommandStartedEvent $event): void
Expand All @@ -77,7 +77,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServers[] = $event->getServerConnectionId();
$this->commandFailedServers[] = $event->getHost() . ':' . $event->getPort();
}
};

Expand Down Expand Up @@ -130,21 +130,21 @@ public function testRetryOnSameMongos(): void

// Step 4: Enable succeeded and failed command event monitoring
$subscriber = new class implements CommandSubscriber {
public ?int $commandSucceededServer = null;
public ?int $commandFailedServer = null;
public ?string $commandSucceededServer = null;
public ?string $commandFailedServer = null;

public function commandStarted(CommandStartedEvent $event): void
{
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
$this->commandSucceededServer = $event->getServerConnectionId();
$this->commandSucceededServer = $event->getHost() . ':' . $event->getPort();
}

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServer = $event->getServerConnectionId();
$this->commandFailedServer = $event->getHost() . ':' . $event->getPort();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testRetryOnDifferentMongos(): void

// Step 4: Enable failed command event monitoring for client
$subscriber = new class implements CommandSubscriber {
/** @var int[] */
/** @var string[] */
public array $commandFailedServers = [];

public function commandStarted(CommandStartedEvent $event): void
Expand All @@ -80,7 +80,7 @@ public function commandSucceeded(CommandSucceededEvent $event): void

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServers[] = $event->getServerConnectionId();
$this->commandFailedServers[] = $event->getHost() . ':' . $event->getPort();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ public function testRetryOnSameMongos(): void

// Step 4: Enable succeeded and failed command event monitoring
$subscriber = new class implements CommandSubscriber {
public ?int $commandSucceededServer = null;
public ?int $commandFailedServer = null;
public ?string $commandSucceededServer = null;
public ?string $commandFailedServer = null;

public function commandStarted(CommandStartedEvent $event): void
{
}

public function commandSucceeded(CommandSucceededEvent $event): void
{
$this->commandSucceededServer = $event->getServerConnectionId();
$this->commandSucceededServer = $event->getHost() . ':' . $event->getPort();
}

public function commandFailed(CommandFailedEvent $event): void
{
$this->commandFailedServer = $event->getServerConnectionId();
$this->commandFailedServer = $event->getHost() . ':' . $event->getPort();
}
};

Expand Down
11 changes: 5 additions & 6 deletions tests/UnifiedSpecTests/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,17 @@ private function handleCommandMonitoringEvent($event): void
'name' => self::getEventName($event),
'observedAt' => microtime(true),
'commandName' => $event->getCommandName(),
'connectionId' => $event->getServerConnectionId(),
'requestId' => $event->getRequestId(),
'databaseName' => $event->getDatabaseName(),
'operationId' => $event->getOperationId(),
'requestId' => $event->getRequestId(),
'server' => $event->getHost() . ':' . $event->getPort(),
'serverConnectionId' => $event->getServerConnectionId(),
'serviceId' => $event->getServiceId(),
];

/* Note: CommandStartedEvent.command and CommandSucceededEvent.reply can
* be omitted from logged events. */

if ($event instanceof CommandStartedEvent) {
$log['databaseName'] = $event->getDatabaseName();
}

if ($event instanceof CommandSucceededEvent) {
$log['duration'] = $event->getDurationMicros();
}
Expand Down

0 comments on commit c6c672e

Please sign in to comment.