Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logging): add trace to logging on cloud run #6764

Merged
merged 8 commits into from
Apr 19, 2024
16 changes: 13 additions & 3 deletions Core/src/Report/CloudRunMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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();
}

Expand Down Expand Up @@ -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 ]
: [];
}
}
10 changes: 9 additions & 1 deletion Core/tests/Unit/Report/CloudRunMetadataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ 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()
{
$provider = new CloudRunMetadataProvider([]);
$this->assertEquals('unknown-service', $provider->serviceId());
$this->assertEquals('unknown-revision', $provider->versionId());
$this->assertEquals([], $provider->labels());
}
}
2 changes: 1 addition & 1 deletion Logging/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.0",
"google/cloud-core": "^1.52.7",
"google/cloud-core": "^1.58",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump the google/cloud-core dependency to what will be the next release, as the tests depend on the new metadata provider class.

"google/gax": "^1.30"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions Logging/src/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ public function log($level, Stringable|string $message, array $context = []): vo
$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();
Expand Down
15 changes: 13 additions & 2 deletions Logging/tests/Unit/PsrLoggerBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,8 +91,9 @@ public function testSend(
/**
* @dataProvider traceIdProvider
*/
public function testTraceIdLabelOnGAEFlex(
public function testTraceIdLabelOnServerlessPlatforms(
$traceId,
$metadataProviderClass,
$labels,
$expectedLabels
) {
Expand Down Expand Up @@ -125,7 +127,7 @@ public function testTraceIdLabelOnGAEFlex(
[
'batchEnabled' => true,
'batchRunner' => $this->runner->reveal(),
'metadataProvider' => new GAEFlexMetadataProvider($server)
'metadataProvider' => new $metadataProviderClass($server)
]
);

Expand Down Expand Up @@ -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),
Expand Down
Loading