-
Notifications
You must be signed in to change notification settings - Fork 205
Feat: Add Mysql2 and Trilogy db.collection.name
attribute
#1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
43d24ee
8bd5818
146bd71
f9c244e
6047052
cc43b33
b61e51e
bc7df18
a23b954
f099af3
8134ee3
8328668
8f42fa2
e73b23a
46616da
4af3e2c
d6ffcdf
d9f1975
b8d5a96
68f42ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ module Mysql2 | |
module Patches | ||
# Module to prepend to Mysql2::Client for instrumentation | ||
module Client | ||
# Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands | ||
TABLE_NAME = /\b(?:(?:FROM|INTO|UPDATE)|(?:(?:CREATE|DROP|ALTER)\s+TABLE(?:\s+IF\s+(?:NOT\s+)?EXISTS)?))\s+["]?([\w.]+)["]?/i | ||
|
||
def query(sql, options = {}) | ||
tracer.in_span( | ||
_otel_span_name(sql), | ||
|
@@ -47,15 +50,20 @@ def _otel_span_name(sql) | |
end | ||
|
||
def _otel_span_attributes(sql) | ||
attributes = _otel_client_attributes | ||
case config[:db_statement] | ||
when :include | ||
attributes[SemanticConventions::Trace::DB_STATEMENT] = sql | ||
when :obfuscate | ||
attributes[SemanticConventions::Trace::DB_STATEMENT] = | ||
OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql( | ||
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql | ||
) | ||
attributes = _otel_client_attributes(sql) | ||
|
||
if sql | ||
attributes[SemanticConventions::Trace::DB_SQL_TABLE] = db_sql_table_name(sql) if db_sql_table_name(sql) && config[:db_sql_table] == :include | ||
|
||
case config[:db_statement] | ||
when :include | ||
attributes[SemanticConventions::Trace::DB_STATEMENT] = sql | ||
when :obfuscate | ||
attributes[SemanticConventions::Trace::DB_STATEMENT] = | ||
OpenTelemetry::Helpers::SqlObfuscation.obfuscate_sql( | ||
sql, obfuscation_limit: config[:obfuscation_limit], adapter: :mysql | ||
) | ||
end | ||
end | ||
|
||
attributes.merge!(OpenTelemetry::Instrumentation::Mysql2.attributes) | ||
|
@@ -68,7 +76,7 @@ def _otel_database_name | |
(query_options[:database] || query_options[:dbname] || query_options[:db])&.to_s | ||
end | ||
|
||
def _otel_client_attributes | ||
def _otel_client_attributes(sql) | ||
# The client specific attributes can be found via the query_options instance variable | ||
# exposed on the mysql2 Client | ||
# https://github.com/brianmario/mysql2/blob/ca08712c6c8ea672df658bb25b931fea22555f27/lib/mysql2/client.rb#L25-L26 | ||
|
@@ -83,9 +91,17 @@ def _otel_client_attributes | |
|
||
attributes[SemanticConventions::Trace::DB_NAME] = _otel_database_name | ||
attributes[SemanticConventions::Trace::PEER_SERVICE] = config[:peer_service] | ||
|
||
attributes | ||
end | ||
|
||
def db_sql_table_name(sql) | ||
Regexp.last_match(1) if sql =~ TABLE_NAME | ||
rescue StandardError | ||
OpenTelemetry.handle_error(message: 'Error extracting collection name', exception: e) | ||
nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an error occurs and the attribute is set to Please ensure that the attribute is not set in cases where the table name could not be extracted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made some changes! Will log an error if trouble getting the table name and won't report. Is the error logging here okay? 4af3e2c |
||
end | ||
|
||
def tracer | ||
Mysql2::Instrumentation.instance.tracer | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
[ | ||
{ | ||
"name": "from", | ||
"sql": "SELECT * FROM test_table" | ||
}, | ||
{ | ||
"name": "select_count_from", | ||
"sql": "SELECT COUNT(*) FROM test_table WHERE condition" | ||
}, | ||
{ | ||
"name": "from_with_subquery", | ||
"sql": "SELECT * FROM (SELECT * FROM test_table) AS table_alias" | ||
}, | ||
{ | ||
"name": "insert_into", | ||
"sql": "INSERT INTO test_table (column1, column2) VALUES (value1, value2)" | ||
}, | ||
{ | ||
"name": "update", | ||
"sql": "UPDATE test_table SET column1 = value1 WHERE condition" | ||
}, | ||
{ | ||
"name": "delete_from", | ||
"sql": "DELETE FROM test_table WHERE condition" | ||
}, | ||
{ | ||
"name": "create_table", | ||
"sql": "CREATE TABLE test_table (column1 datatype, column2 datatype)" | ||
}, | ||
{ | ||
"name": "create_table_if_not_exists", | ||
"sql": "CREATE TABLE IF NOT EXISTS test_table (column1 datatype, column2 datatype)" | ||
}, | ||
{ | ||
"name": "alter_table", | ||
"sql": "ALTER TABLE test_table ADD column_name datatype" | ||
}, | ||
{ | ||
"name": "drop_table", | ||
"sql": "DROP TABLE test_table" | ||
}, | ||
{ | ||
"name": "drop_table_if_exists", | ||
"sql": "DROP TABLE IF EXISTS test_table" | ||
}, | ||
{ | ||
"name": "insert_into", | ||
"sql": "INSERT INTO test_table values('', 'a''b c',0, 1 , 'd''e f''s h')" | ||
}, | ||
{ | ||
"name": "from_with_join", | ||
"sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column" | ||
}, | ||
{ | ||
"name": "table_name_with_double_quotes", | ||
"sql": "SELECT columns FROM \"test_table\"" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
[ | ||
{ | ||
"name": "from", | ||
"sql": "SELECT * FROM test_table" | ||
}, | ||
{ | ||
"name": "select_count_from", | ||
"sql": "SELECT COUNT(*) FROM test_table WHERE condition" | ||
}, | ||
{ | ||
"name": "from_with_subquery", | ||
"sql": "SELECT * FROM (SELECT * FROM test_table) AS table_alias" | ||
}, | ||
{ | ||
"name": "insert_into", | ||
"sql": "INSERT INTO test_table (column1, column2) VALUES (value1, value2)" | ||
}, | ||
{ | ||
"name": "update", | ||
"sql": "UPDATE test_table SET column1 = value1 WHERE condition" | ||
}, | ||
{ | ||
"name": "delete_from", | ||
"sql": "DELETE FROM test_table WHERE condition" | ||
}, | ||
{ | ||
"name": "create_table", | ||
"sql": "CREATE TABLE test_table (column1 datatype, column2 datatype)" | ||
}, | ||
{ | ||
"name": "create_table_if_not_exists", | ||
"sql": "CREATE TABLE IF NOT EXISTS test_table (column1 datatype, column2 datatype)" | ||
}, | ||
{ | ||
"name": "alter_table", | ||
"sql": "ALTER TABLE test_table ADD column_name datatype" | ||
}, | ||
{ | ||
"name": "drop_table", | ||
"sql": "DROP TABLE test_table" | ||
}, | ||
{ | ||
"name": "drop_table_if_exists", | ||
"sql": "DROP TABLE IF EXISTS test_table" | ||
}, | ||
{ | ||
"name": "insert_into", | ||
"sql": "INSERT INTO test_table values('', 'a''b c',0, 1 , 'd''e f''s h')" | ||
}, | ||
{ | ||
"name": "from_with_join", | ||
"sql": "SELECT columns FROM test_table JOIN table2 ON test_table.column = table2.column" | ||
}, | ||
{ | ||
"name": "table_name_with_double_quotes", | ||
"sql": "SELECT columns FROM \"test_table\"" | ||
} | ||
] |
Uh oh!
There was an error while loading. Please reload this page.