class Google::Cloud::ErrorReporting::ErrorEvent

# ErrorEvent

An individual error event to report to Stackdriver Error Reporting service.

Google::Cloud::ErrorReporting::ErrorEvent is able to be transformed into the `Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent` gRPC structure. Once an error event is reported, the GCP Stackdriver ErrorReporting service is able to parse the message and backtrace, then group the error events by content.

@see cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events

@example

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",
                                          service_version: "v8"
error_reporting.report error_event

Attributes

event_time[RW]

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

file_path[RW]

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

function_name[RW]

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.

http_method[RW]

The type of HTTP request, such as GET, POST, etc.

http_referrer[RW]

The referrer information that is provided with the request.

http_remote_ip[RW]

The IP address from which the request originated. This can be IPv4, IPv6, or a token which is derived from the IP address, depending on the data that has been provided in the error report.

http_status[RW]

The HTTP response status code for the request.

http_url[RW]

The URL of the request.

http_user_agent[RW]

The user agent information that is provided with the request.

line_number[RW]

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

message[RW]

A message describing the error. The message can contain an exception stack in one of the supported programming languages and formats. In that case, the message is parsed and detailed exception information is returned when retrieving the error event again.

service_name[RW]

An identifier of the service, such as the name of the executable, job, or Google App Engine service name. This field is expected to have a low number of values that are relatively stable over time, as opposed to version, which can be changed whenever new code is deployed.

service_version[RW]

Represents the source code version that the developer provided, which could represent a version label or a Git SHA-1 hash, for example.

user[RW]

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. See affectedUsersCount in ErrorGroupStats.

Public Class Methods

from_exception(exception) click to toggle source

Construct an ErrorEvent object based on a given exception.

@param [Exception] exception A Ruby exception.

@return [ErrorEvent] An ErrorEvent object containing information

