Sha256: 636d330b54f8c7e7af06da0e5705a9397baf4aa7342f6f877f6decad88bdfc1d

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require 'net/http'
require 'uri'

module Synchrolog
  module ExceptionLogger
    class HTTP
      def initialize(api_key, **args)
        @api_key = api_key
        @host = args[:host]
      end
      
      def capture(response, exception, env, anonymous_id, user_id)
        return unless anonymous_id
        json_headers = {'Authorization' => "Basic #{@api_key}", 'Content-Type' =>'application/json'}
        uri = URI.parse("#{@host}/v1/track-backend-error")
        http = Net::HTTP.new(uri.host, uri.port)
        http.post(uri.path, body(response, exception, env, anonymous_id, user_id).to_json, json_headers)
      end

      def body(response, exception, env, anonymous_id, user_id)
        status, headers, body = *response
        return {
          event_type: 'error', 
          timestamp: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ"),
          anonymous_id: anonymous_id,
          user_id: user_id,
          source: 'backend',
          api_key: @api_key,
          error: {
            status: status.to_s,
            description: exception.to_s,
            backtrace: exception.backtrace,
            ip_address: env['REMOTE_ADDR'],
            user_agent: env['HTTP_USER_AGENT']
          }
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
synchrolog-ruby-0.1.5 lib/synchrolog/exception_logger/http.rb
synchrolog-ruby-0.1.4 lib/synchrolog/exception_logger/http.rb