Skip to content

Commit d5c1330

Browse files
fix: Add the rpc uri to the logs (#9307)
Add the rpc uri to the logs
1 parent b7deb00 commit d5c1330

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Gax/src/Transport/GrpcTransport.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function startBidiStreamingCall(Call $call, array $options)
198198
$requestEvent->rpcName = $call->getMethod();
199199
$requestEvent->processId = (int) getmypid();
200200
$requestEvent->requestId = crc32((string) spl_object_id($bidiStream) . getmypid());
201+
$requestEvent->url = $this->getGrpcUrl();
201202

202203
$this->logRequest($requestEvent);
203204
}
@@ -263,6 +264,7 @@ public function startServerStreamingCall(Call $call, array $options)
263264
$requestEvent->rpcName = $call->getMethod();
264265
$requestEvent->processId = (int) getmypid();
265266
$requestEvent->requestId = crc32((string) spl_object_id($serverStream) . getmypid());
267+
$requestEvent->url = $this->getGrpcUrl();
266268

267269
$this->logRequest($requestEvent);
268270
}
@@ -297,6 +299,7 @@ public function startUnaryCall(Call $call, array $options)
297299
$requestEvent->rpcName = $call->getMethod();
298300
$requestEvent->processId = (int) getmypid();
299301
$requestEvent->requestId = crc32((string) spl_object_id($call) . getmypid());
302+
$requestEvent->url = $this->getGrpcUrl();
300303

301304
$this->logRequest($requestEvent);
302305
}
@@ -359,6 +362,11 @@ private function getCallOptions(array $options)
359362
return $callOptions;
360363
}
361364

365+
private function getGrpcUrl(): string
366+
{
367+
return 'grpc://' . str_replace('dns:///', '', $this->getTarget());
368+
}
369+
362370
private static function loadClientCertSource(callable $clientCertSource)
363371
{
364372
return call_user_func($clientCertSource);

Gax/tests/Unit/Transport/GrpcTransportTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,56 @@ public function testBidiOpeningRequestLogsRpcName()
303303
$this->assertNotEmpty($unserializedBuffer);
304304
$this->assertNotEmpty($unserializedBuffer['rpcName']);
305305
$this->assertEquals($rpcName, $unserializedBuffer['rpcName']);
306+
$this->assertNotEmpty($unserializedBuffer['jsonPayload']);
307+
$this->assertEquals('grpc://', $unserializedBuffer['jsonPayload']['request.url']);
308+
}
309+
310+
public function testServerStreamingRequestLogsUrl()
311+
{
312+
$rpcName = 'takeAction';
313+
$serverStreamingCall = $this->prophesize(\Grpc\ServerStreamingCall::class);
314+
$message = $this->createMockRequest();
315+
316+
$transport = new MockGrpcTransport(
317+
$serverStreamingCall->reveal(),
318+
logger: new StdOutLogger()
319+
);
320+
321+
$stream = $transport->startServerStreamingCall(
322+
new Call($rpcName, null, $message),
323+
['headers' => []]
324+
);
325+
326+
$buffer = $this->getActualOutput();
327+
$unserializedBuffer = json_decode($buffer, true);
328+
329+
$this->assertNotEmpty($unserializedBuffer);
330+
$this->assertNotEmpty($unserializedBuffer['jsonPayload']);
331+
$this->assertEquals('grpc://', $unserializedBuffer['jsonPayload']['request.url']);
332+
}
333+
334+
public function testUnaryRequestLogsUrl()
335+
{
336+
$rpcName = 'takeAction';
337+
$unaryCall = $this->prophesize(\Grpc\UnaryCall::class);
338+
$message = $this->createMockRequest();
339+
340+
$transport = new MockGrpcTransport(
341+
$unaryCall->reveal(),
342+
logger: new StdOutLogger()
343+
);
344+
345+
$transport->startUnaryCall(
346+
new Call($rpcName, null, $message),
347+
['headers' => []]
348+
);
349+
350+
$buffer = $this->getActualOutput();
351+
$unserializedBuffer = json_decode($buffer, true);
352+
353+
$this->assertNotEmpty($unserializedBuffer);
354+
$this->assertNotEmpty($unserializedBuffer['jsonPayload']);
355+
$this->assertEquals('grpc://', $unserializedBuffer['jsonPayload']['request.url']);
306356
}
307357

308358
public function testBidiStreamingSuccessObject()

0 commit comments

Comments
 (0)