rails_error_dashboard 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/app/jobs/rails_error_dashboard/async_error_logging_job.rb +10 -0
  3. data/app/jobs/rails_error_dashboard/retention_cleanup_job.rb +54 -0
  4. data/app/jobs/rails_error_dashboard/storm_flush_job.rb +7 -4
  5. data/app/models/rails_error_dashboard/error_log.rb +10 -0
  6. data/app/models/rails_error_dashboard/event_count.rb +132 -0
  7. data/app/models/rails_error_dashboard/event_timing_gap.rb +55 -0
  8. data/app/views/layouts/rails_error_dashboard.html.erb +47 -2
  9. data/app/views/rails_error_dashboard/errors/_request_context.html.erb +2 -0
  10. data/app/views/rails_error_dashboard/errors/overview.html.erb +12 -0
  11. data/app/views/rails_error_dashboard/errors/show.html.erb +3 -3
  12. data/config/locales/de.yml +2 -0
  13. data/config/locales/en.yml +2 -0
  14. data/config/locales/es.yml +2 -0
  15. data/config/locales/fr.yml +2 -0
  16. data/config/locales/it.yml +2 -0
  17. data/config/locales/ja.yml +2 -0
  18. data/config/locales/pl.yml +2 -0
  19. data/config/locales/pt-BR.yml +2 -0
  20. data/config/locales/ru.yml +2 -0
  21. data/config/locales/uk.yml +2 -0
  22. data/config/locales/zh-CN.yml +2 -0
  23. data/db/migrate/20260919000001_create_event_counts.rb +71 -0
  24. data/db/migrate/20260920000001_add_buckets_incomplete_to_storm_events.rb +25 -0
  25. data/db/migrate/20260920000002_create_event_timing_gaps.rb +55 -0
  26. data/lib/rails_error_dashboard/commands/find_or_increment_error.rb +70 -4
  27. data/lib/rails_error_dashboard/commands/flush_storm_counts.rb +225 -8
  28. data/lib/rails_error_dashboard/commands/log_error.rb +213 -21
  29. data/lib/rails_error_dashboard/configuration.rb +20 -0
  30. data/lib/rails_error_dashboard/engine.rb +13 -0
  31. data/lib/rails_error_dashboard/manual_error_reporter.rb +16 -5
  32. data/lib/rails_error_dashboard/queries/analytics_stats.rb +85 -27
  33. data/lib/rails_error_dashboard/queries/dashboard_stats.rb +167 -30
  34. data/lib/rails_error_dashboard/queries/event_volume.rb +503 -0
  35. data/lib/rails_error_dashboard/services/breadcrumb_collector.rb +23 -0
  36. data/lib/rails_error_dashboard/services/storm_protection/count_buffer.rb +64 -6
  37. data/lib/rails_error_dashboard/services/variable_serializer.rb +125 -10
  38. data/lib/rails_error_dashboard/subscribers/breadcrumb_subscriber.rb +111 -0
  39. data/lib/rails_error_dashboard/value_objects/error_context.rb +37 -2
  40. data/lib/rails_error_dashboard/version.rb +1 -1
  41. data/lib/rails_error_dashboard.rb +3 -0
  42. metadata +8 -2
@@ -175,19 +175,109 @@ module RailsErrorDashboard
175
175
  return { value: label, truncated: false }
176
176
  end
177
177
 
178
- # Fallback: .inspect with truncation
178
+ # #inspect on an unknown object is arbitrary APPLICATION code, running
179
+ # on the failure path. Truncating its output bounds what is STORED,
180
+ # not what it COSTS: an inspect that sleeps or builds a megabyte pays
181
+ # that in full before a single character is discarded. So the default
182
+ # is a safe structural summary, and inspect runs only for types the
183
+ # host app opted in to -- under a wall-clock budget even then.
179
184
  max_len = config.local_variable_max_string_length || 200
185
+
186
+ # A Struct is serialized MEMBER-WISE, never through its own #inspect.
187
+ #
188
+ # Struct was allowlisted because it prints its attributes cheaply --
189
+ # true of the container, false of what it holds. Struct#inspect calls
190
+ # each member's #inspect, so a Struct wrapping an unknown object ran
191
+ # that object's arbitrary code in full. Walking the members instead
192
+ # gives every one of them the same safe-summary default an unknown
193
+ # object already gets, so the guarantee holds by construction rather
194
+ # than by measuring afterwards.
195
+ return serialize_struct(value, config, depth, max_depth) if struct?(value)
196
+
197
+ return { value: safe_summary(value), truncated: false } unless inspectable?(value, config)
198
+
199
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
180
200
  inspected = value.inspect
