Module: LogStruct::Integrations::ActiveStorage

Extended by:
IntegrationInterface, T::Sig
Defined in:
lib/log_struct/integrations/active_storage.rb

Overview

Integration for ActiveStorage structured logging

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.process_active_storage_event(event, config) ⇒ void

This method returns an undefined value.

Process ActiveStorage events and create structured logs

Parameters:



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/log_struct/integrations/active_storage.rb', line 34

def self.process_active_storage_event(event, config)
  return unless config.enabled
  return unless config.integrations.enable_activestorage

  # Extract key information from the event
  event_name = event.name.sub(/\.active_storage$/, "")
  service_name = event.payload[:service]
  duration = event.duration

  # Map service events to log event types
  event_type = case event_name
  when "service_upload"
    Event::Upload
  when "service_download"
    Event::Download
  when "service_delete"
    Event::Delete
  when "service_delete_prefixed"
    Event::Delete
  when "service_exist"
    Event::Exist
  when "service_url"
    Event::Url
  when "service_download_chunk"
    Event::Download
  when "service_stream"
    Event::Stream
  when "service_update_metadata"
    Event::Metadata
  else
    Event::Unknown
  end

  # Map the event name to an operation
  operation = event_name.sub(/^service_/, "").to_sym

  # Create structured log event specific to ActiveStorage
  log_data = Log::ActiveStorage.new(
    event: event_type,
    operation: operation,
    storage: service_name.to_s,
    file_id: event.payload[:key].to_s,
    checksum: event.payload[:checksum].to_s,
    duration: duration,
    # Add other fields where available
    metadata: event.payload[:metadata],
    exist: event.payload[:exist],
    url: event.payload[:url],
    filename: event.payload[:filename],
    mime_type: event.payload[:content_type],
    size: event.payload[:byte_size],
    prefix: event.payload[:prefix],
    range: event.payload[:range]
  )

  # Log structured data
  LogStruct.info(log_data)
end

.setup(config) ⇒ Boolean?

Set up ActiveStorage structured logging

Parameters:

Returns:

  • (Boolean, nil)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/log_struct/integrations/active_storage.rb', line 17

def self.setup(config)
  return nil unless defined?(::ActiveStorage)
  return nil unless config.enabled
  return nil unless config.integrations.enable_activestorage

  # Subscribe to all ActiveStorage service events
  ::ActiveSupport::Notifications.subscribe(/service_.*\.active_storage/) do |*args|
    process_active_storage_event(::ActiveSupport::Notifications::Event.new(*args), config)
  end

  true
end