Class: Errorgap::Breadcrumbs
- Inherits:
-
Object
- Object
- Errorgap::Breadcrumbs
- Defined in:
- lib/errorgap/breadcrumbs.rb
Overview
Fixed-size ring of recent application events (requests, queries, jobs) attached to every notice as context.breadcrumbs. Thread-safe so it can be written from request threads and read at notify time.
Instance Method Summary collapse
- #add(message, category: nil, metadata: nil) ⇒ Object
- #clear ⇒ Object
-
#initialize(capacity) ⇒ Breadcrumbs
constructor
A new instance of Breadcrumbs.
- #to_a ⇒ Object
Constructor Details
#initialize(capacity) ⇒ Breadcrumbs
Returns a new instance of Breadcrumbs.
10 11 12 13 14 |
# File 'lib/errorgap/breadcrumbs.rb', line 10 def initialize(capacity) @capacity = capacity.to_i @crumbs = [] @mutex = Mutex.new end |
Instance Method Details
#add(message, category: nil, metadata: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/errorgap/breadcrumbs.rb', line 16 def add(, category: nil, metadata: nil) return if @capacity <= 0 crumb = { message: .to_s, timestamp: Time.now.utc.iso8601(3) } crumb[:category] = category.to_s if category crumb[:metadata] = if @mutex.synchronize do @crumbs << crumb @crumbs.shift(@crumbs.length - @capacity) if @crumbs.length > @capacity end end |
#clear ⇒ Object
29 30 31 |
# File 'lib/errorgap/breadcrumbs.rb', line 29 def clear @mutex.synchronize { @crumbs = [] } end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/errorgap/breadcrumbs.rb', line 33 def to_a @mutex.synchronize { @crumbs.dup } end |