201
+ elapsed_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000
202
+
203
+ # An OUTPUT-selection threshold, not an execution budget: the inspect
204
+ # above has already run to completion by the time this is measured.
205
+ # Only reachable for a type the host app explicitly opted in to, and
206
+ # that opt-in is documented as accepting unbounded execution -- the
207
+ # only way to interrupt arbitrary Ruby mid-call is Timeout, which is
208
+ # not safe on the capture path (safety rule 1).
209
+ budget = config.local_variable_inspect_budget_ms || 5
210
+ if elapsed_ms > budget
211
+ RailsErrorDashboard::Logger.debug(
212
+ "[RailsErrorDashboard] #{value.class}#inspect took #{elapsed_ms.round(1)}ms " \
213
+ "(budget #{budget}ms) — storing a summary instead"
214
+ )
215
+ return { value: safe_summary(value), truncated: true }
216
+ end
217
+
181
218
  if inspected.length > max_len
182
219
  { value: inspected[0, max_len], truncated: true }
183
220
  else
184
221
  { value: inspected, truncated: false }
185
222
  end
186
223
  rescue
187
- { value: "#<#{value.class.name rescue "Object"}>", truncated: false }
224
+ { value: safe_summary(value), truncated: false }
188
225
  end
189
226
  private_class_method :serialize_object
190
227
 
228
+ def self.struct?(value)
229
+ value.is_a?(Struct)
230
+ rescue StandardError
231
+ false
232
+ end
233
+ private_class_method :struct?
234
+
235
+ # Serialize a Struct's members through the ordinary bounded path.
236
+ #
237
+ # Bounded twice over: member count is capped, and each member recurses
238
+ # with depth + 1, so a Struct of Structs cannot reintroduce unbounded
239
+ # work through recursion instead of through #inspect.
240
+ def self.serialize_struct(value, config, depth, max_depth)
241
+ max_members = config.local_variable_max_array_items || 10
242
+ members = value.members.first(max_members)
243
+ truncated = value.members.size > members.size
244
+
245
+ pairs = members.map do |member|
246
+ serialized = serialize_value(value[member], config, depth + 1, max_depth)
247
+ truncated ||= serialized[:truncated]
248
+ "#{member}=#{serialized[:value]}"
249
+ end
250
+
251
+ # An anonymous Struct has no class name; label it by shape rather than
252
+ # rendering "#<struct a=1>" with a hole in it.
253
+ label = value.class.name.presence || "struct"
254
+ { value: "#<#{label} #{pairs.join(', ')}>", truncated: truncated }
255
+ rescue StandardError
256
+ { value: safe_summary(value), truncated: false }
257
+ end
258
+ private_class_method :serialize_struct
259
+
260
+ # What an object is, without asking the object. Costs one class-name read.
261
+ def self.safe_summary(value)
262
+ "#<#{value.class.name}>"
263
+ rescue StandardError
264
+ "#<Object>"
265
+ end
266
+ private_class_method :safe_summary
267
+
268
+ # True when this object's class (or an ancestor) is on the allowlist, so
269
+ # the host app has accepted the cost of its #inspect.
270
+ def self.inspectable?(value, config)
271
+ allowlist = Array(config.local_variable_inspect_allowlist)
272
+ return false if allowlist.empty?
273
+
274
+ ancestors = value.class.ancestors.map { |mod| mod.name }.compact
275
+ (ancestors & allowlist).any?
276
+ rescue StandardError
277
+ false
278
+ end
279
+ private_class_method :inspectable?
280
+
191
281
  # --- Sensitive data filtering (post-serialization) ---
192
282
  # Reuses SensitiveDataFilter.parameter_filter — same pattern as BreadcrumbCollector.
193
283
  # Applied AFTER serialization so ParameterFilter works on clean JSON-compatible values.
@@ -215,14 +305,39 @@ module RailsErrorDashboard
215
305
  info[:value] = SensitiveDataFilter.send(:filter_message, filter, info[:value])
216
306
  end
217
307
 
