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",
                                          timestamp: Time.now
                                          service_name: "my_app_name"
error_reporting.report error_event

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_projectString

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
  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_MODULE_NAME environment Variables, or just "ruby".

Returns:

  • (String)

    default GCP service_name



65
66
67
68
69
# File 'lib/google/cloud/error_reporting/project.rb', line 65

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_MODULE_VERSION environment varaibles.

Returns:

  • (String)

    default GCP service_version



77
78
79
80
# File 'lib/google/cloud/error_reporting/project.rb', line 77

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:



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/google/cloud/error_reporting/project.rb', line 206

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

#projectString

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

Returns:

  • (String)

    The current project_id



105
106
107
# File 'lib/google/cloud/error_reporting/project.rb', line 105

def project
  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


121
122
123
# File 'lib/google/cloud/error_reporting/project.rb', line 121

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 StandardError => exception
  error_reporting.report_exception, service_name: "my_app_name",
                                    service_version: "v8"
end

Parameters:

Yields:



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/google/cloud/error_reporting/project.rb', line 148

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