Setting Up Stackdriver Trace

App Engine

Stackdriver Trace is automatically enabled for all applications running on App Engine standard environment. App Engine standard environment applications do not require any additional instrumentation libraries or SDKs.

For setup in the App Engine flexible environment, see the following section, VMs or containers.

VMs or containers

Applications running on VMs or containers are not automatically traced. This includes VM instances from Compute Engine and App Engine flexible environment, and containers in Google Container Engine. Use the SDKs listed below, or the Stackdriver Trace API, to send traces to the service.


Google.Cloud.Diagnostics.AspNetCore is a .NET Core client library that enables trace for your application using the Stackdriver Trace API v1.

To use the Google.Cloud.Diagnostics.AspNetCore client library in Visual Studio, do the following:

  1. Right-click your solution in Visual Studio and select Manage Nuget packages for solution.

  2. Select the Include prerelease checkbox.

  3. Search for and install the package named Google.Cloud.Diagnostics.AspNetCore.

Once the Google.Cloud.Diagnostics.AspNetCore client is installed, complete the following 4 steps to enable and configure trace:

  1. Add the following using directive to your application's Startup.cs file:

    using Google.Cloud.Diagnostics.AspNetCore;

  2. Add this code to your web application's appsettings.json file and update YOUR-GOOGLE-PROJECT-ID to be your project ID or number:

      "Stackdriver": {
        "ProjectId": "YOUR-GOOGLE-PROJECT-ID",
        "ServiceName": "NAME-OF-YOUR-SERVICE",
        "Version": "VERSION-OF-YOUR-SERVICE"

  3. Create or edit the ConfigureServices method in your application's Startup.cs file and then add the following code:

    // Add trace service.
    TraceConfiguration traceConfig = TraceConfiguration.Create(bufferOptions: BufferOptions.NoBuffer());
    services.AddGoogleTrace(Configuration["Stackdriver:ProjectId"], traceConfig);

  4. Edit the Configure method in your application's Startup.cs file and add the following code:

    // Configure trace service.

A sampling of the HTTP requests recieved by your application will now be traced automatically. You can also initiate traces from your application using the following code:

    public class TraceController : Controller
        // This incoming HTTP request should be captured by Trace.
        public IActionResult Index([FromServices] IManagedTracer tracer)
            using (tracer.StartSpan(nameof(Index)))
                var traceHeaderHandler = new TraceHeaderPropagatingHandler(() => tracer);
                var response = TraceOutgoing(traceHeaderHandler);
                ViewData["text"] = response.Result.ToString();
                return View();

        // This outgoing HTTP request should be captured by Trace.
        public async Task<string> TraceOutgoing(TraceHeaderPropagatingHandler traceHeaderHandler)
            // Add a handler to trace outgoing requests and to propagate the trace header.
            using (var httpClient = new HttpClient(traceHeaderHandler))
                string url = "";
                using (var response = await httpClient.GetAsync(url))
                    return await response.Content.ReadAsStringAsync();


The Google Cloud Trace SDK for Java is available on GitHub:


The Stackdriver Trace SDK for Go is available on GitHub. Usage instructions are included in the code comments.


See Setting Up Stackdriver Trace for Node.js.


The Stackdriver Trace SDK for Ruby is available on GitHub.

Viewing traces

After deployment, you can view recorded traces and inspect the RPCs in your application.

Send feedback about...

Stackdriver Trace