Class: Profiler::Collectors::MailerCollector

Inherits:
BaseCollector show all
Defined in:
lib/profiler/collectors/mailer_collector.rb

Constant Summary collapse

MAX_EMAILS =
50
MAX_BODY_SIZE =

100 KB

100 * 1024

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, inherited, #name, #panel_content, #render_html, #render_mode

Constructor Details

#initialize(profile) ⇒ MailerCollector

Returns a new instance of MailerCollector.



11
12
13
14
15
16
17
18
# File 'lib/profiler/collectors/mailer_collector.rb', line 11

def initialize(profile)
  super
  @emails = []
  @errors = []
  @queued = []
  @loop_warnings = []
  @subscriptions = []
end

Instance Method Details

#collectObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/profiler/collectors/mailer_collector.rb', line 86

def collect
  # Any remaining pending process entries had no matching deliver event:
  # they were enqueued via deliver_later in the HTTP context.
  pending = Thread.current[:profiler_pending_processes] || []
  Thread.current[:profiler_pending_processes] = nil

  pending.each do |info|
    @queued << {
      mailer_class: info[:mailer_class],
      action: info[:action],
      delivery_mode: "deliver_later",
      delivery_method: extract_delivery_method,
      duration_ms: info[:duration_ms],
      assigns: info[:assigns],
      body_captured: false,
      triggered_at: Time.now.utc.iso8601(3)
    }
  end

  @subscriptions.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }

  detect_loops

  truncated = @emails.size > MAX_EMAILS
  emails = @emails.first(MAX_EMAILS)

  store_data(
    emails: emails.map { |e| e.transform_keys(&:to_s) },
    errors: @errors.map { |e| e.transform_keys(&:to_s) },
    queued: @queued.map { |e| e.transform_keys(&:to_s) },
    loop_warnings: @loop_warnings.map { |w| w.transform_keys(&:to_s) },
    total: @emails.size + @errors.size,
    queued_count: @queued.size,
    deliver_now: @emails.count { |e| e[:delivery_mode] == "deliver_now" },
    deliver_later: @emails.count { |e| e[:delivery_mode] == "deliver_later" },
    multi_part_count: @emails.count { |e| e[:parts]&.size.to_i > 1 },
    failed: @errors.size,
    truncated: truncated
  )
end

#has_data?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/profiler/collectors/mailer_collector.rb', line 127

def has_data?
  @emails.any? || @errors.any? || @queued.any?
end

#iconObject



20
21
22
# File 'lib/profiler/collectors/mailer_collector.rb', line 20

def icon
  "✉️"
end

#priorityObject



24
25
26
# File 'lib/profiler/collectors/mailer_collector.rb', line 24

def priority
  45
end

#subscribeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/profiler/collectors/mailer_collector.rb', line 39

def subscribe
  return unless defined?(ActiveSupport::Notifications)
  return unless Profiler.configuration.track_mailers

  # Capture the subscriber thread so notifications from other threads (e.g. async
  # job threads delivering mail enqueued via deliver_later) are ignored by this collector.
  @subscriber_thread = Thread.current

  # Use a stack so multiple deliver_later calls in the same request are all tracked.
  Thread.current[:profiler_pending_processes] ||= []

  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("process.action_mailer") do |_name, started, finished, _id, payload|
    next unless Thread.current.equal?(@subscriber_thread)
    next if rails_preview_request?

    mailer_class = payload[:mailer].to_s
    action = payload[:action].to_s
    (Thread.current[:profiler_pending_processes] ||= []) << {
      mailer_class: mailer_class,
      action: action,
      duration_ms: ((finished - started) * 1000).round(2),
      assigns: extract_assigns(mailer_class, action, payload[:args])
    }
  end

  @subscriptions << ActiveSupport::Notifications.monotonic_subscribe("deliver.action_mailer") do |_name, started, finished, _id, payload|
    next unless Thread.current.equal?(@subscriber_thread)
    next unless payload[:perform_deliveries]
    next if rails_preview_request?

    delivery_ms = ((finished - started) * 1000).round(2)
    process_info = (Thread.current[:profiler_pending_processes] ||= []).pop || {}
    mail = payload[:mail]

    mailer_class = process_info[:mailer_class] || payload[:mailer_class].to_s
    action = process_info[:action].to_s
    config = Profiler.configuration
    next if config.mailer_skip_actions.any? { |a| a == "#{mailer_class}##{action}" || a == mailer_class }

    delivery_mode = mail_delivery_job? ? "deliver_later" : "deliver_now"
    email = build_email_record(payload, mail, process_info, delivery_ms, delivery_mode)
    email[:error] ? @errors << email : @emails << email
  rescue StandardError => e
    @errors << { error: e.message, triggered_at: Time.now.utc.iso8601(3) }
  end
end

#tab_configObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/profiler/collectors/mailer_collector.rb', line 28

def tab_config
  {
    key: "mailer",
    label: "Mailers",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/profiler/collectors/mailer_collector.rb', line 131

def toolbar_summary
  total = @emails.size + @errors.size
  queued = @queued.size
  return { text: "0 emails", color: "gray" } if total == 0 && queued == 0

  now_count = @emails.count { |e| e[:delivery_mode] == "deliver_now" }
  later_count = @emails.count { |e| e[:delivery_mode] == "deliver_later" }
  has_errors = @errors.any? || @loop_warnings.any?

  parts = []
  parts << "#{now_count} now" if now_count > 0
  parts << "#{later_count} later" if later_count > 0
  parts << "#{queued} queued" if queued > 0
  detail = parts.any? ? " (#{parts.join(" · ")})" : ""

  text = "#{total + queued} email#{(total + queued) > 1 ? "s" : ""}#{detail}"
  text += " ⚠️ #{@errors.size} error#{@errors.size > 1 ? "s" : ""}" if @errors.any?

  { text: text, color: has_errors ? "red" : "green" }
end