Class: Google::Cloud::ErrorReporting::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/error_reporting/project.rb

Overview

Project

Projects are top-level containers in Google Cloud Platform. They store information about billing and authorized users, and they control access to Stackdriver ErrorReporting. Each project has a friendly name and a unique ID. Projects can be created only in the Google Developers Console.

See new

Examples:

require "google/cloud/error_reporting"

error_reporting = Google::Cloud::ErrorReporting.new
error_event = error_reporting.error_event "Error with Backtrace",
                                          event_time: Time.now,
                                          service_name: "my_app_name"
error_reporting.report error_event

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_project_idString Also known as: default_project

Find default project_id from ERROR_REPORTING_RPOJECT, GOOGLE_CLOUD_PROJECT, GCLOUD_PROJECT environment varaibles, or query from GCE meta service.

Returns:

  • (String)

    default valid GCP project_id



53
54
55
56
57
# File 'lib/google/cloud/error_reporting/project.rb', line 53

def self.default_project_id
  ENV["ERROR_REPORTING_PROJECT"] ||
    ENV["GOOGLE_CLOUD_PROJECT"] ||
    Google::Cloud.env.project_id
end

.default_service_nameString

Find default service_name from ERROR_REPORTING_SERVICE, GAE_SERVICE environment Variables, or just "ruby".

Returns:

  • (String)

    default GCP service_name



68
69
70
71
72
# File 'lib/google/cloud/error_reporting/project.rb', line 68

def self.default_service_name
  ENV["ERROR_REPORTING_SERVICE"] ||
    Google::Cloud.env.app_engine_service_id ||
    "ruby"
end

.default_service_versionString

Find default service_version from ERROR_REPORTING_VERSION or GAE_VERSION environment varaibles.

Returns:

  • (String)

    default GCP service_version



80
81
82
83
# File 'lib/google/cloud/error_reporting/project.rb', line 80

def self.default_service_version
  ENV["ERROR_REPORTING_VERSION"] ||
    Google::Cloud.env.app_engine_service_version
end

Instance Method Details

#error_event(message = nil, service_name: nil, service_version: nil, event_time: nil, user: nil, file_path: nil, line_number: nil, function_name: nil) ⇒ ErrorEvent

Create a new ErrorEvent instance with given parameters.

Examples:

require "google/cloud/error_reporting"

error_reporting = Google::Cloud::ErrorReporting.new

error_event =
  error_reporting.error_event "Error Message with Backtrace",
                              event_time: Time.now,
                              service_name: "my_app_name",
                              service_version: "v8",
                              user: "johndoh",
                              file_path: "MyController.rb",
                              line_number: 123,
                              function_name: "index"
error_reporting.report error_event

Parameters:

  • message (String) (defaults to: nil)

    The error message along with backtrace

  • service_name (String)

    The service's name. Default to default_service_name

  • service_version (String)

    The service's version. Default to default_service_version

  • event_time (Time)

    Time when the event occurred. If not provided, the time when the event was received by the Error Reporting system will be used.

  • user (String)

    The user who caused or was affected by the crash. This can be a user ID, an email address, or an arbitrary token that uniquely identifies the user. When sending an error report, leave this field empty if the user was not logged in. In this case the Error Reporting system will use other data, such as remote IP address, to distinguish affected users.

  • file_path (String)

    The source code filename, which can include a truncated relative path, or a full path from a production machine.

  • line_number (Number)

    1-based. 0 indicates that the line number is unknown.

  • function_name (String)

    Human-readable name of a function or method. The value can include optional context like the class or package name. For example, my.package.MyClass.method in case of Java.

Returns:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/google/cloud/error_reporting/project.rb', line 211

def error_event message = nil, service_name: nil, service_version: nil,
                event_time: nil, user: nil, file_path: nil,
                line_number: nil, function_name: nil
  ErrorEvent.new.tap do |e|
    e.message = message
    e.event_time = event_time
    e.service_name = service_name || self.class.default_service_name
    e.service_version = service_version ||
                        self.class.default_service_version
    e.user = user
    e.file_path = file_path
    e.line_number = line_number
    e.function_name = function_name
  end
end

#project_idString Also known as: project

Get the name of current project_id from underneath gRPC Service object.

Returns:

  • (String)

    The current project_id



108
109
110
# File 'lib/google/cloud/error_reporting/project.rb', line 108

def project_id
  service.project
end

#report(*args, &block) ⇒ Object

Report a ErrorEvent to Stackdriver Error Reporting service.

Examples:

require "google/cloud/error_reporting"

error_reporting = Google::Cloud::ErrorReporting.new

error_event = error_reporting.error_event "Error with Backtrace"
error_reporting.report error_event


125
126
127
# File 'lib/google/cloud/error_reporting/project.rb', line 125

def report *args, &block
  service.report *args, &block
end

#report_exception(exception, service_name: nil, service_version: nil) {|error_event| ... } ⇒ Object

Create a ErrorEvent from the given exception, and report this ErrorEvent to Stackdriver Error Reporting service.

Examples:

require "google/cloud/error_reporting"

error_reporting = Google::Cloud::ErrorReporting.new

begin
  fail StandardError, "A serious problem"
rescue => exception
  error_reporting.report_exception exception,
                                   service_name: "my_app_name",
                                   service_version: "v8"
end

Parameters:

Yields:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/google/cloud/error_reporting/project.rb', line 153

def report_exception exception, service_name: nil, service_version: nil
  error_event = ErrorEvent.from_exception exception

  error_event.service_name =
    service_name || self.class.default_service_name
  error_event.service_version =
    service_version || self.class.default_service_version

  yield error_event if block_given?

  report error_event
end