Setting up Stackdriver Trace for PHP

Stackdriver Trace can be enabled for PHP applications using the Stackdriver Trace library for PHP and the OpenCensus library for PHP.

Installing the library

Install the OpenCensus Stackdriver exporter composer package with the following command (this will also install OpenCensus and Trace composer packages):

composer require opencensus/opencensus-exporter-stackdriver

We recommend that you also install the OpenCensus extension with the following command:

pecl install opencensus-alpha

You also need to add the following line to your php.ini:

If you're on Windows, download the DLL file from pecl download page to your extension directory of your PHP installation and add the following line to your php.ini:


The opencensus extension is required if you want to use the framework integrations described below.

Enabling the library

To enable the Stackdriver Trace library for PHP, complete the following steps:

  1. Import the classes

    use OpenCensus\Trace\Exporter\StackdriverExporter;
    use OpenCensus\Trace\Tracer;

  2. Initialize the exporter

    $exporter = new StackdriverExporter([
        'clientConfig' => [
            'projectId' => $projectId
  3. Start the tracer


Put the code at the top of the script to get the best result.

Configuring the client

You can customize the behavior of the Stackdriver Trace library for PHP. See the exporter's configuration and the tracer's configuration for a list of possible configuration options.

Add Custom Trace Span

The Stackdriver Trace library for PHP automatically creates a trace record for each HTTP request. You can also add custom trace spans within the request:

    ['name' => 'slow_function'],
    function () {

Framework integrations

The OpenCensus library provides useful integrations for web frameworks and functions. See available integrations and examples for some frameworks for more details.

Running on Google Cloud Platform

You don't need to provide authentication credentials for instances running on Google Cloud Platform, provided Stackdriver Trace API access scope is enabled on that instance. However, we recommend setting the broadest possible access scope for your instance and then using Cloud Identity and Access Management to restrict access.

App Engine flexible environment

On App Engine flexible environment, the Stackdriver Trace API access scope is enabled by default, and the OpenCensus client library can be used without providing credentials or a project ID.

App Engine flexible environment provides a performance optimization for reporting the trace data in a background process. Use async option when initializing the Stackdriver exporter, and use enable_stackdriver_integration in runtime_config section in your app.yaml file as follows:

enable_stackdriver_integration: true

See also the exporter's configuration.

App Engine standard environment

On App Engine standard environment, the Stackdriver Trace API access scope is enabled by default, and the OpenCensus client library can be used without providing credentials or a project ID.


To use Trace with GKE, add the trace.append OAuth access scope when you create your cluster.

To create a cluster using gcloud, do the following:

  1. (Optional) Update gcloud to the latest version:
    gcloud components update
  2. Set your default project ID:
    gcloud config set project [PROJECT_ID]
  3. If you're working with zonal clusters, set your default compute zone:
    gcloud config set compute/zone [COMPUTE_ZONE]
  4. If you're working with regional clusters, set your default compute region:
    gcloud config set compute/region [COMPUTE_REGION]
  5. Issue the create command:
    gcloud container clusters create example-cluster-name --scopes

For more detailed information on creating a cluster, see Creating a cluster.

Compute Engine

For Compute Engine VM instances, you need to explicitly enable the trace.append Stackdriver Trace API access scope for each VM instance. When creating a new instance through the Google Cloud Platform Console, select the following values in the Identity and API access section of the Create an instance panel:

  1. Select Compute Engine default service account for Service account
  2. Select Allow full access to all Cloud APIs for Access Scopes

To use something other than the Compute Engine default service account see the Creating and enabling service accounts for instances and Running locally and elsewhere sections. The important thing is that the service account you use has the Cloud Trace Agent role.

Running locally and elsewhere

To run Stackdriver Trace outside of GCP you must supply your GCP project ID and appropriate service account credentials directly to OpenCensus client library . Your service account must contain the Cloud Trace agent role. For instructions, see Creating a service account.

Project ID

Provide a project ID to the OpenCensus as shown in Initialize the exporter. We recommend that you set the environment variable GOOGLE_CLOUD_PROJECT to your project ID and modify your application to read this variable. To set this environment variable, do the following:

Linux or macOS

export GOOGLE_CLOUD_PROJECT=your-project-id


set GOOGLE_CLOUD_PROJECT=your-project-id




GCP client libraries use Application default credentials (ADC) to find your application's credentials. You provide credentials to the OpenCensus client library by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable:


    export GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key


    set GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-accounts-private-key



Viewing the traces

After deployment, you can view the traces in the GCP Console Trace Viewer.

Go to the Trace Viewer page

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Trace
Need help? Visit our support page.