Module: LogStruct::Integrations::ActionMailer::MetadataCollection

Extended by:
T::Sig
Defined in:
lib/log_struct/integrations/action_mailer/metadata_collection.rb

Overview

Handles collection of metadata for email logging

Class Method Summary collapse

Class Method Details

.add_context_metadata(mailer, log_data) ⇒ void

This method returns an undefined value.

Add context metadata to log data

Parameters:

  • mailer (T.untyped)
  • log_data (Hash{Symbol => T.untyped})


32
33
34
35
36
37
38
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 32

def self.(mailer, log_data)
  # Add account ID information if available (but not user email)
  extract_ids_to_log_data(mailer, log_data)

  # Add any current tags from ActiveJob or ActionMailer
  add_current_tags_to_log_data(log_data)
end

.add_current_tags_to_log_data(log_data) ⇒ void

This method returns an undefined value.

Parameters:

  • log_data (Hash{Symbol => T.untyped})


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 56

def self.add_current_tags_to_log_data(log_data)
  # Get current tags from ActiveSupport::TaggedLogging if available
  if ::ActiveSupport::TaggedLogging.respond_to?(:current_tags)
    tags = T.unsafe(::ActiveSupport::TaggedLogging).current_tags
    log_data[:tags] = tags if tags.present?
  end

  # Get request_id from ActionDispatch if available
  if ::ActionDispatch::Request.respond_to?(:current_request_id) &&
      T.unsafe(::ActionDispatch::Request).current_request_id.present?
    log_data[:request_id] = T.unsafe(::ActionDispatch::Request).current_request_id
  end

  # Get job_id from ActiveJob if available
  if defined?(::ActiveJob::Logging) && ::ActiveJob::Logging.respond_to?(:job_id) &&
      T.unsafe(::ActiveJob::Logging).job_id.present?
    log_data[:job_id] = T.unsafe(::ActiveJob::Logging).job_id
  end
end

.add_message_metadata(mailer, log_data) ⇒ void

This method returns an undefined value.

Add message-specific metadata to log data

Parameters:

  • mailer (T.untyped)
  • log_data (Hash{Symbol => T.untyped})


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 12

def self.(mailer, log_data)
  message = mailer.respond_to?(:message) ? mailer.message : nil

  # Add recipient count if message is available
  if message
    # Don't log actual email addresses
    log_data[:recipient_count] = [message.to, message.cc, message.bcc].flatten.compact.count

    # Handle case when attachments might be nil
    log_data[:has_attachments] = message.attachments&.any? || false
    log_data[:attachment_count] = message.attachments&.count || 0
  else
    log_data[:recipient_count] = 0
    log_data[:has_attachments] = false
    log_data[:attachment_count] = 0
  end
end

.extract_ids_to_log_data(mailer, log_data) ⇒ void

This method returns an undefined value.

Parameters:

  • mailer (T.untyped)
  • log_data (Hash{Symbol => T.untyped})


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 41

def self.extract_ids_to_log_data(mailer, log_data)
  # Extract account ID if available
  if mailer.instance_variable_defined?(:@account)
     = mailer.instance_variable_get(:@account)
    log_data[:account_id] = .id if .respond_to?(:id)
  end

  # Extract user ID if available
  return unless mailer.instance_variable_defined?(:@user)

  user = mailer.instance_variable_get(:@user)
  log_data[:user_id] = user.id if user.respond_to?(:id)
end