Module: Atatus::Spies::SequelSpy::Ext Private

Defined in:
lib/atatus/spies/sequel.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#log_connection_yield(sql, connection, args = nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/atatus/spies/sequel.rb', line 37

def log_connection_yield(sql, connection, args = nil, &block)
  unless Atatus.current_transaction
    return super(sql, connection, args, &block)
  end

  subtype = database_type.to_s

  name =
    Atatus::Spies::SequelSpy.summarizer.summarize sql

  db_name = ''
  # postgresql shows current database
  db_name = connection&.db.to_s if connection.respond_to?(:db)
  # sqlite may expose a filename
  db_name = connection&.filename.to_s if db_name == '' && connection.respond_to?(:filename)
  # fall back to adapter class name
  db_name = connection.class.to_s if db_name == ''

  context = Atatus::Span::Context.new(
    db: { statement: sql, type: 'sql', user: opts[:user] },
    service: {target: {type: subtype, name: db_name }},
    destination: { service: { resource: subtype } }
  )

  span = Atatus.start_span(
    name,
    TYPE,
    subtype: subtype,
    action: ACTION,
    context: context
  )
  super(sql, connection, args, &block).tap do |result|
    if /^(UPDATE|DELETE)/.match?(name)
      if connection.respond_to?(:changes)
        span.context.db.rows_affected = connection.changes
      elsif result.is_a?(Integer)
        span.context.db.rows_affected = result
      end
    end
  end
rescue
  span&.outcome = Span::Outcome::FAILURE
  raise
ensure
  span&.outcome ||= Span::Outcome::SUCCESS
  Atatus.end_span
end