NOTE: This repository is part of Google Cloud PHP. Any
support requests, bug reports, or development contributions should be directed to
that project.
A distributed tracing system for Google Cloud Platform that collects latency
data from App Engine applications and displays it in near real time in the
Google Cloud Platform Console.
Installation
To begin, install the preferred dependency manager for PHP,
Composer.
Now install this component:
$ composer require google/cloud-trace
This component supports both REST over HTTP/1.1 and gRPC. In order to take
advantage of the benefits offered by gRPC (such as streaming methods)
please see our gRPC installation guide.
Authentication
Please see our Authentication guide
for more information on authenticating your client. Once authenticated, you'll
be ready to start making requests.
Sample
require 'vendor/autoload.php';
use Google\Cloud\Trace\TraceClient;
$traceClient = new TraceClient();
// Create a Trace
$trace = $traceClient->trace();
$span = $trace->span([
'name' => 'main'
]);
$span->setStartTime();
// some expensive operation
$span->setEndTime();
$trace->setSpans([$span]);
$traceClient->insert($trace);
// List recent Traces
foreach($traceClient->traces() as $trace) {
var_dump($trace->traceId());
}
Creating a Trace
use Google\Cloud\Trace\TraceClient;
$client = new TraceClient();
$trace = $client->trace();
$span = $trace->span(['name' => 'main']);
$trace->setSpans([$span]);
$client->insert($trace);
Using OpenCensus
We highly recommend using the OpenCensus project to instrument
your application. OpenCensus is an open source, distributed tracing framework
that maintains integrations with popular frameworks and tools. OpenCensus
provides a data exporter for Stackdriver Trace which uses this library. If you
were using google/cloud-trace <= v0.3.3 or google/cloud <= 0.46.0, then check
out the migration guide to OpenCensus.
Install with composer or add to your composer.json.
opencensus/opencensus provides a service-agnostic implementation. Be sure to
also require opencensus/opencensus-exporter-stackdriver to enable exporting of
traces to Stackdriver Trace.
use OpenCensus\Trace\Exporter\StackdriverExporter;
use OpenCensus\Trace\Tracer;
Tracer::start(new StackdriverExporter());
Please see our Debugging guide
for more information about the debugging tools.
Version
This component is considered GA (generally available). As such, it will not introduce backwards-incompatible changes in
any minor or patch releases. We will address issues and requests with the highest priority.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["Version latestkeyboard_arrow_down\n\n- [1.8.10 (latest)](/php/docs/reference/cloud-trace/latest)\n- [1.8.9](/php/docs/reference/cloud-trace/1.8.9)\n- [1.7.0](/php/docs/reference/cloud-trace/1.7.0)\n- [1.6.5](/php/docs/reference/cloud-trace/1.6.5)\n- [1.5.3](/php/docs/reference/cloud-trace/1.5.3)\n- [1.4.11](/php/docs/reference/cloud-trace/1.4.11) \n\nStackdriver Trace for PHP\n=========================\n\n\u003e Idiomatic PHP client for [Stackdriver Trace](https://cloud.google.com/trace/).\n\n[](https://packagist.org/packages/google/cloud-trace) [](https://packagist.org/packages/google/cloud-trace)\n\n**NOTE:** This repository is part of [Google Cloud PHP](https://cloud.google.com/php/docs/reference). Any\nsupport requests, bug reports, or development contributions should be directed to\nthat project.\n\nA distributed tracing system for Google Cloud Platform that collects latency\ndata from App Engine applications and displays it in near real time in the\nGoogle Cloud Platform Console.\n\n### Installation\n\nTo begin, install the preferred dependency manager for PHP,\n[Composer](https://getcomposer.org/).\n\nNow install this component: \n\n $ composer require google/cloud-trace\n\nThis component supports both REST over HTTP/1.1 and gRPC. In order to take\nadvantage of the benefits offered by gRPC (such as streaming methods)\nplease see our [gRPC installation guide](https://cloud.google.com/php/grpc).\n\n### Authentication\n\nPlease see our [Authentication guide](https://github.com/googleapis/google-cloud-php/blob/main/AUTHENTICATION.md)\nfor more information on authenticating your client. Once authenticated, you'll\nbe ready to start making requests.\n\n### Sample\n\n require 'vendor/autoload.php';\n\n use Google\\Cloud\\Trace\\TraceClient;\n\n $traceClient = new TraceClient();\n\n // Create a Trace\n $trace = $traceClient-\u003etrace();\n $span = $trace-\u003espan([\n 'name' =\u003e 'main'\n ]);\n $span-\u003esetStartTime();\n // some expensive operation\n $span-\u003esetEndTime();\n\n $trace-\u003esetSpans([$span]);\n $traceClient-\u003einsert($trace);\n\n // List recent Traces\n foreach($traceClient-\u003etraces() as $trace) {\n var_dump($trace-\u003etraceId());\n }\n\n### Creating a Trace\n\n use Google\\Cloud\\Trace\\TraceClient;\n\n $client = new TraceClient();\n $trace = $client-\u003etrace();\n $span = $trace-\u003espan(['name' =\u003e 'main']);\n $trace-\u003esetSpans([$span]);\n\n $client-\u003einsert($trace);\n\n### Using OpenCensus\n\nWe highly recommend using the [OpenCensus](http://opencensus.io) project to instrument\nyour application. OpenCensus is an open source, distributed tracing framework\nthat maintains integrations with popular frameworks and tools. OpenCensus\nprovides a data exporter for Stackdriver Trace which uses this library. If you\nwere using google/cloud-trace \\\u003c= v0.3.3 or google/cloud \\\u003c= 0.46.0, then check\nout the [migration guide to OpenCensus](http://opencensus.io/opencensus-php/migrating-stackdriver-trace).\n\nInstall with `composer` or add to your `composer.json`. \n\n $ composer require opencensus/opencensus opencensus/opencensus-exporter-stackdriver\n\n`opencensus/opencensus` provides a service-agnostic implementation. Be sure to\nalso require `opencensus/opencensus-exporter-stackdriver` to enable exporting of\ntraces to Stackdriver Trace. \n\n use OpenCensus\\Trace\\Exporter\\StackdriverExporter;\n use OpenCensus\\Trace\\Tracer;\n\n Tracer::start(new StackdriverExporter());\n\nSee the [OpenCensus documentation](https://github.com/census-instrumentation/opencensus-php) for more configuration\noptions and features.\n\n### Debugging\n\nPlease see our [Debugging guide](https://github.com/googleapis/google-cloud-php/blob/main/DEBUG.md)\nfor more information about the debugging tools.\n\n### Version\n\nThis component is considered GA (generally available). As such, it will not introduce backwards-incompatible changes in\nany minor or patch releases. We will address issues and requests with the highest priority.\n\n### Next Steps\n\n1. Understand the [official documentation](https://cloud.google.com/trace/docs/).\n2. Take a look at [in-depth usage samples](https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/trace/)."]]