Class: GoogleCloudRun::ErrorReportingEntry

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/google_cloud_run/entry.rb

Overview

Constant Summary

Constants included from Logger::Severity

Logger::Severity::G_ALERT, Logger::Severity::G_CRITICAL, Logger::Severity::G_DEBUG, Logger::Severity::G_DEFAULT, Logger::Severity::G_EMERGENCY, Logger::Severity::G_ERROR, Logger::Severity::G_INFO, Logger::Severity::G_NOTICE, Logger::Severity::G_WARNING

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrorReportingEntry

Returns a new instance of ErrorReportingEntry.



76
77
78
79
80
# File 'lib/google_cloud_run/entry.rb', line 76

def initialize
  @severity = G_CRITICAL
  @timestamp = Time.now.utc
  @insert_id = SecureRandom.uuid
end

Instance Attribute Details

#context_serviceObject

Returns the value of attribute context_service.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def context_service
  @context_service
end

#context_versionObject

Returns the value of attribute context_version.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def context_version
  @context_version
end

#exceptionObject

Returns the value of attribute exception.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def exception
  @exception
end

#labelsObject

Returns the value of attribute labels.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def labels
  @labels
end

#location_lineObject

Returns the value of attribute location_line.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def location_line
  @location_line
end

#location_methodObject

Returns the value of attribute location_method.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def location_method
  @location_method
end

#location_pathObject

Returns the value of attribute location_path.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def location_path
  @location_path
end

#messageObject

Returns the value of attribute message.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def message
  @message
end

#project_idObject

Returns the value of attribute project_id.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def project_id
  @project_id
end

#requestObject

Returns the value of attribute request.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def request
  @request
end

#severityObject

Returns the value of attribute severity.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def severity
  @severity
end

#timestampObject

Returns the value of attribute timestamp.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def timestamp
  @timestamp
end

#userObject

Returns the value of attribute user.



65
66
67
# File 'lib/google_cloud_run/entry.rb', line 65

def user
  @user
end

Instance Method Details

#to_jsonObject



82
83
84
85
86
87
88
89
90
91
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/google_cloud_run/entry.rb', line 82

def to_json
  raise "labels must be hash" if !@labels.blank? && !@labels.is_a?(Hash)

  j = {}

  j["@type"] = "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"
  j["logging.googleapis.com/insertId"] = @insert_id
  j["severity"] = Severity.to_s(Severity.mapping(@severity))
  j["eventTime"] = @timestamp.strftime("%FT%T.%9NZ")
  j["logging.googleapis.com/labels"] = @labels unless @labels.blank?

  if @context_service || @context_version
    j["serviceContext"] = {}
    j["serviceContext"]["service"] = @context_service.to_s
    j["serviceContext"]["version"] = @context_version.to_s
  end

  if @request
    # https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
    j["httpRequest"] = {}
    j["httpRequest"]["requestMethod"] = @request&.method.to_s
    j["httpRequest"]["requestUrl"] = @request&.url.to_s
    j["httpRequest"]["userAgent"] = @request&.headers["user-agent"].to_s unless @request&.headers["user-agent"].blank?
    j["httpRequest"]["remoteIp"] = @request&.remote_ip.to_s
    j["httpRequest"]["referer"] = @request&.headers["referer"].to_s unless @request&.headers["referer"].blank?

    trace, span, sample = GoogleCloudRun.parse_trace_context(@request&.headers["X-Cloud-Trace-Context"])
    j["logging.googleapis.com/trace"] = "projects/#{@project_id}/traces/#{trace}" unless trace.blank?
    j["logging.googleapis.com/spanId"] = span unless span.blank?
    j["logging.googleapis.com/trace_sampled"] = sample unless sample.nil?
  end

  if @exception
    j["message"] = @exception.class.to_s

    e_message = @exception&.message.to_s.strip
    unless e_message.blank?
      j["message"] << ": " + e_message + "\n"
    end

    j["message"] << @exception&.backtrace.join("\n")
  else
    j["message"] = @message.is_a?(String) ? @message.strip : @message.inspect
  end

  j["context"] = {}

  if @request
    # https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
    j["context"]["httpRequest"] = {}
    j["context"]["httpRequest"]["method"] = @request&.method
    j["context"]["httpRequest"]["url"] = @request&.url
    j["context"]["httpRequest"]["userAgent"] = @request&.headers["user-agent"] unless @request&.headers["user-agent"].blank?
    j["context"]["httpRequest"]["remoteIp"] = @request&.remote_ip
    j["context"]["httpRequest"]["referrer"] = @request&.headers["referer"] unless @request&.headers["referer"].blank?
  end

  if @user
    j["context"]["user"] = @user
  end

  if @location_path || @location_line || @location_method
    j["context"]["reportLocation"] = {}
    j["context"]["reportLocation"]["filePath"] = @location_path.to_s
    j["context"]["reportLocation"]["lineNumber"] = @location_line.to_i
    j["context"]["reportLocation"]["functionName"] = @location_method.to_s
  end

  j.to_json
end