-
Notifications
You must be signed in to change notification settings - Fork 438
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
Support log correlation on App Engine standard 2nd gen #1407
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
124 changes: 124 additions & 0 deletions
124
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,124 @@ | ||
<?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() | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
{ | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.