require "net/http"
require "json"
module Squasher
ENDPOINT = URI("https://ingest.squasher.ai/v1/ingest/#{ENV['SQUASHER_PROJECT_ID']}")
API_KEY = ENV["SQUASHER_API_KEY"]
def self.capture_error(error, tags: {})
payload = {
message: error.message,
type: error.class.name,
level: "error",
stack: error.backtrace&.join("\n"),
environment: ENV.fetch("RAILS_ENV", "production"),
tags: tags.transform_values(&:to_s),
}
Thread.new do
req = Net::HTTP::Post.new(ENDPOINT)
req["Content-Type"] = "application/json"
req["x-squasher-key"] = API_KEY
req.body = payload.to_json
Net::HTTP.start(ENDPOINT.hostname, ENDPOINT.port, use_ssl: true) do |http|
http.request(req)
end
rescue StandardError
# Silently fail
end
end
def self.log(level, message, tags: {})
payload = {
message: message,
level: level.to_s,
tags: tags.transform_values(&:to_s),
}
Thread.new do
req = Net::HTTP::Post.new(ENDPOINT)
req["Content-Type"] = "application/json"
req["x-squasher-key"] = API_KEY
req.body = payload.to_json
Net::HTTP.start(ENDPOINT.hostname, ENDPOINT.port, use_ssl: true) do |http|
http.request(req)
end
rescue StandardError
# Silently fail
end
end
end