Module: OpenTelemetry::Instrumentation::Mysql2::Patches::Client

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

Overview

Module to prepend to Mysql2::Client for instrumentation

Constant Summary collapse

QUERY_NAMES =
[
  '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

Instance Method Summary collapse

Instance Method Details

#query(sql, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/opentelemetry/instrumentation/mysql2/patches/client.rb', line 53

def query(sql, options = {})
  attributes = client_attributes
  case config[:db_statement]
  when :include
    attributes['db.statement'] = sql
  when :obfuscate
    attributes['db.statement'] = obfuscate_sql(sql)
  end
  tracer.in_span(
    database_span_name(sql),
    attributes: attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes),
    kind: :client
  ) do
    super(sql, options)
  end
end