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

Support log correlation on App Engine standard 2nd gen #1407

Merged
merged 5 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()

This comment was marked as spam.

This comment was marked as spam.

{
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