from the given exception.
# File lib/google/cloud/error_reporting/error_event.rb, line 210
def self.from_exception exception
  backtrace = exception.backtrace
  message = "#{exception.class}: #{exception.message}"

  if !backtrace.nil? && !backtrace.empty?
    message = "#{message}\n\t" + backtrace.join("\n\t")

    file_path, line_number, function_name = backtrace.first.split ":"
    function_name = function_name.to_s[/`(.*)'/, 1]
  end

  new.tap do |e|
    e.message = message
    e.file_path = file_path
    e.line_number = line_number.to_i
    e.function_name = function_name
  end
end
from_grpc(grpc) click to toggle source

Build a new ErrorEvent from a `Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent` object.

@param [Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent]

grpc A `Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent`
object

@return [ErrorEvent] A new ErrorEvent instance derived from given grpc

object
# File lib/google/cloud/error_reporting/error_event.rb, line 130
def self.from_grpc grpc
  return new if grpc.nil?
  new.tap do |event|
    event.event_time = extract_timestamp grpc.event_time
    event.message = grpc.message

    extract_service_context event, grpc.service_context
    extract_error_context event, grpc.context
  end
end

Private Class Methods

extract_error_context(error_event, error_context_grpc) click to toggle source

@private Extract error context info from gRPC into an ErrorEvent.

# File lib/google/cloud/error_reporting/error_event.rb, line 165
def self.extract_error_context error_event, error_context_grpc
  return nil if error_context_grpc.nil?

  error_event.user = error_context_grpc.user
  extract_http_request error_event, error_context_grpc.http_request
  extract_source_location error_event,
                          error_context_grpc.report_location
end
extract_http_request(error_event, http_request_grpc) click to toggle source

@private Extract http request info from gRPC into an ErrorEvent.

# File lib/google/cloud/error_reporting/error_event.rb, line 176
def self.extract_http_request error_event, http_request_grpc
  return nil if http_request_grpc.nil?

  error_event.http_method = http_request_grpc["method"]
  error_event.http_url = http_request_grpc.url
  error_event.http_user_agent = http_request_grpc.user_agent
  error_event.http_referrer = http_request_grpc.referrer
  error_event.http_status = http_request_grpc.response_status_code
  error_event.http_remote_ip = http_request_grpc.remote_ip
end
extract_service_context(error_event, service_context_grpc) click to toggle source

@private Extract service context info from gRPC into an ErrorEvent.

# File lib/google/cloud/error_reporting/error_event.rb, line 156
def self.extract_service_context error_event, service_context_grpc
  return nil if service_context_grpc.nil?

  error_event.service_name = service_context_grpc.service
  error_event.service_version = service_context_grpc.version
end
extract_source_location(error_event, source_location_grpc) click to toggle source

@private Extract source location info from gRPC into an ErrorEvent.

# File lib/google/cloud/error_reporting/error_event.rb, line 189
def self.extract_source_location error_event, source_location_grpc
  return nil if source_location_grpc.nil?

  error_event.file_path = source_location_grpc.file_path
  error_event.line_number = source_location_grpc.line_number
  error_event.function_name = source_location_grpc.function_name
end
extract_timestamp(timestamp_grpc) click to toggle source

@private Get a Time object from a Google::Protobuf::Timestamp object.

@param [Google::Protobuf::Timestamp] timestamp_grpc A protobuf

Timestamp object

@return [Time] The time object derived from input grpc timestamp

# File lib/google/cloud/error_reporting/error_event.rb, line 149
def self.extract_timestamp timestamp_grpc
  return nil if timestamp_grpc.nil?
  Time.at timestamp_grpc.seconds, Rational(timestamp_grpc.nanos, 1000)
end

Public Instance Methods

to_grpc() click to toggle source

Convert ErrorEvent object to gRPC struct.

@return [Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent]

gRPC struct that represent an ErrorEvent.
# File lib/google/cloud/error_reporting/error_event.rb, line 234
def to_grpc
  Google::Cloud::ErrorReporting::V1beta1::ReportedErrorEvent.new(
    event_time: event_time_grpc,
    message: message.to_s,
    service_context: service_context_grpc,
    context: error_context_grpc
  )
end

Private Instance Methods

error_context_grpc() click to toggle source

@private Formats the error context info as a Google::Cloud::ErrorReporting::V1beta1::ErrorContext

# File lib/google/cloud/error_reporting/error_event.rb, line 302
def error_context_grpc
  http_request = http_request_grpc
  source_location = source_location_grpc
  return nil if !http_request && !source_location && !user
  Google::Cloud::ErrorReporting::V1beta1::ErrorContext.new(
    http_request: http_request,
    user: user.to_s,
    report_location: source_location
  )
end
event_time_grpc() click to toggle source

@private Formats the event_time as a Google::Protobuf::Timestamp.

# File lib/google/cloud/error_reporting/error_event.rb, line 248
def event_time_grpc
  return nil if event_time.nil?
  Google::Protobuf::Timestamp.new(
    seconds: event_time.to_i,
    nanos: event_time.nsec
  )
end
http_request_grpc() click to toggle source

@private Formats the http request context as a Google::Cloud::ErrorReporting::V1beta1::HttpRequestContext

# File lib/google/cloud/error_reporting/error_event.rb, line 272
def http_request_grpc
  return nil if !http_method && !http_url && !http_user_agent &&
                !http_referrer && !http_status && !http_remote_ip
  Google::Cloud::ErrorReporting::V1beta1::HttpRequestContext.new(
    method: http_method.to_s,
    url: http_url.to_s,
    user_agent: http_user_agent.to_s,
    referrer: http_referrer.to_s,
    response_status_code: http_status.to_i,
    remote_ip: http_remote_ip.to_s
  )
end
service_context_grpc() click to toggle source

@private Formats the service_name and service_version as a Google::Cloud::ErrorReporting::V1beta1::ServiceContext.

# File lib/google/cloud/error_reporting/error_event.rb, line 260
def service_context_grpc
  return nil if !service_name && !service_version
  Google::Cloud::ErrorReporting::V1beta1::ServiceContext.new(
    service: service_name.to_s,
    version: service_version.to_s
  )
end
source_location_grpc() click to toggle source

@private Formats the source location as a Google::Cloud::ErrorReporting::V1beta1::SourceLocation

# File lib/google/cloud/error_reporting/error_event.rb, line 289
def source_location_grpc
  return nil if !file_path && !line_number && !function_name
  Google::Cloud::ErrorReporting::V1beta1::SourceLocation.new(
    file_path: file_path.to_s,
    line_number: line_number.to_i,
    function_name: function_name.to_s
  )
end