Class: Google::Cloud::ErrorReporting::Railtie

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

Overview

Railtie

Google::Cloud::ErrorReporting::Railtie automatically add the Google::Cloud::ErrorReporting::Middleware to Rack in a Rails environment. It will automatically capture Exceptions from the Rails app and report them to Stackdriver Error Reporting.

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

When loaded, the Google::Cloud::ErrorReporting::Middleware will be inserted after ::ActionDispatch::DebugExceptions Middleware, which allows it to actually rescue all Exceptions and throw it back up. The middleware should also be initialized with correct gcp project_id, keyfile, service_name, and service_version if they are defined in Rails environment files as follow: config.google_cloud.project_id = "my-gcp-project" config.google_cloud.keyfile = "/path/to/secret.json" or config.google_cloud.error_reporting.project_id = "my-gcp-project" config.google_cloud.error_reporting.keyfile = "/path/to/secret.json" config.google_cloud.error_reporting.service_name = "my-service-name" config.google_cloud.error_reporting.service_version = "v1"

Class Method Summary collapse

Class Method Details

.grpc_channel(keyfile = nil) ⇒ GRPC::Core::Channel

Construct a gRPC Channel object from given keyfile

Parameters:

  • keyfile (String, Hash) (defaults to: nil)

    Keyfile downloaded from Google Cloud. If file path the file must be readable.

Returns:

  • (GRPC::Core::Channel)

    gRPC Channel object based on given keyfile



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/google/cloud/error_reporting/rails.rb', line 98

def self.grpc_channel keyfile = nil
  require "grpc"

  scopes = V1beta1::ReportErrorsServiceClient::ALL_SCOPES
  credentials = if keyfile.nil?
                  Google::Cloud::Credentials.default(
                    scope: scopes)
                else
                  Google::Cloud::Credentials.new(
                    keyfile, scope: scopes)
                end

  # Return nil if :this_channel_is_insecure
  return nil if credentials == :this_channel_is_insecure

  channel_cred = GRPC::Core::ChannelCredentials.new.compose \
    GRPC::Core::CallCredentials.new credentials.client.updater_proc
  host = V1beta1::ReportErrorsServiceClient::SERVICE_ADDRESS

  GRPC::Core::Channel.new host, nil, channel_cred
end

.use_error_reporting?(config) ⇒ Boolean

Determine whether to use Stackdriver Error Reporting or not.

Returns true if valid GCP project_id and keyfile are provided and either Rails is in "production" environment or \ config.google_cloud.use_error_reporting is explicitly true. Otherwise false.

Parameters:

  • config (Rails::Railtie::Configuration)

    The Rails.application.config

Returns:

  • (Boolean)

    Whether to use Stackdriver Error Reporting



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/google/cloud/error_reporting/rails.rb', line 133

def self.use_error_reporting? config
  gcp_config = config.google_cloud
  er_config = gcp_config.error_reporting

  # Return false if config.google_cloud.use_error_reporting is
  # explicitly false
  return false if gcp_config.key?(:use_error_reporting) &&
                  !gcp_config.use_error_reporting

  # Check credentialing. Returns false if authorization errors are
  # rescued.
  keyfile = er_config.keyfile || gcp_config.keyfile
  begin
    grpc_channel keyfile
  rescue StandardError => e
    Rails.logger.warn "Google::Cloud::ErrorReporting is not " \
    "activated due to authorization error: #{e.message}"
    return false
  end

  if project_id(config).to_s.empty?
    Rails.logger.warn "Google::Cloud::ErrorReporting is not " \
    "activated due to empty project_id"
    return false
  end

  # Otherwise return true if Rails is running in production or
  # config.google_cloud.use_error_reporting is explicitly true
  Rails.env.production? ||
    (gcp_config.key?(:use_error_reporting) &&
      gcp_config.use_error_reporting)
end