Module: Sequel::NewRelicInstrumentation

Defined in:
lib/sequel/extensions/new_relic_instrumentation.rb

Overview

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.

Defined Under Namespace

Modules: Naming

Constant Summary collapse

THREAD_SAFE_CONNECTION_POOL_CLASSES =
[
  (defined?(::Sequel::ThreadedConnectionPool) && ::Sequel::ThreadedConnectionPool),
  (defined?(::Sequel::TimedQueueConnectionPool) && RUBY_VERSION >= '3.2' && ::Sequel::TimedQueueConnectionPool)
].compact.freeze

Instance Method Summary collapse

Instance Method Details

#explainer_for(sql) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/sequel/extensions/new_relic_instrumentation.rb', line 85

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) ⇒ Object

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.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sequel/extensions/new_relic_instrumentation.rb', line 67

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