218
- # Filter nested hash keys recursively
219
- if info[:value].is_a?(Hash)
220
- info[:value] = filter_hash_recursive(filter, info[:value])
221
- end
222
-
223
- # Filter nested array items
224
- if info[:value].is_a?(Array)
225
- info[:value] = filter_array_recursive(filter, info[:value])
308
+ # Path-aware filtering, in ONE call for every container type.
309
+ #
310
+ # The value is wrapped back under its own variable name so the filter
311
+ # sees the SAME key path Rails sees for request params. A dotted
312
+ # pattern like "profile.private_note" is a path, not a name: dropping
313
+ # the "profile" segment means the value Rails redacts in params stays
314
+ # readable here. Unwrapping afterwards leaves the stored shape
315
+ # unchanged.
316
+ #
317
+ # This must NOT ask "what shape is this?" first. Wrapping only Hashes
318
+ # and sending Arrays straight to the recursive walker is exactly how
319
+ # `profile = [{ private_note: ... }]` leaked while the identical
320
+ # request params were redacted: ParameterFilter already traverses
321
+ # arbitrary nesting of Hash and Array, so one wrap covers every shape
322
+ # and a new container type cannot reintroduce the gap.
323
+ #
324
+ # Parity with Rails is the contract in both directions -- a pattern
325
+ # that Rails does NOT match (profile.list.private_note, or a bare
326
+ # scalar) must survive here too. Over-redaction silently destroys
327
+ # data a developer needs to debug.
328
+ if info[:value].is_a?(Hash) || info[:value].is_a?(Array)
329
+ scoped = filter.filter(var_name => info[:value])[var_name]
330
+
331
+ # The recursive pass stays, and runs AFTER the path-aware filter:
332
+ # it scrubs sensitive CONTENT inside strings (credit-card and
333
+ # key=value patterns), which ParameterFilter does not do -- it only
334
+ # matches keys.
335
+ info[:value] =
336
+ case scoped
337
+ when Hash then filter_hash_recursive(filter, scoped)
338
+ when Array then filter_array_recursive(filter, scoped)
339
+ else scoped
340
+ end
226
341
  end
227
342
  end
228
343
 
@@ -16,6 +16,18 @@ module RailsErrorDashboard
16
16
  class BreadcrumbSubscriber
17
17
  SQL_MESSAGE_MAX = 200
18
18
 
19
+ # Where a failing job's trail waits for the reporter.
20
+ #
21
+ # ActiveJob reports a job error OUTSIDE the frame that runs
22
+ # `run_callbacks :perform`: perform_now's `rescue Exception` is one frame
23
+ # out (activejob execution.rb), and the report itself fires two frames
24
+ # further out still, from the :execute around-callback that the railtie
25
+ # registers (ExecutionWrapper.wrap's `rescue Exception` ->
26
+ # error_reporter.report). By the time LogError runs, our ensure has
27
+ # already cleared the buffer. So we snapshot on the way out and leave the
28
+ # snapshot here for LogError to pick up.
29
+ JOB_TRAIL_KEY = :rails_error_dashboard_job_breadcrumb_trail
30
+
19
31
  # Event subscriptions managed by this class
20
32
  @subscriptions = []
21
33
 
@@ -38,6 +50,105 @@ module RailsErrorDashboard
38
50
  @subscriptions
39
51
  end
40
52
 
