Class: WaterDrop::Instrumentation::Monitor

Inherits:
Karafka::Core::Monitoring::Monitor
  • Object
show all
Defined in:
lib/waterdrop/instrumentation/monitor.rb

Overview

WaterDrop instrumentation monitor that we use to publish events By default uses our internal notifications bus but can be used with ‘ActiveSupport::Notifications` as well

Instance Method Summary collapse

Constructor Details

#initialize(notifications_bus = WaterDrop::Instrumentation::Notifications.new, namespace = nil) ⇒ Monitor

Returns a new instance of Monitor.

Parameters:

  • notifications_bus (Object) (defaults to: WaterDrop::Instrumentation::Notifications.new)

    either our internal notifications bus or ‘ActiveSupport::Notifications`

  • namespace (String, nil) (defaults to: nil)

    namespace for events or nil if no namespace



21
22
23
24
25
26
27
# File 'lib/waterdrop/instrumentation/monitor.rb', line 21

def initialize(
  notifications_bus = WaterDrop::Instrumentation::Notifications.new,
  namespace = nil
)
  super
  @statistics_listeners_frozen = false
end

Instance Method Details

#freeze_statistics_listeners!Object

Marks this monitor as no longer accepting new subscriptions to ‘statistics.emitted`. Called by the rdkafka client builder when it decides to leave librdkafka statistics disabled (because no listener was present at build time). Any subsequent attempt to subscribe to `statistics.emitted` — either via a block or via a listener object that responds to `on_statistics_emitted` — will raise `WaterDrop::Errors::StatisticsNotEnabledError` instead of silently doing nothing.



35
36
37
# File 'lib/waterdrop/instrumentation/monitor.rb', line 35

def freeze_statistics_listeners!
  @statistics_listeners_frozen = true
end

#subscribe(event_id_or_listener, &block) ⇒ Object

Subscribes to the notifications bus, raising if the user tries to subscribe to ‘statistics.emitted` after statistics have been disabled at client build time. This prevents the “silent nothing” pitfall where a user expects statistics but no events ever arrive because librdkafka statistics were turned off entirely.

Parameters:

  • event_id_or_listener (String, Symbol, Object)

    event id (with block) or listener

  • block (Proc, nil)

    handler block when subscribing to a named event

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/waterdrop/instrumentation/monitor.rb', line 48

def subscribe(event_id_or_listener, &block)
  if @statistics_listeners_frozen && targets_statistics?(event_id_or_listener, block)
    raise Errors::StatisticsNotEnabledError, <<~MSG.tr("\n", " ").strip
      Cannot subscribe to `statistics.emitted` after the producer has been connected.
      Statistics are disabled because no listener was subscribed before the underlying
      rdkafka client was built, so librdkafka is not emitting statistics at all.
      Subscribe your listener BEFORE the first producer use (before the underlying
      client is lazily initialized), or explicitly keep statistics enabled by leaving
      a listener in place at build time.
    MSG
  end

  super
end