Module: Pinot::ActiveSupportNotifications

Defined in:
lib/pinot/active_support_notifications.rb

Overview

Opt-in ActiveSupport::Notifications bridge for Rails applications.

Setup

Add one line to an initializer (e.g. config/initializers/pinot.rb):

require "pinot/active_support_notifications"
Pinot::ActiveSupportNotifications.install!

That’s it. Every query executed via Connection#execute_sql (including those from execute_sql_with_params, execute_many, and PreparedStatement) will publish a “sql.pinot” event on the ActiveSupport::Notifications bus.

Subscribing

ActiveSupport::Notifications.subscribe("sql.pinot") do |name, start, finish, id, payload|
  Rails.logger.debug "[Pinot] #{payload[:name]}#{payload[:sql]} (#{payload[:duration].round(1)} ms)"
end

Payload keys

:sql              — the SQL query string
:name             — the Pinot table name (empty string for table-less queries)
:duration         — execution time in milliseconds (Float)
:success          — true on success, false when an exception was raised
:exception        — [ExceptionClassName, message] on error, absent on success
                    (follows the ActiveSupport::Notifications convention)
:exception_object — the raw exception on error, absent on success
                    (follows the ActiveSupport::Notifications convention)

Lifecycle

The bridge is installed idempotently:

Pinot::ActiveSupportNotifications.install!   # register
Pinot::ActiveSupportNotifications.installed? # => true
Pinot::ActiveSupportNotifications.uninstall! # deregister (e.g. in tests)

Note: this gem does NOT depend on activesupport. The bridge requires ActiveSupport::Notifications to already be defined at install! time (which is always the case in a Rails process).

Constant Summary collapse

EVENT_NAME =
"sql.pinot"

Class Method Summary collapse

Class Method Details

.install!Object



48
49
50
51
52
53
# File 'lib/pinot/active_support_notifications.rb', line 48

def self.install!
  return if installed?

  Pinot::Instrumentation.on_query = method(:notify)
  @installed = true
end

.installed?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/pinot/active_support_notifications.rb', line 55

def self.installed?
  @installed || false
end

.uninstall!Object



59
60
61
62
# File 'lib/pinot/active_support_notifications.rb', line 59

def self.uninstall!
  Pinot::Instrumentation.on_query = nil
  @installed = false
end