Class: Rollout::Logging::Logger
- Inherits:
-
Object
- Object
- Rollout::Logging::Logger
- Defined in:
- lib/rollout/logging.rb
Constant Summary collapse
- CONTEXT_THREAD_KEY =
:rollout_logging_context
- WITHOUT_THREAD_KEY =
:rollout_logging_disabled
Instance Method Summary collapse
- #current_context ⇒ Object
- #delete(feature_name) ⇒ Object
- #events(feature_name) ⇒ Object
- #global_events ⇒ Object
-
#initialize(storage: nil, history_length: 50, global: false) ⇒ Logger
constructor
A new instance of Logger.
- #last_event(feature_name) ⇒ Object
- #log(event, *args) ⇒ Object
- #logging_enabled? ⇒ Boolean
- #update(before, after) ⇒ Object
- #updated_at(feature_name) ⇒ Object
- #with_context(context) ⇒ Object
- #without ⇒ Object
Constructor Details
#initialize(storage: nil, history_length: 50, global: false) ⇒ Logger
Returns a new instance of Logger.
55 56 57 58 59 |
# File 'lib/rollout/logging.rb', line 55 def initialize(storage: nil, history_length: 50, global: false) @history_length = history_length @storage = storage @global = global end |
Instance Method Details
#current_context ⇒ Object
168 169 170 |
# File 'lib/rollout/logging.rb', line 168 def current_context Thread.current[CONTEXT_THREAD_KEY] || {} end |
#delete(feature_name) ⇒ Object
88 89 90 91 |
# File 'lib/rollout/logging.rb', line 88 def delete(feature_name) storage_key = events_storage_key(feature_name) @storage.del(storage_key) end |
#events(feature_name) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/rollout/logging.rb', line 73 def events(feature_name) storage_key = events_storage_key(feature_name) @storage .zrange(storage_key, 0, -1, with_scores: true) .map { |v| Event.from_raw(*v) } .reverse end |
#global_events ⇒ Object
81 82 83 84 85 86 |
# File 'lib/rollout/logging.rb', line 81 def global_events @storage .zrange(global_events_storage_key, 0, -1, with_scores: true) .map { |v| Event.from_raw(*v) } .reverse end |
#last_event(feature_name) ⇒ Object
67 68 69 70 71 |
# File 'lib/rollout/logging.rb', line 67 def last_event(feature_name) storage_key = events_storage_key(feature_name) value = @storage.zrange(storage_key, 0, 0, with_scores: true).first Event.from_raw(*value) if value end |
#log(event, *args) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rollout/logging.rb', line 137 def log(event, *args) return unless logging_enabled? unless respond_to?(event) raise ArgumentError, "Invalid log event: #{event}" end expected_arity = method(event).arity unless args.count == expected_arity raise( ArgumentError, "Invalid number of arguments for event '#{event}': expected #{expected_arity} but got #{args.count}", ) end public_send(event, *args) end |
#logging_enabled? ⇒ Boolean
179 180 181 |
# File 'lib/rollout/logging.rb', line 179 def logging_enabled? !Thread.current[WITHOUT_THREAD_KEY] end |
#update(before, after) ⇒ Object
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 |
# File 'lib/rollout/logging.rb', line 93 def update(before, after) before_hash = before.to_hash before_hash.delete(:data).each do |k, v| before_hash["data.#{k}"] = v end after_hash = after.to_hash after_hash.delete(:data).each do |k, v| after_hash["data.#{k}"] = v end keys = before_hash.keys | after_hash.keys change = { before: {}, after: {} } changed_count = 0 keys.each do |key| next if before_hash[key] == after_hash[key] change[:before][key] = before_hash[key] change[:after][key] = after_hash[key] changed_count += 1 end return if changed_count == 0 event = Event.new( feature: after.name, name: :update, data: change, context: current_context, created_at: Time.now, ) storage_key = events_storage_key(after.name) @storage.zadd(storage_key, -event., event.serialize) @storage.zremrangebyrank(storage_key, @history_length, -1) if @global @storage.zadd(global_events_storage_key, -event., event.serialize) @storage.zremrangebyrank(global_events_storage_key, @history_length, -1) end end |
#updated_at(feature_name) ⇒ Object
61 62 63 64 65 |
# File 'lib/rollout/logging.rb', line 61 def updated_at(feature_name) storage_key = events_storage_key(feature_name) _, score = @storage.zrange(storage_key, 0, 0, with_scores: true).first Time.at(-score.to_f / 1_000_000) if score end |
#with_context(context) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/rollout/logging.rb', line 158 def with_context(context) raise ArgumentError, "context must be a Hash" unless context.is_a?(Hash) raise ArgumentError, "block is required" unless block_given? Thread.current[CONTEXT_THREAD_KEY] = context yield ensure Thread.current[CONTEXT_THREAD_KEY] = nil end |
#without ⇒ Object
172 173 174 175 176 177 |
# File 'lib/rollout/logging.rb', line 172 def without Thread.current[WITHOUT_THREAD_KEY] = true yield ensure Thread.current[WITHOUT_THREAD_KEY] = nil end |