Skip to content

Commit

Permalink
Support log correlation on App Engine standard 2nd gen (#1407)
Browse files Browse the repository at this point in the history
* 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
Takashi Matsuo authored and dwsupplee committed Nov 8, 2018
1 parent 91ea697 commit 38457a8
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 84 deletions.
87 changes: 3 additions & 84 deletions Core/src/Report/GAEFlexMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,91 +20,10 @@
/**
* An MetadataProvider for GAE Flex.
*/
class GAEFlexMetadataProvider implements MetadataProviderInterface
class GAEFlexMetadataProvider extends GAEMetadataProvider
{
/** @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' =>
substr($server['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32)]
: [];
$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()
protected function getTraceValue($server)
{
return $this->data['labels'];
return substr($server['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32);
}
}
112 changes: 112 additions & 0 deletions Core/src/Report/GAEMetadataProvider.php
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'];
}
}
37 changes: 37 additions & 0 deletions Core/src/Report/GAEStandardMetadataProvider.php
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;
}
}
3 changes: 3 additions & 0 deletions Core/src/Report/MetadataProviderUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class MetadataProviderUtils
public static function autoSelect($server)
{
if (isset($server['GAE_SERVICE'])) {
if (isset($server['GAE_ENV']) && $server['GAE_ENV'] === 'standard') {
return new GAEStandardMetadataProvider($server);
}
return new GAEFlexMetadataProvider($server);
}
return new EmptyMetadataProvider();
Expand Down
123 changes: 123 additions & 0 deletions Core/tests/Unit/Report/GAEStandardMetadataProviderTest.php
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',
[],
],
];
}
}
Loading

0 comments on commit 38457a8

Please sign in to comment.