Class: Google::Cloud::Debugger::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/google/cloud/debugger/rails.rb

Overview

Railtie

Google::Cloud::Debugger::Railtie automatically adds the Google::Cloud::Debugger::Middleware to Rack in a Rails environment.

The Middleware is only added when certain conditions are met. See Railtie.use_debugger? for detail.

When loaded, the Google::Cloud::Debugger::Middleware will be inserted after the Rack::ETag Middleware, which is top of the Rack stack, closest to the application code.

The Railtie should also initialize a debugger to be used by the middleware. The debugger can be configured using the following Rails configuration:

Examples:

# Explicitly enable or disable Stackdriver Debugger Agent
config.google_cloud.use_debugger = true
# Shared Google Cloud Platform project identifier
config.google_cloud.project_id = "gcloud-project"
# Google Cloud Platform project identifier for Stackdriver Debugger
config.google_cloud.debugger.project_id = "debugger-project"
# Share Google Cloud authentication json file
config.google_cloud.keyfile = "/path/to/keyfile.json"
# Google Cloud authentication json file for Stackdriver Debugger only
config.google_cloud.debugger.keyfile = "/path/to/keyfile.json"
# Stackdriver Debugger Agent module name identifier
config.google_cloud.debugger.module_name = "my-ruby-app"
# Stackdriver Debugger Agent module version identifier
config.google_cloud.debugger.module_version = "v1"

Class Method Summary collapse

Class Method Details

.use_debugger?(config) ⇒ Boolean

Determine whether to use Stackdriver Debugger or not.

Returns true if valid GCP project_id is provided and underneath API is able to authenticate. Also either Rails needs to be in "production" environment or config.stackdriver.use_debugger is explicitly true.

Parameters:

  • config (Rails::Railtie::Configuration)

    The Rails.application.config

Returns:

  • (Boolean)

    Whether to use Stackdriver Debugger



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/google/cloud/debugger/rails.rb', line 92

def self.use_debugger? config
  debugger_config = parse_rails_config config

  # Return false if config.stackdriver.use_debugger is explicitly false
  use_debugger = debugger_config[:use_debugger]
  return false if !use_debugger.nil? && !use_debugger

  # Try authenticate authorize client API. Return false if unable to
  # authorize.
  begin
    Google::Cloud::Debugger::Credentials.credentials_with_scope(
      debugger_config[:keyfile])
  rescue => e
    Rails.log "Google::Cloud::Debugger is not activated due to " \
      "authorization error: #{e.message}"
    return false
  end

  project_id = debugger_config[:project_id] ||
               Google::Cloud::Debugger::Project.default_project
  if project_id.to_s.empty?
    Rails.log "Google::Cloud::Debugger is not activated due to empty " \
      "project_id"
    return false
  end

  # Otherwise default to true if Rails is running in production or
  # config.stackdriver.use_debugger is true
  Rails.env.production? || use_debugger
end