module Sequel::NewRelicInstrumentation

New Relic’s Sequel instrumentation is implemented via a plugin for Sequel::Models, and an extension for Sequel::Databases. Every database handle that Sequel knows about when New Relic is loaded will automatically be instrumented, but if you’re using a version of Sequel before 3.47.0, you’ll need to add the extension yourself if you create any after the instrumentation is loaded:

db = Sequel.connect( ... )
db.extension :new_relic_instrumentation

Versions 3.47.0 and later use ‘Database.extension` to automatically install the extension for new connections.

Disabling

If you don’t want your models or database connections to be instrumented, you can disable them by setting ‘disable_database_instrumentation` in your `newrelic.yml` to `true`. It will also honor the `disable_active_record_instrumentation` setting.

Constants

THREAD_SAFE_CONNECTION_POOL_CLASSES

Public Instance Methods

explainer_for(sql) click to toggle source
# File lib/sequel/extensions/new_relic_instrumentation.rb, line 83
def explainer_for(sql)
  proc do |*|
    if THREAD_SAFE_CONNECTION_POOL_CLASSES.include?(self.pool.class)
      self[sql].explain
    else
      NewRelic::Agent.logger.log_once(:info, :sequel_explain_skipped, 'Not running SQL explains because Sequel is not in recognized multi-threaded mode')
      nil
    end
  end
end
notice_sql(sql) click to toggle source

We notice sql through the current_segment due to the disable_all_tracing block in the sequel Plugin instrumentation. A simple ORM operation from the Plugin instrumentation may trigger a number of calls to this method. We want to append the statements that come from the disable_all_tracing block into this node’s statement, otherwise we would end up overwriting the sql statement with the last one executed.

# File lib/sequel/extensions/new_relic_instrumentation.rb, line 66
def notice_sql(sql)
  return unless txn = NewRelic::Agent::Tracer.current_transaction

  current_segment = txn.current_segment
  return unless current_segment.is_a?(NewRelic::Agent::Transaction::DatastoreSegment)

  if current_segment.sql_statement
    current_segment.sql_statement.append_sql(sql)
  else
    current_segment._notice_sql(sql, self.opts, explainer_for(sql))
  end
end