|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "json" |
| 4 | +require "fileutils" |
| 5 | + |
| 6 | +module Sentry |
| 7 | + # DebugTransport is a transport that logs events to a file for debugging purposes. |
| 8 | + # |
| 9 | + # It can optionally also send events to Sentry via HTTP transport if a real DSN |
| 10 | + # is provided. |
| 11 | + class DebugTransport < Transport |
| 12 | + attr_reader :log_file_path, :http_transport |
| 13 | + |
| 14 | + def initialize(configuration) |
| 15 | + super |
| 16 | + |
| 17 | + @log_file_path = configuration.sdk_debug_transport_log_file || default_log_file_path |
| 18 | + |
| 19 | + FileUtils.mkdir_p(File.dirname(@log_file_path)) |
| 20 | + |
| 21 | + log_debug("DebugTransport: Initialized with log file: #{@log_file_path}") |
| 22 | + |
| 23 | + if configuration.dsn && !configuration.dsn.to_s.include?("localhost") |
| 24 | + @http_transport = Sentry::HTTPTransport.new(configuration) |
| 25 | + log_debug("DebugTransport: Initialized with HTTP transport for DSN: #{configuration.dsn}") |
| 26 | + else |
| 27 | + @http_transport = nil |
| 28 | + log_debug("DebugTransport: Using local-only mode for DSN: #{configuration.dsn}") |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def send_event(event) |
| 33 | + envelope = envelope_from_event(event) |
| 34 | + send_envelope(envelope) |
| 35 | + event |
| 36 | + end |
| 37 | + |
| 38 | + def send_envelope(envelope) |
| 39 | + envelope_data = { |
| 40 | + timestamp: Time.now.utc.iso8601, |
| 41 | + envelope_headers: envelope.headers, |
| 42 | + items: envelope.items.map do |item| |
| 43 | + { |
| 44 | + headers: item.headers, |
| 45 | + payload: item.payload |
| 46 | + } |
| 47 | + end |
| 48 | + } |
| 49 | + |
| 50 | + File.open(log_file_path, "a") do |file| |
| 51 | + file << JSON.dump(envelope_data) << "\n" |
| 52 | + end |
| 53 | + |
| 54 | + if http_transport |
| 55 | + http_transport.send_envelope(envelope) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def events |
| 60 | + return [] unless File.exist?(log_file_path) |
| 61 | + |
| 62 | + File.readlines(log_file_path).map do |line| |
| 63 | + JSON.load(line) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def clear |
| 68 | + File.write(log_file_path, "") |
| 69 | + log_debug("DebugTransport: Cleared events from #{log_file_path}") |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def default_log_file_path |
| 75 | + if defined?(Rails) && Rails.respond_to?(:root) && Rails.root |
| 76 | + File.join(Rails.root, "log", "sentry_debug_events.log") |
| 77 | + elsif File.directory?("log") |
| 78 | + File.join("log", "sentry_debug_events.log") |
| 79 | + else |
| 80 | + "/tmp/sentry_debug_events.json" |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments