-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support log correlation on App Engine standard 2nd gen (#1407)
* Support log correlation on App Engine standard 2nd gen fixes #1277 * Test refactoring * Removed unnecessary else clause * Code style * Code style fix
- Loading branch information
Showing
6 changed files
with
285 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Core\Report; | ||
|
||
/** | ||
* MetadataProvider for GAE. | ||
*/ | ||
abstract class GAEMetadataProvider implements MetadataProviderInterface | ||
{ | ||
abstract protected function getTraceValue($server); | ||
|
||
/** @var array */ | ||
private $data; | ||
|
||
/** | ||
* @param array $server An array for holding the values. Normally just use | ||
* $_SERVER. | ||
*/ | ||
public function __construct(array $server) | ||
{ | ||
$projectId = isset($server['GOOGLE_CLOUD_PROJECT']) | ||
? $server['GOOGLE_CLOUD_PROJECT'] | ||
: 'unknown-projectid'; | ||
$serviceId = isset($server['GAE_SERVICE']) | ||
? $server['GAE_SERVICE'] | ||
: 'unknown-service'; | ||
$versionId = isset($server['GAE_VERSION']) | ||
? $server['GAE_VERSION'] | ||
: 'unknown-version'; | ||
$labels = isset($server['HTTP_X_CLOUD_TRACE_CONTEXT']) | ||
? ['appengine.googleapis.com/trace_id' => | ||
$this->getTraceValue($server)] | ||
: []; | ||
$this->data = | ||
[ | ||
'resource' => [ | ||
'type' => 'gae_app', | ||
'labels' => [ | ||
'project_id' => $projectId, | ||
'version_id' => $versionId, | ||
'module_id' => $serviceId | ||
] | ||
], | ||
'projectId' => $projectId, | ||
'serviceId' => $serviceId, | ||
'versionId' => $versionId, | ||
'labels' => $labels | ||
]; | ||
} | ||
|
||
/** | ||
* Return an array representing MonitoredResource. | ||
* {@see https://cloud.google.com/logging/docs/reference/v2/rest/v2/MonitoredResource} | ||
* | ||
* @return array | ||
*/ | ||
public function monitoredResource() | ||
{ | ||
return $this->data['resource']; | ||
} | ||
|
||
/** | ||
* Return the project id. | ||
* @return string | ||
*/ | ||
public function projectId() | ||
{ | ||
return $this->data['projectId']; | ||
} | ||
|
||
/** | ||
* Return the service id. | ||
* @return string | ||
*/ | ||
public function serviceId() | ||
{ | ||
return $this->data['serviceId']; | ||
} | ||
|
||
/** | ||
* Return the version id. | ||
* @return string | ||
*/ | ||
public function versionId() | ||
{ | ||
return $this->data['versionId']; | ||
} | ||
|
||
/** | ||
* Return the labels. We need to evaluate $_SERVER for each request. | ||
* @return array | ||
*/ | ||
public function labels() | ||
{ | ||
return $this->data['labels']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Core\Report; | ||
|
||
/** | ||
* An MetadataProvider for GAE Standard. | ||
*/ | ||
class GAEStandardMetadataProvider extends GAEMetadataProvider | ||
{ | ||
protected function getTraceValue($server) | ||
{ | ||
$traceId = substr($server['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32); | ||
if (isset($server['GOOGLE_CLOUD_PROJECT'])) { | ||
return sprintf( | ||
'projects/%s/traces/%s', | ||
$server['GOOGLE_CLOUD_PROJECT'], | ||
$traceId | ||
); | ||
} | ||
return $traceId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
Core/tests/Unit/Report/GAEStandardMetadataProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Core\Tests\Unit\Report; | ||
|
||
use Google\Cloud\Core\Report\GAEStandardMetadataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group core | ||
*/ | ||
class GAEStandardMetadataProviderTest extends TestCase | ||
{ | ||
private $envs = [ | ||
'GAE_SERVICE' => 'my-service', | ||
'GAE_VERSION' => 'my-version', | ||
'GOOGLE_CLOUD_PROJECT' => 'my-project', | ||
'HTTP_X_CLOUD_TRACE_CONTEXT' => 'my-traceId' | ||
]; | ||
|
||
/** | ||
* @dataProvider testData | ||
*/ | ||
public function testGAEStandardMetadataProvider( | ||
$envs, | ||
$monitoredResource, | ||
$projectId, | ||
$serviceId, | ||
$versionId, | ||
$labels | ||
) { | ||
$metadataProvider = new GAEStandardMetadataProvider($envs); | ||
$this->assertEquals($monitoredResource, $metadataProvider->monitoredResource()); | ||
$this->assertEquals($projectId, $metadataProvider->projectId()); | ||
$this->assertEquals($serviceId, $metadataProvider->serviceId()); | ||
$this->assertEquals($versionId, $metadataProvider->versionId()); | ||
$this->assertEquals($labels, $metadataProvider->labels()); | ||
} | ||
|
||
public function testData() | ||
{ | ||
return [ | ||
[ | ||
[ | ||
'GAE_SERVICE' => 'my-service', | ||
'GAE_VERSION' => 'my-version', | ||
'GOOGLE_CLOUD_PROJECT' => 'my-project', | ||
'HTTP_X_CLOUD_TRACE_CONTEXT' => 'my-traceId' | ||
], | ||
[ | ||
'type' => 'gae_app', | ||
'labels' => [ | ||
'project_id' => 'my-project', | ||
'version_id' => 'my-version', | ||
'module_id' => 'my-service' | ||
] | ||
], | ||
'my-project', | ||
'my-service', | ||
'my-version', | ||
[ | ||
'appengine.googleapis.com/trace_id' => | ||
sprintf( | ||
'projects/%s/traces/%s', | ||
'my-project', | ||
'my-traceId' | ||
) | ||
], | ||
], | ||
[ | ||
[ | ||
'GAE_SERVICE' => 'my-service', | ||
'GAE_VERSION' => 'my-version', | ||
'HTTP_X_CLOUD_TRACE_CONTEXT' => 'my-traceId' | ||
], | ||
[ | ||
'type' => 'gae_app', | ||
'labels' => [ | ||
'project_id' => 'unknown-projectid', | ||
'version_id' => 'my-version', | ||
'module_id' => 'my-service' | ||
] | ||
], | ||
'unknown-projectid', | ||
'my-service', | ||
'my-version', | ||
[ | ||
'appengine.googleapis.com/trace_id' => | ||
'my-traceId' | ||
], | ||
], | ||
[ | ||
[], | ||
[ | ||
'type' => 'gae_app', | ||
'labels' => [ | ||
'project_id' => 'unknown-projectid', | ||
'version_id' => 'unknown-version', | ||
'module_id' => 'unknown-service' | ||
] | ||
], | ||
'unknown-projectid', | ||
'unknown-service', | ||
'unknown-version', | ||
[], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.