From 7d7edff57e0156dc4917841dfc8b9c7b3961b848 Mon Sep 17 00:00:00 2001 From: "Javi H. Gil" Date: Thu, 2 Nov 2023 15:02:41 +0100 Subject: [PATCH 1/3] Add trace to logging on cloud run --- Core/src/Report/CloudRunMetadataProvider.php | 16 +++++++++++++--- .../Unit/Report/CloudRunMetadataProviderTest.php | 10 +++++++++- Logging/src/PsrLogger.php | 4 ++++ Logging/tests/Unit/PsrLoggerBatchTest.php | 13 ++++++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Core/src/Report/CloudRunMetadataProvider.php b/Core/src/Report/CloudRunMetadataProvider.php index aa09d3d24505..5b7d771924b4 100644 --- a/Core/src/Report/CloudRunMetadataProvider.php +++ b/Core/src/Report/CloudRunMetadataProvider.php @@ -39,6 +39,11 @@ class CloudRunMetadataProvider implements MetadataProviderInterface */ private $revisionId; + /** + * @var string + */ + private $traceId; + public function __construct(array $env) { $this->serviceId = isset($env['K_SERVICE']) @@ -47,6 +52,9 @@ public function __construct(array $env) $this->revisionId = isset($env['K_REVISION']) ? $env['K_REVISION'] : 'unknown-revision'; + $this->traceId = isset($env['HTTP_X_CLOUD_TRACE_CONTEXT']) + ? substr($env['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32) + : null; $this->metadata = new Metadata(); } @@ -87,11 +95,13 @@ public function versionId() } /** - * not implemented - * @TODO + * Return the labels. We need to evaluate $_SERVER for each request. + * @return array */ public function labels() { - return []; + return !empty($this->traceId) + ? ['run.googleapis.com/trace_id' => $this->traceId ] + : []; } } diff --git a/Core/tests/Unit/Report/CloudRunMetadataProviderTest.php b/Core/tests/Unit/Report/CloudRunMetadataProviderTest.php index f23833b591e6..f355e62f39d2 100644 --- a/Core/tests/Unit/Report/CloudRunMetadataProviderTest.php +++ b/Core/tests/Unit/Report/CloudRunMetadataProviderTest.php @@ -29,10 +29,17 @@ public function testMetadataProvider() { $provider = new CloudRunMetadataProvider([ 'K_SERVICE' => 'my-service', - 'K_REVISION' => 'my-revision' + 'K_REVISION' => 'my-revision', + 'HTTP_X_CLOUD_TRACE_CONTEXT' => 'my-traceId' ]); $this->assertEquals('my-service', $provider->serviceId()); $this->assertEquals('my-revision', $provider->versionId()); + $this->assertEquals( + [ + 'run.googleapis.com/trace_id' => 'my-traceId' + ], + $provider->labels() + ); } public function testDefaults() @@ -40,5 +47,6 @@ public function testDefaults() $provider = new CloudRunMetadataProvider([]); $this->assertEquals('unknown-service', $provider->serviceId()); $this->assertEquals('unknown-revision', $provider->versionId()); + $this->assertEquals([], $provider->labels()); } } diff --git a/Logging/src/PsrLogger.php b/Logging/src/PsrLogger.php index 933e25f0fd20..ce1f67e6f496 100644 --- a/Logging/src/PsrLogger.php +++ b/Logging/src/PsrLogger.php @@ -402,6 +402,10 @@ public function log($level, $message, array $context = []) $options['trace'] = $labels['appengine.googleapis.com/trace_id']; } + if (isset($labels['run.googleapis.com/trace_id'])) { + $options['trace'] = + $labels['run.googleapis.com/trace_id']; + } } // Adding MonitoredResource $resource = $this->metadataProvider->monitoredResource(); diff --git a/Logging/tests/Unit/PsrLoggerBatchTest.php b/Logging/tests/Unit/PsrLoggerBatchTest.php index 1bab1c9c091b..2ad9a36e29c3 100644 --- a/Logging/tests/Unit/PsrLoggerBatchTest.php +++ b/Logging/tests/Unit/PsrLoggerBatchTest.php @@ -18,6 +18,7 @@ namespace Google\Cloud\Logging\Tests\Unit; use Google\Cloud\Core\Batch\BatchRunner; +use Google\Cloud\Core\Report\CloudRunMetadataProvider; use Google\Cloud\Core\Report\GAEFlexMetadataProvider; use Google\Cloud\Logging\Connection\Rest; use Google\Cloud\Logging\Entry; @@ -92,6 +93,7 @@ public function testSend( */ public function testTraceIdLabelOnGAEFlex( $traceId, + $metadataProviderClass, $labels, $expectedLabels ) { @@ -125,7 +127,7 @@ public function testTraceIdLabelOnGAEFlex( [ 'batchEnabled' => true, 'batchRunner' => $this->runner->reveal(), - 'metadataProvider' => new GAEFlexMetadataProvider($server) + 'metadataProvider' => new $metadataProviderClass($server) ] ); @@ -187,16 +189,25 @@ public function traceIdProvider() return [ [ '', + GAEFlexMetadataProvider::class, [], [], ], [ str_repeat('x', 32), + GAEFlexMetadataProvider::class, [], ['appengine.googleapis.com/trace_id' => str_repeat('x', 32)] ], [ str_repeat('x', 32), + CloudRunMetadataProvider::class, + [], + ['run.googleapis.com/trace_id' => str_repeat('x', 32)] + ], + [ + str_repeat('x', 32), + GAEFlexMetadataProvider::class, ['myKey' => 'myVal'], [ 'appengine.googleapis.com/trace_id' => str_repeat('x', 32), From ff409ce1719e449d3dca752af6b51c78160c5f9d Mon Sep 17 00:00:00 2001 From: Yash Sahu <54198301+yash30201@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:35:25 +0530 Subject: [PATCH 2/3] Update test name Update the `PsrLoggerBatchTest:: testTraceIdLabelOnGAEFlex` to `PsrLoggerBatchTest:: testTraceIdLabelOnServerlessPlatforms` as it now tests GAE as well as Cloud Run. --- Logging/tests/Unit/PsrLoggerBatchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Logging/tests/Unit/PsrLoggerBatchTest.php b/Logging/tests/Unit/PsrLoggerBatchTest.php index 2ad9a36e29c3..e1e9e03502bc 100644 --- a/Logging/tests/Unit/PsrLoggerBatchTest.php +++ b/Logging/tests/Unit/PsrLoggerBatchTest.php @@ -91,7 +91,7 @@ public function testSend( /** * @dataProvider traceIdProvider */ - public function testTraceIdLabelOnGAEFlex( + public function testTraceIdLabelOnServerlessPlatforms( $traceId, $metadataProviderClass, $labels, From 0c9a224eab4b7d510e58dc2dabebca773bc83147 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 19 Apr 2024 11:55:58 -0700 Subject: [PATCH 3/3] bump Logging core dep to the version of the next release --- Logging/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Logging/composer.json b/Logging/composer.json index dd38cea71c3f..5a0414702806 100644 --- a/Logging/composer.json +++ b/Logging/composer.json @@ -5,7 +5,7 @@ "minimum-stability": "stable", "require": { "php": "^8.0", - "google/cloud-core": "^1.52.7", + "google/cloud-core": "^1.58", "google/gax": "^1.30" }, "require-dev": {