Skip to content

Add Stackdriver Trace sample code for the doc #561

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

Merged
merged 3 commits into from
Mar 1, 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
1 change: 1 addition & 0 deletions testing/run_test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ do
fi
fi
pushd ${DIR}
mkdir -p build/logs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

# Temporarily allowing error
set +e
if [ -f "composer.json" ]; then
Expand Down
4 changes: 4 additions & 0 deletions trace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# The sample code and the test for Stackdriver Trace documentation

This directory holds the sample code and the test for the following page:
https://cloud.google.com/trace/docs/setup/php
6 changes: 6 additions & 0 deletions trace/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"opencensus/opencensus": "^0.2.1",
"google/cloud-trace": "^0.6.2"
}
}
34 changes: 34 additions & 0 deletions trace/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2018 Google Inc.

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.
-->
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="PHP Stackdriver Trace test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<file>trace-sample.php</file>
</whitelist>
</filter>
<php>
<env name="USE_NULL_EXPORTER" value="true"/>
</php>
</phpunit>
20 changes: 20 additions & 0 deletions trace/test/TraceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require_once __DIR__ . '/../trace-sample.php';

use PHPUnit\Framework\TestCase;

class TraceTest extends TestCase
{
public function testTraceSample()
{
trace_callable();
$reflection = new \ReflectionProperty('\OpenCensus\Trace\Tracer', 'instance');
$reflection->setAccessible(true);
$handler = $reflection->getValue();
$tracer = $handler->tracer();
$spans = $tracer->spans();
$this->assertEquals(2, count($spans));
$this->assertEquals('slow_function', $spans[1]->name());
}
}
57 changes: 57 additions & 0 deletions trace/trace-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright 2018 Google Inc.
*
* 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.
*/

require_once __DIR__ . '/vendor/autoload.php';

use OpenCensus\Trace\Exporter\NullExporter;
# [START trace_use_statement]
use OpenCensus\Trace\Exporter\StackdriverExporter;
use OpenCensus\Trace\Tracer;

# [END trace_use_statement]

$projectId = getenv('GOOGLE_CLOUD_PROJECT');
if ($projectId === false) {
die('Set GOOGLE_CLOUD_PROJECT envvar');
}

# [START exporter_setup]
$exporter = new StackdriverExporter([
'clientConfig' => [
'projectId' => $projectId
]
]);
# [END exporter_setup]
// When running tests, use a null exporter instead.
if (getenv('USE_NULL_EXPORTER')) {
$exporter = new NullExporter();
}
# [START tracer_start]
Tracer::start($exporter);
# [END tracer_start]

function trace_callable()
{
# [START span_with_closure]
Tracer::inSpan(
['name' => 'slow_function'],
function () {
sleep(1);
}
);
# [END span_with_closure]
}