Module: LogStruct::Integrations::Shrine

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

Overview

Shrine integration for structured logging

Constant Summary collapse

SHRINE_EVENTS =
T.let(%i[upload exists download delete metadata open].freeze, T::Array[Symbol])

Class Method Summary collapse

Methods included from IntegrationInterface

setup

Class Method Details

.instrumentation_already_configured?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/log_struct/integrations/shrine.rb', line 106

def self.instrumentation_already_configured?
  return false unless defined?(::Shrine)

  opts = T.unsafe(::Shrine).opts
  return false unless opts.is_a?(Hash)

  instrumentation_opts = opts[:instrumentation]
  return false unless instrumentation_opts.is_a?(Hash)

  subscribers = instrumentation_opts[:subscribers]
  return false unless subscribers.is_a?(Hash)

  !subscribers.empty?
end

.replace_existing_subscribers(new_subscriber) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/log_struct/integrations/shrine.rb', line 122

def self.replace_existing_subscribers(new_subscriber)
  opts = T.unsafe(::Shrine).opts
  instrumentation_opts = opts[:instrumentation]
  subscribers = instrumentation_opts[:subscribers]

  # Clear all existing subscribers and add our new one
  SHRINE_EVENTS.each do |event_name|
    # Clear existing subscribers for this event
    subscribers[event_name] = [] if subscribers[event_name]

    # Add our subscriber
    subscribers[event_name] ||= []
    subscribers[event_name] << new_subscriber

    # Also re-subscribe via ActiveSupport::Notifications
    # Shrine uses "shrine.#{event_name}" as the notification name
    notification_name = "shrine.#{event_name}"

    # Unsubscribe existing listeners for this event
    # ActiveSupport::Notifications stores subscriptions, we need to find and remove them
    notifier = ::ActiveSupport::Notifications.notifier
    if notifier.respond_to?(:listeners_for)
      # Rails 7.0+ uses listeners_for
      listeners = notifier.listeners_for(notification_name)
      listeners.each do |listener|
        ::ActiveSupport::Notifications.unsubscribe(listener)
      end
    end

    # Subscribe our new subscriber
    ::ActiveSupport::Notifications.subscribe(notification_name) do |*args|
      event = ::ActiveSupport::Notifications::Event.new(*args)
      new_subscriber.call(event)
    end
  end
end

.setup(config) ⇒ Object



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
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
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/log_struct/integrations/shrine.rb', line 21

def self.setup(config)
  return nil unless defined?(::Shrine)
  return nil unless config.enabled
  return nil unless config.integrations.enable_shrine

  # Create a structured log subscriber for Shrine
  # ActiveSupport::Notifications::Event has name, time, end, transaction_id, payload, and duration
  shrine_log_subscriber = T.unsafe(lambda do |event|
    payload = event.payload.except(:io, :metadata, :name).dup

    # Map event name to Event type
    event_type = case event.name
    when :upload then Event::Upload
    when :download then Event::Download
    when :open then Event::Download
    when :delete then Event::Delete
    when :metadata then Event::Metadata
    when :exists then Event::Exist
    else Event::Unknown
    end

    # Create structured log data
    # Ensure storage is always a symbol
    storage_sym = payload[:storage].to_sym

    log_data = case event_type
    when Event::Upload
      Log::Shrine::Upload.new(
        storage: storage_sym,
        location: payload[:location],
        uploader: payload[:uploader]&.to_s,
        upload_options: payload[:upload_options],
        options: payload[:options],
        duration_ms: event.duration.to_f
      )
    when Event::Download
      Log::Shrine::Download.new(
        storage: storage_sym,
        location: payload[:location],
        download_options: payload[:download_options]
      )
    when Event::Delete
      Log::Shrine::Delete.new(
        storage: storage_sym,
        location: payload[:location]
      )
    when Event::Metadata
       = {
        storage: storage_sym,
        metadata: payload[:metadata]
      }
      [:location] = payload[:location] if payload[:location]
      Log::Shrine::Metadata.new(**)
    when Event::Exist
      Log::Shrine::Exist.new(
        storage: storage_sym,
        location: payload[:location],
        exist: payload[:exist]
      )
    else
      unknown_params = {storage: storage_sym, metadata: payload[:metadata]}
      unknown_params[:location] = payload[:location] if payload[:location]
      Log::Shrine::Metadata.new(**unknown_params)
    end

    # Log directly through SemanticLogger, NOT through Shrine.logger
    # Shrine.logger is a basic Logger that would just call .to_s on the struct
    ::SemanticLogger[::Shrine].info(log_data)
  end)

  # Check if instrumentation plugin is already loaded
  # If so, we need to replace the existing subscribers, not add duplicates
  if instrumentation_already_configured?
    replace_existing_subscribers(shrine_log_subscriber)
  else
    # First time setup - configure the instrumentation plugin
    ::Shrine.plugin :instrumentation,
      events: SHRINE_EVENTS,
      log_subscriber: shrine_log_subscriber
  end

  true
end