Skip to content

Commit c6c672e

Browse files
authored
Use host:port for server logging and add extra fields to EventCollector 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.
1 parent 2109214 commit c6c672e

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

tests/SpecTests/RetryableReads/Prose2_RetryOnMongosTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testRetryOnDifferentMongos(): void
6464

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

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

7878
public function commandFailed(CommandFailedEvent $event): void
7979
{
80-
$this->commandFailedServers[] = $event->getServerConnectionId();
80+
$this->commandFailedServers[] = $event->getHost() . ':' . $event->getPort();
8181
}
8282
};
8383

@@ -130,21 +130,21 @@ public function testRetryOnSameMongos(): void
130130

131131
// Step 4: Enable succeeded and failed command event monitoring
132132
$subscriber = new class implements CommandSubscriber {
133-
public ?int $commandSucceededServer = null;
134-
public ?int $commandFailedServer = null;
133+
public ?string $commandSucceededServer = null;
134+
public ?string $commandFailedServer = null;
135135

136136
public function commandStarted(CommandStartedEvent $event): void
137137
{
138138
}
139139

140140
public function commandSucceeded(CommandSucceededEvent $event): void
141141
{
142-
$this->commandSucceededServer = $event->getServerConnectionId();
142+
$this->commandSucceededServer = $event->getHost() . ':' . $event->getPort();
143143
}
144144

145145
public function commandFailed(CommandFailedEvent $event): void
146146
{
147-
$this->commandFailedServer = $event->getServerConnectionId();
147+
$this->commandFailedServer = $event->getHost() . ':' . $event->getPort();
148148
}
149149
};
150150

tests/SpecTests/RetryableWrites/Prose4_RetryOnDifferentMongosTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testRetryOnDifferentMongos(): void
6767

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

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

8181
public function commandFailed(CommandFailedEvent $event): void
8282
{
83-
$this->commandFailedServers[] = $event->getServerConnectionId();
83+
$this->commandFailedServers[] = $event->getHost() . ':' . $event->getPort();
8484
}
8585
};
8686

tests/SpecTests/RetryableWrites/Prose5_RetryOnSameMongosTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ public function testRetryOnSameMongos(): void
4949

5050
// Step 4: Enable succeeded and failed command event monitoring
5151
$subscriber = new class implements CommandSubscriber {
52-
public ?int $commandSucceededServer = null;
53-
public ?int $commandFailedServer = null;
52+
public ?string $commandSucceededServer = null;
53+
public ?string $commandFailedServer = null;
5454

5555
public function commandStarted(CommandStartedEvent $event): void
5656
{
5757
}
5858

5959
public function commandSucceeded(CommandSucceededEvent $event): void
6060
{
61-
$this->commandSucceededServer = $event->getServerConnectionId();
61+
$this->commandSucceededServer = $event->getHost() . ':' . $event->getPort();
6262
}
6363

6464
public function commandFailed(CommandFailedEvent $event): void
6565
{
66-
$this->commandFailedServer = $event->getServerConnectionId();
66+
$this->commandFailedServer = $event->getHost() . ':' . $event->getPort();
6767
}
6868
};
6969

tests/UnifiedSpecTests/EventCollector.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,17 @@ private function handleCommandMonitoringEvent($event): void
119119
'name' => self::getEventName($event),
120120
'observedAt' => microtime(true),
121121
'commandName' => $event->getCommandName(),
122-
'connectionId' => $event->getServerConnectionId(),
123-
'requestId' => $event->getRequestId(),
122+
'databaseName' => $event->getDatabaseName(),
124123
'operationId' => $event->getOperationId(),
124+
'requestId' => $event->getRequestId(),
125+
'server' => $event->getHost() . ':' . $event->getPort(),
126+
'serverConnectionId' => $event->getServerConnectionId(),
127+
'serviceId' => $event->getServiceId(),
125128
];
126129

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

130-
if ($event instanceof CommandStartedEvent) {
131-
$log['databaseName'] = $event->getDatabaseName();
132-
}
133-
134133
if ($event instanceof CommandSucceededEvent) {
135134
$log['duration'] = $event->getDurationMicros();
136135
}

0 commit comments

Comments
 (0)