Setting Up Stackdriver Trace for Ruby

Stackdriver Trace can be enabled for Ruby applications using the Stackdriver Trace library for Ruby.

Installing the library

  1. Install Ruby 2.2+ or greater.

  2. Add the stackdriver gem to your Gemfile:

    gem "stackdriver"

  3. Use bundler to install the gem:

    bundle install
    

Enabling the library

Rails

If you're using Ruby on Rails, bundler automatically loads the library into your application when it starts.

Not Rails

Other Rack-based applications can use the Rack Middleware provided by the library:

require "google/cloud/trace"

use Google::Cloud::Trace::Middleware

For more information or to report issues with the library, see the library's source code repository.

Configuring the client

You can customize the behavior of the Stackdriver Trace library for Ruby. See the library's configuration for a list of possible configuration options.

Add Custom Trace Span

The Stackdriver Trace library for Ruby automatically creates a trace record for each request your Rack application receives. You can also add custom trace spans within each request:

Google::Cloud::Trace.in_span "my_task" do |span|
  # Insert task

  Google::Cloud::Trace.in_span "my_subtask" do |subspan|
    # Insert subtask
  end
end

Running on Google Cloud Platform

The Stackdriver Trace library for Ruby should work without you manually providing authentication credentials for instances running on Google Cloud Platform, as long as the Stackdriver Trace API access scope is enabled on that instance.

App Engine

On Google App Engine, the Stackdriver Trace API access scope is enabled by default, and the Stackdriver Trace library for Ruby can be used without providing credentials or a project ID.

Kubernetes Engine

On Google Kubernetes Engine, you must explicitly add the trace.append OAuth scope when creating the cluster:

gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/trace.append

Compute Engine

For Google Compute Engine instances, you need to explicitly enable the trace.append Stackdriver Trace API access scope for each instance. When creating a new instance through the Google Cloud Platform Console, you can do this under Identity and API access: Use the Compute Engine default service account and select "Allow full access to all Cloud APIs" under Access scopes.

To use something other than the Compute Engine default service account see the docs for Creating and Enabling Service Accounts for Instances and the Running elsewhere section below. The important thing is that the service account you use has the Cloud Trace Agent role.

Running locally and elsewhere

To run the Stackdriver Trace outside of Google Cloud Platform, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Trace. This applies to running the library on your own workstation, on your datacenter's computers, or on the VM instances of another cloud provider. Here are the steps involved:

  1. Create a new service account in your GCP project. It must contain at least the Cloud Trace Agent role. For instructions, see Creating a service account.

  2. Download the service account's key credentials file to your computer. For instructions, see Creating and managing service account keys.

  3. Provide your GCP project ID and the location of the credentials file to the Stackdriver Trace using either parameters (project_id and keyfile) or environment variables (GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS). These methods are shown below.

Using environment variables

Define the environment variables where they are visible to the Stackdriver Trace. If they are visible more widely, they could interfere with existing authorizations for other applications.

Linux or Mac OS X

export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

Windows

Command window:

set GOOGLE_CLOUD_PROJECT=your-project-id
set GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json

Powershell:

$env:GOOGLE_CLOUD_PROJECT="your-project-id"
$env:GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"

Using Ruby on Rails configuration interface

Provide the parameters through the Ruby on Rails configuration interface if you are using Ruby on Rails framework:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Trace specific parameters
  config.google_cloud.trace.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.trace.keyfile    = "/path/to/service-account.json"
end

You can also set shared configuration for all stackdriver gems using:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Shared parameters
  config.google_cloud.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.keyfile    = "/path/to/service-account.json"
end

Using instrumentation configuration interface

Provide the parameters through the configuration interface when used in other Rack-based applications:

require "google/cloud/trace"

Google::Cloud.configure do |config|
  # Stackdriver Trace specific parameters
  config.trace.project_id = "YOUR-PROJECT-ID"
  config.trace.keyfile    = "/path/to/service-account.json"
end

You can also set shared configuration for all stackdriver gems using:

require "stackdriver"

Google::Cloud.configure do |config|
  # Stackdriver Shared parameters
  config.project_id = "YOUR-PROJECT-ID"
  config.keyfile    = "/path/to/service-account.json"
end

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