Module: Railsmith::Instrumentation

Defined in:
lib/railsmith/instrumentation.rb

Overview

Lightweight instrumentation hook layer for domain-tagged service events.

Uses ActiveSupport::Notifications when available so events slot naturally into Rails instrumentation pipelines. Falls back to plain Ruby subscribers for non-Rails contexts.

Example (plain Ruby subscriber):

Railsmith::Instrumentation.subscribe("service.call") do |event, payload|
  Rails.logger.info "[#{payload[:domain]}] #{payload[:service]}##{payload[:action]}"
end

Constant Summary collapse

EVENT_NAMESPACE =
"railsmith"

Class Method Summary collapse

Class Method Details

.instrument(event_name, payload = {}, &block) ⇒ Object

Emit a domain-tagged event, yielding to the wrapped block if given. Payload is always a Hash; a :domain key is expected for domain tagging. Always dispatches to plain Ruby subscribers; also emits to ActiveSupport::Notifications when available for Rails integration.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/railsmith/instrumentation.rb', line 22

def instrument(event_name, payload = {}, &block)
  return block&.call unless Railsmith.configuration.instrumentation_enabled

  full_name = "#{event_name}.#{EVENT_NAMESPACE}"
  result = nil
  if active_support_notifications?
    ActiveSupport::Notifications.instrument(full_name, payload) { result = block&.call }
  else
    result = block&.call
  end
  dispatch(full_name, payload)
  result
end

.reset!Object

Remove all plain Ruby subscribers (useful in tests).



43
44
45
# File 'lib/railsmith/instrumentation.rb', line 43

def reset!
  @subscribers = []
end

.subscribe(pattern = nil, &block) ⇒ Object

Register a plain Ruby subscriber for events matching an optional prefix. Subscriber is called with (event_name, payload).



38
39
40
# File 'lib/railsmith/instrumentation.rb', line 38

def subscribe(pattern = nil, &block)
  subscribers << { pattern: pattern, handler: block }
end