Class: JetstreamBridge::ConsumerMiddleware::MetricsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/consumer/middleware.rb

Overview

Metrics middleware for tracking event processing

Examples:

consumer.use(JetstreamBridge::Consumer::MetricsMiddleware.new(
  on_success: ->(event, duration) { StatsD.timing("event.process", duration) },
  on_failure: ->(event, error) { StatsD.increment("event.failed") }
))

Instance Method Summary collapse

Constructor Details

#initialize(on_success: nil, on_failure: nil) ⇒ MetricsMiddleware



95
96
97
98
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 95

def initialize(on_success: nil, on_failure: nil)
  @on_success = on_success
  @on_failure = on_failure
end

Instance Method Details

#call(event) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/jetstream_bridge/consumer/middleware.rb', line 100

def call(event)
  start = Time.now
  yield
  duration = Time.now - start
  @on_success&.call(event, duration)
rescue StandardError => e
  @on_failure&.call(event, e)
  raise
end