Module: OpenTelemetry::Instrumentation::Trilogy::Patches::Client

Defined in:
lib/opentelemetry/instrumentation/trilogy/patches/client.rb

Overview

Module to prepend to Trilogy for instrumentation

Constant Summary collapse

QUERY_NAMES =

rubocop:disable Metrics/ModuleLength

[
  'set names',
  'select',
  'insert',
  'update',
  'delete',
  'begin',
  'commit',
  'rollback',
  'savepoint',
  'release savepoint',
  'explain',
  'drop database',
  'drop table',
  'create database',
  'create table'
].freeze
QUERY_NAME_RE =
Regexp.new("^(#{QUERY_NAMES.join('|')})", Regexp::IGNORECASE)
COMPONENTS_REGEX_MAP =
{
  single_quotes: /'(?:[^']|'')*?(?:\\'.*|'(?!'))/,
  double_quotes: /"(?:[^"]|"")*?(?:\\".*|"(?!"))/,
  numeric_literals: /-?\b(?:[0-9]+\.)?[0-9]+([eE][+-]?[0-9]+)?\b/,
  boolean_literals: /\b(?:true|false|null)\b/i,
  hexadecimal_literals: /0x[0-9a-fA-F]+/,
  comments: /(?:#|--).*?(?=\r|\n|$)/i,
  multi_line_comments: %r{\/\*(?:[^\/]|\/[^*])*?(?:\*\/|\/\*.*)}
}.freeze
MYSQL_COMPONENTS =
%i[
  single_quotes
  double_quotes
  numeric_literals
  boolean_literals
  hexadecimal_literals
  comments
  multi_line_comments
].freeze
FULL_SQL_REGEXP =
Regexp.union(MYSQL_COMPONENTS.map { |component| COMPONENTS_REGEX_MAP[component] })

Instance Method Summary collapse

Instance Method Details

#initialize(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/opentelemetry/instrumentation/trilogy/patches/client.rb', line 51

def initialize(options = {})
  @connection_options = options # This is normally done by Trilogy#initialize

  tracer.in_span(
    'connect',
    attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
    kind: :client
  ) do
    super
  end
end

#pingObject



63
64
65
66
67
68
69
70
71
# File 'lib/opentelemetry/instrumentation/trilogy/patches/client.rb', line 63

def ping(...)
  tracer.in_span(
    'ping',
    attributes: client_attributes.merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
    kind: :client
  ) do
    super
  end
end

#query(sql) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/opentelemetry/instrumentation/trilogy/patches/client.rb', line 73

def query(sql)
  tracer.in_span(
    database_span_name(sql),
    attributes: client_attributes(sql).merge!(OpenTelemetry::Instrumentation::Trilogy.attributes),
    kind: :client
  ) do
    super(sql)
  end
end