Module: TraceView::Inst::Sequel

Included in:
SequelDatabase, SequelDataset
Defined in:
lib/traceview/inst/sequel.rb

Overview

TraceView::Inst::Sequel

The common (shared) methods used by the TraceView Sequel instrumentation across multiple modules/classes.

Instance Method Summary collapse

Instance Method Details

#exec_with_traceview(method, sql, opts = ::Sequel::OPTS, &block) ⇒ Object

exec_with_traceview

This method wraps and routes the call to the specified original method call



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/traceview/inst/sequel.rb', line 70

def exec_with_traceview(method, sql, opts = ::Sequel::OPTS, &block)
  kvs = extract_trace_details(sql, opts)

  TraceView::API.log_entry(:sequel, kvs)

  send(method, sql, opts, &block)
rescue => e
  TraceView::API.log_exception(:sequel, e)
  raise e
ensure
  TraceView::API.log_exit(:sequel)
end

#extract_trace_details(sql, opts) ⇒ Object

extract_trace_details

Given SQL and the options hash, this method extracts the interesting bits for reporting to the TraceView dashboard.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/traceview/inst/sequel.rb', line 19

def extract_trace_details(sql, opts)
  kvs = {}

  if !sql.is_a?(String)
    kvs[:IsPreparedStatement] = true
  end

  if ::Sequel::VERSION > '4.36.0' && !sql.is_a?(String)
    # In 4.37.0, sql was converted to a prepared statement object
    sql = sql.prepared_sql unless sql.is_a?(Symbol)
  end

  if TraceView::Config[:sanitize_sql]
    # Sanitize SQL and don't report binds
    if sql.is_a?(Symbol)
      kvs[:Query] = sql
    else
      kvs[:Query] = TraceView::Util.sanitize_sql(sql)
    end
  else
    # Report raw SQL and any binds if they exist
    kvs[:Query] = sql.to_s
    kvs[:QueryArgs] = opts[:arguments] if opts.is_a?(Hash) && opts.key?(:arguments)
  end

  kvs[:Backtrace] = TraceView::API.backtrace if TraceView::Config[:sequel][:collect_backtraces]

  if ::Sequel::VERSION < '3.41.0' && !(self.class.to_s =~ /Dataset$/)
    db_opts = @opts
  elsif @pool
    db_opts = @pool.db.opts
  else
    db_opts = @db.opts
  end

  kvs[:Database]   = db_opts[:database]
  kvs[:RemoteHost] = db_opts[:host]
  kvs[:RemotePort] = db_opts[:port] if db_opts.key?(:port)
  kvs[:Flavor]     = db_opts[:adapter]
rescue => e
  TraceView.logger.debug "[traceview/debug Error capturing Sequel KVs: #{e.message}" if TraceView::Config[:verbose]
ensure
  return kvs
end