Module: Sequel::OneApmInstrumentation

Includes:
OneApm::Agent::Instrumentation::ActiveRecordHelper, OneApm::Support::MethodTracer
Defined in:
lib/sequel/extensions/oneapm_instrumentation.rb

Constant Summary collapse

THREAD_SAFE_CONNECTION_POOL_CLASSES =
[
  (defined?(::Sequel::ThreadedConnectionPool) && ::Sequel::ThreadedConnectionPool),
].compact.freeze

Constants included from OneApm::Support::MethodTracer::ClassMethods::AddMethodTracer

OneApm::Support::MethodTracer::ClassMethods::AddMethodTracer::ALLOWED_KEYS, OneApm::Support::MethodTracer::ClassMethods::AddMethodTracer::DEPRECATED_KEYS, OneApm::Support::MethodTracer::ClassMethods::AddMethodTracer::OA_DEFAULT_SETTINGS

Constants included from OneApm::Agent::Instrumentation::ActiveRecordHelper

OneApm::Agent::Instrumentation::ActiveRecordHelper::ALL, OneApm::Agent::Instrumentation::ActiveRecordHelper::ALLOTHER, OneApm::Agent::Instrumentation::ActiveRecordHelper::ALLWEB, OneApm::Agent::Instrumentation::ActiveRecordHelper::DATABASE, OneApm::Agent::Instrumentation::ActiveRecordHelper::STATEMENT

Instance Method Summary collapse

Methods included from OneApm::Support::MethodTracer

extended, included, #trace_execution_scoped, #trace_execution_unscoped

Methods included from OneApm::Support::MethodTracer::ClassMethods

#add_method_tracer, #remove_method_tracer

Methods included from OneApm::Support::MethodTracer::ClassMethods::AddMethodTracer

#assemble_code_header, #check_for_illegal_keys!, #check_for_push_scope_and_metric, #code_to_eval, #default_metric_name_code, #method_with_push_scope, #method_without_push_scope, #oneapm_method_exists?, #traced_method_exists?, #validate_options

Methods included from OneApm::Agent::Instrumentation::ActiveRecordHelper

all, database_info, database_type, metric_for, model_for_name, operation_all_metric_for, operation_metric_for, operator_for_name, operator_for_sql, product_rollup, rename_for, rollup_metrics_for, type_and_default_port_for_db, without_database_name

Instance Method Details

#generate_metrics(operation, config = {}) ⇒ Object



32
33
34
35
# 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) ⇒ Object

Instrument all queries that go through #execute_query.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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, _) ⇒ Object



82
83
84
# 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, _) ⇒ Object

Derive a primary database metric for the specified sql.



78
79
80
# 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) ⇒ Object

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



39
40
41
42
43
44
# 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