Module: StagingTable::Instrumentation

Defined in:
lib/staging_table/instrumentation.rb

Overview

Provides ActiveSupport::Notifications instrumentation for StagingTable operations.

Available events:

- staging_table.create_table  - When a staging table is created
- staging_table.drop_table    - When a staging table is dropped
- staging_table.insert        - When records are inserted into staging
- staging_table.transfer      - When data is transferred to target table
- staging_table.stage         - Wraps the entire staging block operation

Example:

ActiveSupport::Notifications.subscribe('staging_table.transfer') do |event|
  Rails.logger.info "Transfer completed in #{event.duration}ms"
  StatsD.measure('staging_table.transfer.duration', event.duration)
end

Constant Summary collapse

NAMESPACE =
"staging_table"
EVENTS =
%i[
  create_table
  drop_table
  insert
  transfer
  stage
].freeze

Class Method Summary collapse

Class Method Details

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

Instruments a block with the given event name.

Parameters:

  • event_name (Symbol)

    The event name (without namespace)

  • payload (Hash) (defaults to: {})

    Additional payload data

Yields:

  • The block to instrument

Returns:

  • The result of the block



39
40
41
42
# File 'lib/staging_table/instrumentation.rb', line 39

def instrument(event_name, payload = {}, &block)
  full_event_name = "#{NAMESPACE}.#{event_name}"
  ActiveSupport::Notifications.instrument(full_event_name, payload, &block)
end

.subscribe(event_name) {|event| ... } ⇒ ActiveSupport::Notifications::Fanout::Subscribers::Evented

Subscribe to a StagingTable event.

Parameters:

  • event_name (Symbol, String)

    Event name (with or without namespace)

Yields:

  • (event)

    Block called for each event

Yield Parameters:

  • event (ActiveSupport::Notifications::Event)

Returns:

  • (ActiveSupport::Notifications::Fanout::Subscribers::Evented)


50
51
52
53
# File 'lib/staging_table/instrumentation.rb', line 50

def subscribe(event_name, &block)
  full_name = event_name.to_s.start_with?(NAMESPACE) ? event_name : "#{NAMESPACE}.#{event_name}"
  ActiveSupport::Notifications.subscribe(full_name, &block)
end

.subscribe_all {|event| ... } ⇒ ActiveSupport::Notifications::Fanout::Subscribers::Evented

Subscribe to all StagingTable events.

Yields:

  • (event)

    Block called for each event

Returns:

  • (ActiveSupport::Notifications::Fanout::Subscribers::Evented)


66
67
68
# File 'lib/staging_table/instrumentation.rb', line 66

def subscribe_all(&block)
  ActiveSupport::Notifications.subscribe(/^#{NAMESPACE}\./o, &block)
end

.unsubscribe(subscriber) ⇒ Object

Unsubscribe from a StagingTable event.

Parameters:

  • subscriber (Object)

    The subscriber to remove



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

def unsubscribe(subscriber)
  ActiveSupport::Notifications.unsubscribe(subscriber)
end