class Google::Cloud::Trace::Railtie

# Rails integration for Stackdriver Trace

This Railtie is a drop-in Stackdriver Trace instrumentation plugin for Ruby on Rails applications. If present, it automatically instruments your Rails app to record performance traces and cause them to appear on your Stackdriver console.

## Installation

To install this plugin, the gem `google-cloud-trace` must be in your Gemfile. You also must add the following line to your `application.rb` file:

“`ruby require “google/cloud/trace/rails” “`

If you include the `stackdriver` gem in your Gemfile, the above is done for you automatically, and you do not need to edit your `application.rb`.

## Configuration

See the {file:INSTRUMENTATION.md Instrumentation Guide} and [Configuration Guide](googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html) on how to configure the Railtie and Middleware.

## Measuring custom functionality

To add a custom measurement to a request trace, use the classes provided in this library. Below is an example to get you started.

“`ruby class MyController < ApplicationController

def index
  Google::Cloud::Trace.in_span "Sleeping on the job!" do
    sleep rand
  end
  render plain: "Hello World!"
end

end “`

Constants

DEFAULT_NOTIFICATIONS

The default list of ActiveSupport notification types to include in traces.

Public Class Methods

consolidate_rails_config(config) click to toggle source

@private Consolidate Rails configuration into Trace instrumentation configuration. Also consolidate the `use_trace` setting by verifying credentials and Rails environment. The `use_trace` setting will be true if credentials are valid, and the setting is manually set to true or Rails is in production environment.

@param [Rails::Railtie::Configuration] config The

Rails.application.config
# File lib/google/cloud/trace/rails.rb, line 125
def self.consolidate_rails_config config
  merge_rails_config config

  init_default_config

  # Done if Google::Cloud.configure.use_trace is explicitly false
  return if Google::Cloud.configure.use_trace == false

  # Verify credentials and set use_trace to false if
  # credentials are invalid
  unless valid_credentials? Trace.configure.project_id,
                            Trace.configure.credentials
    Cloud.configure.use_trace = false
    return
  end

  # Otherwise set use_trace to true if Rails is running in production
  Google::Cloud.configure.use_trace ||= Rails.env.production?
end
init_middleware(app) click to toggle source

Initialize trace integration for Rails. Sets up the configuration, adds and configures middleware, and installs notifications.

@private

# File lib/google/cloud/trace/rails.rb, line 101
def self.init_middleware app
  trace_config = Trace.configure

  app.middleware.insert_before Rack::Runtime,
                               Google::Cloud::Trace::Middleware

  trace_config.notifications.each do |type|
    Google::Cloud::Trace::Notifications.instrument \
      type,
      max_length: trace_config.max_data_length,
      capture_stack: trace_config.capture_stack
  end
end

Private Class Methods

init_default_config() click to toggle source

Fallback to default config values if config parameters not provided.

# File lib/google/cloud/trace/rails.rb, line 178
def self.init_default_config
  Trace.configure.project_id ||= Trace.default_project_id
end
merge_rails_config(rails_config) click to toggle source

@private Merge Rails configuration into Trace instrumentation configuration.

# File lib/google/cloud/trace/rails.rb, line 150
def self.merge_rails_config rails_config
  gcp_config = rails_config.google_cloud
  trace_config = gcp_config.trace

  if Cloud.configure.use_trace.nil?
    Cloud.configure.use_trace = gcp_config.use_trace
  end
  Trace.configure do |config|
    config.project_id ||= (config.project ||
      trace_config.project_id || trace_config.project ||
      gcp_config.project_id || gcp_config.project)
    config.credentials ||= (config.keyfile ||
      trace_config.credentials || trace_config.keyfile ||
      gcp_config.credentials || gcp_config.keyfile)
    config.notifications ||= trace_config.notifications
    config.max_data_length ||= trace_config.max_data_length
    if config.capture_stack.nil?
      config.capture_stack = trace_config.capture_stack
    end
    config.sampler ||= trace_config.sampler
    config.span_id_generator ||= trace_config.span_id_generator
  end
end
valid_credentials?(project_id, credentials) click to toggle source

@private Verify credentials

# File lib/google/cloud/trace/rails.rb, line 184
def self.valid_credentials? project_id, credentials
  begin
    # if credentials is nil, get default
    credentials ||= Trace::Credentials.default
    # only create a new Credentials object if the val isn't one already
    unless credentials.is_a? Google::Auth::Credentials
      # if credentials is not a Credentials object, create one
      Trace::Credentials.new credentials
    end
  rescue Exception => e
    $stdout.puts "Note: Google::Cloud::Trace is disabled because " \
      "it failed to authorize with the service. (#{e.message})"
    return false
  end

  if project_id.to_s.empty?
    $stdout.puts "Note: Google::Cloud::Trace is disabled because " \
      "the project ID could not be determined."
    return false
  end

  true
end