Module: Familia::Instrumentation
- Defined in:
- lib/familia/instrumentation.rb
Overview
Provides instrumentation hooks for observability into Familia operations.
This module allows applications to register callbacks for various events in Familia's lifecycle, enabling audit trails, performance monitoring, and operational observability.
Class Method Summary collapse
-
.hooks?(type) ⇒ Boolean
Check whether any hooks are registered for the given event type.
-
.notify_command(cmd, duration, context = {}) ⇒ Object
private
Notify all registered command hooks.
-
.notify_error(error, context = {}) ⇒ Object
private
Notify all registered error hooks.
-
.notify_lifecycle(event, instance, context = {}) ⇒ Object
private
Notify all registered lifecycle hooks.
-
.notify_pipeline(command_count, duration, context = {}) ⇒ Object
private
Notify all registered pipeline hooks.
-
.on_command {|cmd, duration, context| ... } ⇒ Object
Register a callback for Redis command execution.
-
.on_error {|error, context| ... } ⇒ Object
Register a callback for error conditions.
-
.on_lifecycle {|event, instance, context| ... } ⇒ Object
Register a callback for Horreum lifecycle events.
-
.on_pipeline {|command_count, duration, context| ... } ⇒ Object
Register a callback for pipelined Redis operations.
Class Method Details
.hooks?(type) ⇒ Boolean
Check whether any hooks are registered for the given event type.
Because Familia::Instrumentation is always loaded, a
defined?(Familia::Instrumentation) check is always true and cannot be
used to gate fast paths. This predicate answers the question that
actually matters: is anyone listening? Middleware uses it to decide
whether timing must be measured so that observability hooks keep firing
at full rate even when command capture is disabled.
132 133 134 135 |
# File 'lib/familia/instrumentation.rb', line 132 def hooks?(type) hooks = @hooks[type] !hooks.nil? && !hooks.empty? end |
.notify_command(cmd, duration, context = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notify all registered command hooks.
139 140 141 142 143 144 145 |
# File 'lib/familia/instrumentation.rb', line 139 def notify_command(cmd, duration, context = {}) @hooks[:command].each do |hook| hook.call(cmd, duration, context) rescue => e Familia.error("Instrumentation hook failed", error: e., hook_type: :command) end end |
.notify_error(error, context = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notify all registered error hooks.
169 170 171 172 173 174 175 |
# File 'lib/familia/instrumentation.rb', line 169 def notify_error(error, context = {}) @hooks[:error].each do |hook| hook.call(error, context) rescue => e # Don't recurse on hook failures - just silently skip end end |
.notify_lifecycle(event, instance, context = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notify all registered lifecycle hooks.
159 160 161 162 163 164 165 |
# File 'lib/familia/instrumentation.rb', line 159 def notify_lifecycle(event, instance, context = {}) @hooks[:lifecycle].each do |hook| hook.call(event, instance, context) rescue => e Familia.error("Instrumentation hook failed", error: e., hook_type: :lifecycle) end end |
.notify_pipeline(command_count, duration, context = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Notify all registered pipeline hooks.
149 150 151 152 153 154 155 |
# File 'lib/familia/instrumentation.rb', line 149 def notify_pipeline(command_count, duration, context = {}) @hooks[:pipeline].each do |hook| hook.call(command_count, duration, context) rescue => e Familia.error("Instrumentation hook failed", error: e., hook_type: :pipeline) end end |
.on_command {|cmd, duration, context| ... } ⇒ Object
Register a callback for Redis command execution.
55 56 57 |
# File 'lib/familia/instrumentation.rb', line 55 def on_command(&block) @hooks[:command] << block end |
.on_error {|error, context| ... } ⇒ Object
Register a callback for error conditions.
111 112 113 |
# File 'lib/familia/instrumentation.rb', line 111 def on_error(&block) @hooks[:error] << block end |
.on_lifecycle {|event, instance, context| ... } ⇒ Object
Register a callback for Horreum lifecycle events.
93 94 95 |
# File 'lib/familia/instrumentation.rb', line 93 def on_lifecycle(&block) @hooks[:lifecycle] << block end |
.on_pipeline {|command_count, duration, context| ... } ⇒ Object
Register a callback for pipelined Redis operations.
72 73 74 |
# File 'lib/familia/instrumentation.rb', line 72 def on_pipeline(&block) @hooks[:pipeline] << block end |