Module: FlowChat::Instrumentation::Setup

Defined in:
lib/flow_chat/instrumentation/setup.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log_subscriberObject

Returns the value of attribute log_subscriber.



5
6
7
# File 'lib/flow_chat/instrumentation/setup.rb', line 5

def log_subscriber
  @log_subscriber
end

.metrics_collectorObject

Access the metrics collector instance



74
75
76
# File 'lib/flow_chat/instrumentation/setup.rb', line 74

def metrics_collector
  @metrics_collector
end

Class Method Details

.cleanup!Object

Cleanup all subscribers



38
39
40
41
42
43
44
45
# File 'lib/flow_chat/instrumentation/setup.rb', line 38

def cleanup!
  @log_subscriber = nil
  @metrics_collector = nil

  # Note: ActiveSupport::Notifications doesn't provide an easy way to
  # unsubscribe all subscribers, so this is mainly for reference cleanup
  FlowChat.logger&.info { "FlowChat::Instrumentation: Cleaned up instrumentation" }
end

.initialize!Object

Initialize instrumentation with default subscribers



8
9
10
11
12
13
# File 'lib/flow_chat/instrumentation/setup.rb', line 8

def initialize!
  setup_log_subscriber if FlowChat::Config.logger
  setup_metrics_collector

  FlowChat.logger&.info { "FlowChat::Instrumentation: Initialized with logging and metrics collection" }
end

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

Instrument a one-off event



63
64
65
66
67
68
69
70
71
# File 'lib/flow_chat/instrumentation/setup.rb', line 63

def instrument(event_name, payload = {}, &block)
  full_event_name = "#{event_name}.flow_chat"

  enriched_payload = {
    timestamp: Time.current
  }.merge(payload).compact

  ActiveSupport::Notifications.instrument(full_event_name, enriched_payload, &block)
end

.metricsObject

Get current metrics (thread-safe)



48
49
50
# File 'lib/flow_chat/instrumentation/setup.rb', line 48

def metrics
  @metrics_collector&.snapshot || {}
end

.reset!Object

Reset instrumentation (useful for testing)



79
80
81
82
83
84
# File 'lib/flow_chat/instrumentation/setup.rb', line 79

def reset!
  @log_subscriber_setup = false
  @metrics_collector_setup = false
  @log_subscriber = nil
  @metrics_collector = nil
end

.reset_metrics!Object

Reset metrics



53
54
55
# File 'lib/flow_chat/instrumentation/setup.rb', line 53

def reset_metrics!
  @metrics_collector&.reset!
end

.setup_instrumentation!(options = {}) ⇒ Object

Set up both logging and metrics collection



16
17
18
19
# File 'lib/flow_chat/instrumentation/setup.rb', line 16

def setup_instrumentation!(options = {})
  setup_logging!(options)
  setup_metrics!(options)
end

.setup_logging!(options = {}) ⇒ Object

Set up logging (LogSubscriber)



22
23
24
25
26
27
# File 'lib/flow_chat/instrumentation/setup.rb', line 22

def setup_logging!(options = {})
  return if @log_subscriber_setup

  setup_log_subscriber(options)
  @log_subscriber_setup = true
end

.setup_metrics!(options = {}) ⇒ Object

Set up metrics collection (MetricsCollector)



30
31
32
33
34
35
# File 'lib/flow_chat/instrumentation/setup.rb', line 30

def setup_metrics!(options = {})
  return if @metrics_collector_setup

  setup_metrics_collector(options)
  @metrics_collector_setup = true
end

.subscribe(event_pattern, &block) ⇒ Object

Subscribe to custom events



58
59
60
# File 'lib/flow_chat/instrumentation/setup.rb', line 58

def subscribe(event_pattern, &block)
  ActiveSupport::Notifications.subscribe(event_pattern, &block)
end