Class: TraceForge::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/traceforge/client.rb

Class Method Summary collapse

Class Method Details

.capture_exception(exception, metadata = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/traceforge/client.rb', line 8

def self.capture_exception(exception,  = {})
  return unless TraceForge.configuration.api_key

  payload = {
    type: [:type] || 'exception',
    message: exception.message,
    stackTrace: exception.backtrace&.join("\n") || '',
    file: exception.backtrace&.first&.split(':')&.first || '',
    line: exception.backtrace&.first&.split(':')&.fetch(1, 0)&.to_i || 0,
    metadata: {
      framework: 'ruby-on-rails',
      language: 'ruby'
    }.merge()
  }

  send_payload(payload)
end

.send_payload(payload) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/traceforge/client.rb', line 28

def self.send_payload(payload)
  Thread.new do
    begin
      uri = URI.parse(TraceForge.configuration.ingest_url)
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = (uri.scheme == 'https')
      http.open_timeout = 1
      http.read_timeout = 1

      request = Net::HTTP::Post.new(uri.request_uri)
      request['X-Traceforge-Key'] = TraceForge.configuration.api_key
      request['Content-Type'] = 'application/json'
      request.body = payload.to_json

      http.request(request)
    rescue StandardError => e
      # Fail silently to not crash the host application
    end
  end
end