53
+ # Open a breadcrumb buffer around every Active Job perform, so a job
54
+ # that fails outside a request still has an activity trail.
55
+ #
56
+ # Idempotent: the callback list belongs to ActiveJob::Base, and a
57
+ # second registration would open and close the buffer twice per job.
58
+ #
59
+ # The config check is INSIDE the callback, not around this method.
60
+ # enable_breadcrumbs defaults to false and the callback list is fixed
61
+ # once the class loads, so gating registration would permanently
62
+ # disable job breadcrumbs for any host that enables the feature in an
63
+ # initializer running after the engine's.
64
+ # @return [Boolean] true when the callback was installed
65
+ def install_job_buffer!
66
+ return false if @job_buffer_installed
67
+ return false unless defined?(ActiveJob::Base)
68
+
69
+ @job_buffer_installed = true
70
+ ActiveSupport.on_load(:active_job) do
71
+ around_perform do |job, block|
72
+ collector = RailsErrorDashboard::Services::BreadcrumbCollector
73
+ subscriber = RailsErrorDashboard::Subscribers::BreadcrumbSubscriber
74
+
75
+ # Drop any snapshot a previous perform on this pooled thread left
76
+ # behind. This, not the ensure below, is what bounds the leak:
77
+ # discard_on (and a host rescue_from) can swallow an exception so
78
+ # that nothing is ever reported and nothing ever consumes the
79
+ # snapshot. At most one job's serialized trail survives, and only
80
+ # until this thread's very next perform.
81
+ Thread.current[subscriber::JOB_TRAIL_KEY] = nil
82
+
83
+ owned =
84
+ if RailsErrorDashboard.configuration.enable_breadcrumbs &&
85
+ !subscriber.capture_job?(job)
86
+ collector.init_buffer_unless_present
87
+ else
88
+ false
89
+ end
90
+
91
+ completed = false
92
+ begin
93
+ block.call
94
+ completed = true
95
+ ensure
96
+ # Snapshot BEFORE clearing, and only when the job did not
97
+ # finish normally -- see JOB_TRAIL_KEY: the error is reported
98
+ # two frames outside this one, long after the clear.
99
+ #
100
+ # `completed`, not a `rescue Exception`, because retry_on and
101
+ # discard_on with `report: true` report the error and then
102
+ # return normally: no exception passes through here, yet the
103
+ # capture still needs the trail.
104
+ #
105
+ # Gated on `owned`: a job running inline inside a request must
106
+ # not copy out -- or clear -- a buffer the request owns.
107
+ if owned && !completed
108
+ begin
109
+ trail = collector.current_breadcrumbs
110
+ Thread.current[subscriber::JOB_TRAIL_KEY] = trail if trail.is_a?(Array) && trail.any?
111
+ rescue StandardError
112
+ nil # never raise from the capture path
113
+ end
114
+ end
115
+
116
+ # ensure, always: a worker pool reuses threads, and a
117
+ # thread-local left behind would leak one job's trail into the
118
+ # next (safety rule 4).
119
+ collector.clear_buffer_if_owned(owned)
120
+ end
121
+ end
122
+ end
123
+ true
124
+ rescue StandardError => e
125
+ RailsErrorDashboard::Logger.debug(
126
+ "[RailsErrorDashboard] install_job_buffer! failed: #{e.class} - #{e.message}"
127
+ )
128
+ false
129
+ end
130
+
131
+ # Is the job now performing one of the gem's OWN capture jobs?
132
+ #
133
+ # AsyncErrorLoggingJob descends from ActiveJob::Base, so it runs
134
+ # through the around_perform above like any host job. Opening a buffer
135
+ # for it collects the gem's own write traffic -- its cache reads for
136
+ # the application record, its SAVEPOINT/RELEASE SAVEPOINT pairs -- and
137
+ # the anti-recursion filter does not catch those (it is a substring
138
+ # test for "rails_error_dashboard_" against the SQL text, and
139
+ # transaction control carries no table name).
140
+ #
141
+ # Today that noise is discarded because the envelope wins in LogError.
142
+ # Once a failing job's buffer is snapshotted and stored, it would
143
+ # become the trail on the gem's own failure path. Never open one.
144
+ # @param job [ActiveJob::Base]
145
+ # @return [Boolean]
146
+ def capture_job?(job)
147
+ job.class.name.to_s.start_with?("RailsErrorDashboard::")
148
+ rescue StandardError
149
+ false
150
+ end
151
+
41
152
  # Remove all breadcrumb subscribers
42
153
  def unsubscribe!
43
154
  @subscriptions.each do |sub|
@@ -7,7 +7,8 @@ module RailsErrorDashboard
7
7
  class ErrorContext
8
8
  attr_reader :user_id, :request_url, :request_params, :user_agent, :ip_address, :platform,
9
9
  :controller_name, :action_name, :request_id, :session_id,
10
- :http_method, :hostname, :content_type, :request_duration_ms, :environment
10
+ :http_method, :hostname, :content_type, :request_duration_ms, :environment,
11
+ :occurred_at, :app_version
11
12
 
12
13
  def initialize(context, source = nil)
13
14
  @context = context
@@ -23,6 +24,8 @@ module RailsErrorDashboard
23
24
  @action_name = extract_action_name
24
25
  @request_id = extract_request_id
25
26
  @session_id = extract_session_id
27
+ @occurred_at = extract_occurred_at
28
+ @app_version = extract_app_version
26
29
  @http_method = extract_http_method
27
30
  @hostname = extract_hostname
28
31
  @content_type = extract_content_type
@@ -52,12 +55,40 @@ module RailsErrorDashboard
52
55
  hostname: hostname,
53
56
  content_type: content_type,
54
57
  request_duration_ms: request_duration_ms,
55
- environment: environment
58
+ environment: environment,
59
+ # Both belong in to_h, not only in the readers: LogError builds a
60
+ # SECOND ErrorContext from this hash on the async path, and a key
61
+ # missing here is silently dropped there. That hop is what lost
62
+ # request_id and session_id before.
63
+ occurred_at: occurred_at,
64
+ app_version: app_version
56
65
  }
57
66
  end
58
67
 
59
68
  private
60
69
 
