module Sequel::OneApmInstrumentation

Constants

THREAD_SAFE_CONNECTION_POOL_CLASSES

Public Instance Methods

generate_metrics(operation, config = {}) click to toggle source
# File lib/sequel/extensions/oneapm_instrumentation.rb, line 32
def generate_metrics(operation, config = {})
  @product ||= database_info(config)
  metric_for(@product, operation).compact
end
log_yield(sql, args=nil) click to toggle source

Instrument all queries that go through execute_query.

Calls superclass method
# File lib/sequel/extensions/oneapm_instrumentation.rb, line 13
def log_yield(sql, args=nil)
  state = OneApm::TransactionState.tl_get
  return super unless state.is_execution_traced?

  t0 = Time.now
  rval = super
  t1 = Time.now

  begin
    duration = t1 - t0
    record_metrics(sql, args, duration)
    notice_sql(state, sql, args, t0, t1)
  rescue => err
    OneApm::Manager.logger.debug "while recording metrics for Sequel", err
  end

  return rval
end
notice_sql(state, sql, args, start, finish) click to toggle source

Record the given sql within a new frame, using the given start and finish times.

# File lib/sequel/extensions/oneapm_instrumentation.rb, line 52
def notice_sql(state, sql, args, start, finish)
  operation = operator_metric_for(sql, args)
  base, *other_metrics = generate_metrics(operation, self.opts)
  agent    = OneApm::Manager.agent
  duration = finish - start
  stack    = state.traced_method_stack

  begin
    frame = stack.push_frame(state, :sequel, start)
    explainer = Proc.new do |*|
      if THREAD_SAFE_CONNECTION_POOL_CLASSES.include?(self.pool.class)
        self[ sql ].explain
      else
        OneApm::Manager.logger.log_once(:info, :sequel_explain_skipped, "Not running SQL explains because Sequel is not in recognized multi-threaded mode")
        nil
      end
    end
    agent.transaction_sampler.notice_sql(sql, self.opts, duration, state, &explainer)
    agent.sql_sampler.notice_sql(sql, base, self.opts, duration, state, &explainer)
  ensure
    stack.pop_frame(state, frame, metric, finish)
  end
end
operator_metric_for(sql, _) click to toggle source
# File lib/sequel/extensions/oneapm_instrumentation.rb, line 82
def operator_metric_for(sql, _)
  return operator_for_sql(OneApm::Helper.correctly_encoded(sql))
end
primary_metric_for(sql, _) click to toggle source

Derive a primary database metric for the specified sql.

# File lib/sequel/extensions/oneapm_instrumentation.rb, line 78
def primary_metric_for(sql, _)
  return metric_for_sql(OneApm::Helper.correctly_encoded(sql))
end
record_metrics(sql, args, duration) click to toggle source

Record metrics for the specified sql and args using the specified duration.

# File lib/sequel/extensions/oneapm_instrumentation.rb, line 39
def record_metrics(sql, args, duration)
  operation = operator_metric_for(sql, args)
  engine = OneApm::Manager.agent.stats_engine
  base, *other_metrics = generate_metrics(operation, self.opts)
  engine.tl_record_scoped_and_unscoped_metrics(base, other_metrics, duration)
end