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})


25
26
27
28
29
30
31
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 25

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})


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 48

def self.add_current_tags_to_log_data(log_data)
  # Get current tags from thread-local storage or ActiveSupport::TaggedLogging
  tags = if ::ActiveSupport::TaggedLogging.respond_to?(:current_tags)
    T.unsafe(::ActiveSupport::TaggedLogging).current_tags
  else
    Thread.current[:activesupport_tagged_logging_tags] || []
  end
  log_data[:tags] = tags if tags.present?

  # 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
# 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 attachment count if message is available
  log_data[:attachment_count] = if message
    message.attachments&.count || 0
  else
    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})


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/log_struct/integrations/action_mailer/metadata_collection.rb', line 34

def self.extract_ids_to_log_data(mailer, log_data)
  # Use configured ID mapping from LogStruct configuration
  id_mapping = LogStruct.config.integrations.actionmailer_id_mapping

  id_mapping.each do |ivar_name, log_key|
    ivar = :"@#{ivar_name}"
    next unless mailer.instance_variable_defined?(ivar)

    obj = mailer.instance_variable_get(ivar)
    log_data[log_key] = obj.id if obj.respond_to?(:id)
  end
end