Module: OpenTelemetry::Helpers::MySQL

Defined in:
lib/opentelemetry/helpers/mysql.rb,
lib/opentelemetry/helpers/mysql/version.rb

Overview

This module contains helpers for MySQL instrumentation libraries, like mysql2 and trilogy. It is intended for use by instrumentation developers and not to be called directly by an application.

To use this in your instrumentation, the Instrumentation class for your gem must contain configuration options for:

  • :span_name Example: option :span_name, default: :statement_type, validate: %I[statement_type db_name db_operation_and_name]

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
PREPENDED_COMMENTS_REGEX =

Ignore query names that might appear in comments prepended to the statement.

%r{(?:\/\*.*?\*\/)}m
QUERY_NAME_REGEX =
Regexp.new("^\s*(?:#{PREPENDED_COMMENTS_REGEX})?\s*\\b(#{QUERY_NAMES.join('|')})\\b.*", Regexp::IGNORECASE)
VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.database_span_name(sql, operation, database_name, config) ⇒ String

This is a span naming utility intended for use in MySQL database adapter instrumentation.

Parameters:

  • sql (String)

    The SQL statement for the span.

  • operation (String)

    The database operation.

  • database_name (String)

    The name of the database.

  • config (Hash)

    The user's configuration for the database adapter. Desired keys:

    • :span_name => A symbol describing the type of name desired. Expected options are :statement_type, :db_name, and :db_operation_and_name. A nil or unknown :span_name will return 'mysql' as the span name

Returns:

  • (String)

    The span name.



58
59
60
61
62
63
64
65
66
67
# File 'lib/opentelemetry/helpers/mysql.rb', line 58

def database_span_name(sql, operation, database_name, config)
  case config[:span_name]
  when :statement_type
    extract_statement_type(sql)
  when :db_name
    database_name
  when :db_operation_and_name
    db_operation_and_name(operation, database_name)
  end || 'mysql'
end

.db_operation_and_name(operation, database_name) ⇒ 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.



80
81
82
83
84
85
86
87
88
# File 'lib/opentelemetry/helpers/mysql.rb', line 80

def db_operation_and_name(operation, database_name)
  if operation && database_name
    "#{operation} #{database_name}"
  elsif operation
    operation
  elsif database_name
    database_name
  end
end

.extract_statement_type(sql) ⇒ 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.



70
71
72
73
74
75
76
77
# File 'lib/opentelemetry/helpers/mysql.rb', line 70

def extract_statement_type(sql)
  sql = OpenTelemetry::Common::Utilities.utf8_encode(sql, binary: true)

  QUERY_NAME_REGEX.match(sql) { |match| match[1].downcase } unless sql.nil?
rescue StandardError => e
  OpenTelemetry.handle_error(message: 'Error extracting SQL statement type', exception: e)
  nil
end