Skip to content

Commit f0b81fc

Browse files
author
Takashi Matsuo
authored
Add Stackdriver Trace sample code for the doc (GoogleCloudPlatform#561)
* Add Stackdriver Trace sample code for the doc * Correct the directory structure and CS fix * Format, better comment, etc
1 parent 3639d83 commit f0b81fc

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

testing/run_test_suite.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ do
6565
fi
6666
fi
6767
pushd ${DIR}
68+
mkdir -p build/logs
6869
# Temporarily allowing error
6970
set +e
7071
if [ -f "composer.json" ]; then

trace/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# The sample code and the test for Stackdriver Trace documentation
2+
3+
This directory holds the sample code and the test for the following page:
4+
https://cloud.google.com/trace/docs/setup/php

trace/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"opencensus/opencensus": "^0.2.1",
4+
"google/cloud-trace": "^0.6.2"
5+
}
6+
}

trace/phpunit.xml.dist

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="./vendor/autoload.php">
18+
<testsuites>
19+
<testsuite name="PHP Stackdriver Trace test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<file>trace-sample.phpfile>
29+
whitelist>
30+
filter>
31+
<php>
32+
<env name="USE_NULL_EXPORTER" value="true"/>
33+
php>
34+
phpunit>

trace/test/TraceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
require_once __DIR__ . '/../trace-sample.php';
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class TraceTest extends TestCase
8+
{
9+
public function testTraceSample()
10+
{
11+
trace_callable();
12+
$reflection = new \ReflectionProperty('\OpenCensus\Trace\Tracer', 'instance');
13+
$reflection->setAccessible(true);
14+
$handler = $reflection->getValue();
15+
$tracer = $handler->tracer();
16+
$spans = $tracer->spans();
17+
$this->assertEquals(2, count($spans));
18+
$this->assertEquals('slow_function', $spans[1]->name());
19+
}
20+
}

trace/trace-sample.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
require_once __DIR__ . '/vendor/autoload.php';
19+
20+
use OpenCensus\Trace\Exporter\NullExporter;
21+
# [START trace_use_statement]
22+
use OpenCensus\Trace\Exporter\StackdriverExporter;
23+
use OpenCensus\Trace\Tracer;
24+
25+
# [END trace_use_statement]
26+
27+
$projectId = getenv('GOOGLE_CLOUD_PROJECT');
28+
if ($projectId === false) {
29+
die('Set GOOGLE_CLOUD_PROJECT envvar');
30+
}
31+
32+
# [START exporter_setup]
33+
$exporter = new StackdriverExporter([
34+
'clientConfig' => [
35+
'projectId' => $projectId
36+
]
37+
]);
38+
# [END exporter_setup]
39+
// When running tests, use a null exporter instead.
40+
if (getenv('USE_NULL_EXPORTER')) {
41+
$exporter = new NullExporter();
42+
}
43+
# [START tracer_start]
44+
Tracer::start($exporter);
45+
# [END tracer_start]
46+
47+
function trace_callable()
48+
{
49+
# [START span_with_closure]
50+
Tracer::inSpan(
51+
['name' => 'slow_function'],
52+
function () {
53+
sleep(1);
54+
}
55+
);
56+
# [END span_with_closure]
57+
}

0 commit comments

Comments
 (0)