70
+ # A caller-supplied event time, e.g. a mobile client reporting a failure
71
+ # that happened while it was offline. Never in the future: a client clock
72
+ # can be wrong, and a future row would sort above every real error and
73
+ # never age out of a window.
74
+ def extract_occurred_at
75
+ raw = @context[:occurred_at]
76
+ return nil if raw.blank?
77
+
78
+ time = raw.is_a?(String) ? Time.zone.parse(raw) : raw
79
+ return nil unless time.respond_to?(:to_time)
80
+
81
+ [ time, Time.current ].min
82
+ rescue StandardError
83
+ nil
84
+ end
85
+
86
+ # The release the REPORTER was running, which for a mobile or frontend
87
+ # report is the whole point -- it differs from the server's version.
88
+ def extract_app_version
89
+ @context[:app_version].presence
90
+ end
91
+
61
92
  def extract_user_id
62
93
  @context[:current_user]&.id ||
63
94
  @context[:user_id] ||
@@ -120,6 +151,10 @@ module RailsErrorDashboard
120
151
  # Additional context (from mobile apps, etc.)
121
152
  params.merge!(@context[:additional_context]) if @context[:additional_context]
122
153
 
154
+ # Caller-supplied metadata, documented by ManualErrorReporter and
155
+ # previously accepted and discarded.
156
+ params.merge!(@context[:metadata]) if @context[:metadata].is_a?(Hash)
157
+
123
158
  # Pre-serialized params (from async logging or double-ErrorContext path).
124
159
  # LogError creates a second ErrorContext from error_context.to_h which
125
160
  # has :request_params as a JSON string but no :request object.
@@ -1,3 +1,3 @@
1
1
  module RailsErrorDashboard
2
- VERSION = "0.13.0"
2
+ VERSION = "0.14.0"
3
3
  end
@@ -138,6 +138,9 @@ require "rails_error_dashboard/commands/flush_rack_attack_events"
138
138
  require "rails_error_dashboard/commands/scrub_invalid_encoding"
139
139
  require "rails_error_dashboard/commands/backfill_resolved_at"
140
140
  require "rails_error_dashboard/queries/errors_list"
141
+ # Before the queries that use it: window volume is shared by dashboard and
142
+ # analytics, and both would otherwise rescue a NameError into a silent zero.
143
+ require "rails_error_dashboard/queries/event_volume"
141
144
  require "rails_error_dashboard/queries/dashboard_stats"
142
145
  require "rails_error_dashboard/queries/analytics_stats"
143
146
  require "rails_error_dashboard/queries/filter_options"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_error_dashboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anjan Jagirdar
@@ -309,6 +309,8 @@ files:
309
309
  - app/models/rails_error_dashboard/error_log.rb
310
310
  - app/models/rails_error_dashboard/error_logs_record.rb
311
311
  - app/models/rails_error_dashboard/error_occurrence.rb
312
+ - app/models/rails_error_dashboard/event_count.rb
313
+ - app/models/rails_error_dashboard/event_timing_gap.rb
312
314
  - app/models/rails_error_dashboard/rack_attack_event.rb
313
315
  - app/models/rails_error_dashboard/storm_event.rb
314
316
  - app/models/rails_error_dashboard/storm_flush_batch.rb
@@ -420,6 +422,9 @@ files:
420
422
  - db/migrate/20260915000003_add_context_provenance_to_error_logs.rb
421
423
  - db/migrate/20260915000004_create_storm_flush_batches.rb
422
424
  - db/migrate/20260917000001_add_last_notified_at_to_error_logs.rb
425
+ - db/migrate/20260919000001_create_event_counts.rb
426
+ - db/migrate/20260920000001_add_buckets_incomplete_to_storm_events.rb
427
+ - db/migrate/20260920000002_create_event_timing_gaps.rb
423
428
  - lib/generators/rails_error_dashboard/install/install_generator.rb
424
429
  - lib/generators/rails_error_dashboard/install/templates/README
425
430
  - lib/generators/rails_error_dashboard/install/templates/initializer.rb
@@ -490,6 +495,7 @@ files:
490
495
  - lib/rails_error_dashboard/queries/error_cascades.rb
491
496
  - lib/rails_error_dashboard/queries/error_correlation.rb
492
497
  - lib/rails_error_dashboard/queries/errors_list.rb
498
+ - lib/rails_error_dashboard/queries/event_volume.rb
493
499
  - lib/rails_error_dashboard/queries/filter_options.rb
494
500
  - lib/rails_error_dashboard/queries/job_health_summary.rb
495
501
  - lib/rails_error_dashboard/queries/llm_health_summary.rb
@@ -593,7 +599,7 @@ metadata:
593
599
  funding_uri: https://github.com/sponsors/AnjanJ
594
600
  post_install_message: |
595
601
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
596
- RED (Rails Error Dashboard) v0.13.0
602
+ RED (Rails Error Dashboard) v0.14.0
597
603
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
598
604
 
599
605
  First install: