Module: Beacon::Integrations::ActionMailer

Defined in:
lib/beacon/integrations/action_mailer.rb

Class Method Summary collapse

Class Method Details

.install(client: Beacon.client, config: Beacon.config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/beacon/integrations/action_mailer.rb', line 12

def self.install(client: Beacon.client, config: Beacon.config)
  unless defined?(::ActiveSupport::Notifications)
    warn "[beacon] ActionMailer integration requires ActiveSupport — skipping"
    return
  end

  ambient = config.ambient

  ::ActiveSupport::Notifications.subscribe("deliver.action_mailer") do |_name, _start, _finish, _id, payload|
    begin
      # Ambient: emit a mailer_delivery event for every delivery attempt.
      if ambient
        mailer = payload[:mailer] || "UnknownMailer"
        action = payload[:action] || "unknown"
        status = payload[:exception_object].nil? ? "success" : "failure"
        client.push({
          kind:          :ambient,
          name:          "mailer_delivery",
          created_at_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
          properties: {
            mailer: mailer,
            action: action,
            status: status,
          },
        })
      end

      # Error capture: only on failure.
      exception = payload[:exception_object]
      next unless exception

      frame       = (exception.backtrace || []).first.to_s
      fingerprint = Beacon::Fingerprint.compute(exception.class.name, frame)
      client.push({
        kind:          :error,
        name:          exception.class.name,
        created_at_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
        properties: {
          fingerprint:     fingerprint,
          message:         exception.message.to_s[0, 500],
          first_app_frame: frame,
          stack_trace:     exception.backtrace&.join("\n"),
        },
      })
    rescue => e
      warn "[beacon] action_mailer subscriber rescued #{e.class}: #{e.message}"
    end
  end
end