Module: RailsNexus::Breadcrumbs

Defined in:
lib/rails_nexus/breadcrumbs.rb

Overview

Breadcrumbs capture activity trail before a crash. Uses ActiveSupport::Notifications to subscribe to SQL, controller, cache, and other instrumentation events.

Constant Summary collapse

RING_BUFFER_SIZE =
50

Class Method Summary collapse

Class Method Details

.add(type:, name:, payload: nil, duration: nil) ⇒ Object

Capture a breadcrumb manually



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_nexus/breadcrumbs.rb', line 23

def add(type:, name:, payload: nil, duration: nil)
  buffer << {
    type: type,
    name: name,
    payload: payload,
    duration: duration,
    timestamp: Time.current.iso8601(3)
  }
  # Keep only last N entries
  buffer.shift if buffer.size > RING_BUFFER_SIZE
end

.bufferObject

Thread-local ring buffer of breadcrumbs



14
15
16
# File 'lib/rails_nexus/breadcrumbs.rb', line 14

def buffer
  Thread.current[:rails_nexus_breadcrumbs] ||= []
end

.buffer=(val) ⇒ Object



18
19
20
# File 'lib/rails_nexus/breadcrumbs.rb', line 18

def buffer=(val)
  Thread.current[:rails_nexus_breadcrumbs] = val
end

.flushObject

Flush buffer and return captured breadcrumbs



36
37
38
39
40
# File 'lib/rails_nexus/breadcrumbs.rb', line 36

def flush
  crumbs = buffer.dup
  self.buffer = []
  crumbs
end

.reset!Object

Reset state (for testing)



155
156
157
158
# File 'lib/rails_nexus/breadcrumbs.rb', line 155

def reset!
  self.buffer = []
  @subscribed = false
end

.subscribe!Object

Start subscribing to instrumentation events



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
# File 'lib/rails_nexus/breadcrumbs.rb', line 43

def subscribe!
  return if @subscribed

  # SQL queries
  ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start, _finish, _id, payload|
    next if payload[:name] == "SCHEMA"
    next if payload[:sql]&.start_with?("SET")
    next if payload[:sql]&.start_with?("SELECT 1")

    add(
      type: "sql",
      name: payload[:sql]&.truncate(200),
      payload: {
        connection_id: payload[:connection_id],
        cached: payload[:cached]
      },
      duration: payload[:runtime]
    )
  end

  # Controller action
  ActiveSupport::Notifications.subscribe("process_action.action_controller") do |_name, _start, _finish, _id, payload|
    add(
      type: "controller",
      name: "#{payload[:controller]}##{payload[:action]}",
      payload: {
        format: payload[:format],
        status: payload[:status],
        view_runtime: payload[:view_runtime]&.round(2),
        db_runtime: payload[:db_runtime]&.round(2)
      },
      duration: payload[:runtime]&.round(2)
    )
  end

  # Cache reads/writes
  ActiveSupport::Notifications.subscribe("cache_read.active_support") do |_name, _start, _finish, _id, payload|
    next unless payload[:hit]  # Only log cache hits

    add(
      type: "cache",
      name: "Cache read: #{payload[:key]}",
      payload: { hit: true, super: payload[:super] }
    )
  end

  ActiveSupport::Notifications.subscribe("cache_write.active_support") do |_name, _start, _finish, _id, payload|
    add(
      type: "cache",
      name: "Cache write: #{payload[:key]}",
      payload: { hit: false }
    )
  end

  # Active Job
  ActiveSupport::Notifications.subscribe("perform_start.active_job") do |_name, _start, _finish, _id, payload|
    add(
      type: "job",
      name: "Job started: #{payload[:job_class]}",
      payload: {
        job_id: payload[:job_id],
        queue: payload[:queue_name],
        arguments: payload[:arguments]&.first(200)
      }
    )
  end

  ActiveSupport::Notifications.subscribe("perform.active_job") do |_name, _start, _finish, _id, payload|
    add(
      type: "job",
      name: "Job completed: #{payload[:job_class]}",
      payload: {
        job_id: payload[:job_id],
        queue: payload[:queue_name],
        error: payload[:exception_object]&.message
      },
      duration: payload[:runtime]&.round(2)
    )
  end

  # Action Mailer
  ActiveSupport::Notifications.subscribe("deliver.action_mailer") do |_name, _start, _finish, _id, payload|
    add(
      type: "mailer",
      name: "Mail delivered: #{payload[:mailer]}##{payload[:message_id]}",
      payload: {
        mailer: payload[:mailer],
        message_id: payload[:message_id]
      }
    )
  end

  # Render partials
  ActiveSupport::Notifications.subscribe("render_partial.action_view") do |_name, _start, _finish, _id, payload|
    add(
      type: "render",
      name: "Render: #{payload[:identifier]}",
      payload: {
        cache_hits: payload[:cache_hit]
      },
      duration: payload[:duration]&.round(2)
    )
  end

  @subscribed = true
end

.subscribed?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/rails_nexus/breadcrumbs.rb', line 150

def subscribed?
  @subscribed
end