Skip to content

Commit

Permalink
Evaluate $server in the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Matsuo committed May 2, 2017
1 parent 0286fbd commit a87363f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/Core/Report/GAEFlexMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct(array $server)
$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' => [
Expand All @@ -52,7 +56,8 @@ public function __construct(array $server)
],
'projectId' => $projectId,
'serviceId' => $serviceId,
'versionId' => $versionId
'versionId' => $versionId,
'labels' => $labels
];
}

Expand Down Expand Up @@ -100,11 +105,6 @@ public function versionId()
*/
public function labels()
{
if (isset($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'])) {
return ['appengine.googleapis.com/trace_id' =>
substr($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'], 0, 32)];
} else {
return [];
}
return $this->data['labels'];
}
}
3 changes: 1 addition & 2 deletions tests/unit/Core/Report/GAEFlexMetadataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class GAEFlexMetadataProviderTest extends \PHPUnit_Framework_TestCase
'GAE_SERVICE' => 'my-service',
'GAE_VERSION' => 'my-version',
'GCLOUD_PROJECT' => 'my-project',
'HTTP_X_CLOUD_TRACE_CONTEXT' => 'my-traceId'
];

public function testWithEnvs()
{
$_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'] = 'my-traceId';
$metadataProvider = new GAEFlexMetadataProvider($this->envs);
$this->assertEquals(
[
Expand All @@ -58,7 +58,6 @@ public function testWithEnvs()

public function testWithOutEnvs()
{
unset($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT']);
$metadataProvider = new GAEFlexMetadataProvider([]);
$this->assertEquals(
[
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Logging/PsrBatchLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public function testTraceIdLabelOnGAEFlex(
$expectedLabels
) {
if (empty($traceId)) {
$server = [];
unset($_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT']);
} else {
$_SERVER['HTTP_X_CLOUD_TRACE_CONTEXT'] = $traceId;
$server = ['HTTP_X_CLOUD_TRACE_CONTEXT' => $traceId];
}
$this->runner->submitItem(
'stackdriver-logging-my-log', Argument::any()
Expand All @@ -98,7 +99,7 @@ public function testTraceIdLabelOnGAEFlex(
'my-log',
[
'batchRunner' => $this->runner->reveal(),
'metadataProvider' => new GaeFlexMetadataProvider([])
'metadataProvider' => new GaeFlexMetadataProvider($server)
]
);
$psrBatchLogger->info(
Expand Down

0 comments on commit a87363f

Please sign in to comment.