Class: Railscope::Context
- Inherits:
-
Object
- Object
- Railscope::Context
- Defined in:
- lib/railscope/context.rb
Constant Summary collapse
- THREAD_KEY =
:railscope_context
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #add_tag(tag) ⇒ Object
- #add_tags(*new_tags) ⇒ Object
-
#batch_id ⇒ Object
Batch ID groups all entries from a single request/job.
- #batch_id=(value) ⇒ Object
- #buffer_entry(entry_data) ⇒ Object
- #fetch(key, default = nil) ⇒ Object
- #flush_pending! ⇒ Object
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #merge!(hash) ⇒ Object
-
#pending_entries ⇒ Object
Conditional recording: buffer entries until a trigger fires.
-
#request_id ⇒ Object
Request ID from Rails (for correlation with Rails logs).
- #request_id=(value) ⇒ Object
- #tags ⇒ Object
- #to_h ⇒ Object
- #trigger! ⇒ Object
- #triggered? ⇒ Boolean
- #user_id ⇒ Object
- #user_id=(value) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
26 27 28 |
# File 'lib/railscope/context.rb', line 26 def initialize @store = {} end |
Class Method Details
.clear! ⇒ Object
12 13 14 |
# File 'lib/railscope/context.rb', line 12 def clear! Thread.current[THREAD_KEY] = nil end |
.current ⇒ Object
8 9 10 |
# File 'lib/railscope/context.rb', line 8 def current Thread.current[THREAD_KEY] ||= new end |
.with(**attributes) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/railscope/context.rb', line 16 def with(**attributes) previous = current.to_h.dup attributes.each { |key, value| current[key] = value } yield ensure clear! previous.each { |key, value| current[key] = value } end |
Instance Method Details
#[](key) ⇒ Object
34 35 36 |
# File 'lib/railscope/context.rb', line 34 def [](key) @store[key.to_sym] end |
#[]=(key, value) ⇒ Object
30 31 32 |
# File 'lib/railscope/context.rb', line 30 def []=(key, value) @store[key.to_sym] = value end |
#add_tag(tag) ⇒ Object
73 74 75 |
# File 'lib/railscope/context.rb', line 73 def add_tag(tag) << tag unless .include?(tag) end |
#add_tags(*new_tags) ⇒ Object
77 78 79 |
# File 'lib/railscope/context.rb', line 77 def (*) .flatten.each { |tag| add_tag(tag) } end |
#batch_id ⇒ Object
Batch ID groups all entries from a single request/job
52 53 54 |
# File 'lib/railscope/context.rb', line 52 def batch_id self[:batch_id] ||= SecureRandom.uuid end |
#batch_id=(value) ⇒ Object
56 57 58 |
# File 'lib/railscope/context.rb', line 56 def batch_id=(value) self[:batch_id] = value end |
#buffer_entry(entry_data) ⇒ Object
105 106 107 |
# File 'lib/railscope/context.rb', line 105 def buffer_entry(entry_data) pending_entries << entry_data end |
#fetch(key, default = nil) ⇒ Object
38 39 40 |
# File 'lib/railscope/context.rb', line 38 def fetch(key, default = nil) @store.fetch(key.to_sym, default) end |
#flush_pending! ⇒ Object
109 110 111 112 113 114 |
# File 'lib/railscope/context.rb', line 109 def flush_pending! pending_entries.each do |entry_data| Railscope.storage.write(**entry_data) end pending_entries.clear end |
#merge!(hash) ⇒ Object
42 43 44 45 |
# File 'lib/railscope/context.rb', line 42 def merge!(hash) hash.each { |key, value| self[key] = value } self end |
#pending_entries ⇒ Object
Conditional recording: buffer entries until a trigger fires
93 94 95 |
# File 'lib/railscope/context.rb', line 93 def pending_entries self[:pending_entries] ||= [] end |
#request_id ⇒ Object
Request ID from Rails (for correlation with Rails logs)
61 62 63 |
# File 'lib/railscope/context.rb', line 61 def request_id self[:request_id] end |
#request_id=(value) ⇒ Object
65 66 67 |
# File 'lib/railscope/context.rb', line 65 def request_id=(value) self[:request_id] = value end |
#tags ⇒ Object
69 70 71 |
# File 'lib/railscope/context.rb', line 69 def self[:tags] ||= [] end |
#to_h ⇒ Object
47 48 49 |
# File 'lib/railscope/context.rb', line 47 def to_h @store.dup end |
#trigger! ⇒ Object
101 102 103 |
# File 'lib/railscope/context.rb', line 101 def trigger! self[:triggered] = true end |
#triggered? ⇒ Boolean
97 98 99 |
# File 'lib/railscope/context.rb', line 97 def triggered? self[:triggered] == true end |
#user_id ⇒ Object
81 82 83 |
# File 'lib/railscope/context.rb', line 81 def user_id self[:user_id] end |
#user_id=(value) ⇒ Object
85 86 87 |
# File 'lib/railscope/context.rb', line 85 def user_id=(value) self[:user_id] = value end |