diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb index 9a7b78ccb..0f0a939f2 100644 --- a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/instrumentation.rb @@ -28,10 +28,12 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base def require_dependencies require_relative 'patches/client' + require_relative 'patches/statement' end def patch_client ::Mysql2::Client.prepend(Patches::Client) + ::Mysql2::Statement.prepend(Patches::Statement) end end end diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb new file mode 100644 index 000000000..088fbbfa2 --- /dev/null +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# Copyright The OpenTelemetry Authors +# +# SPDX-License-Identifier: Apache-2.0 + +module OpenTelemetry + module Instrumentation + module Mysql2 + module Patches + # Module to prepend to Mysql2::Client for instrumentation + module Statement + def execute(*args, **kwargs) + tracer.in_span( + 'execute', + attributes: _otel_execute_attributes(args, kwargs), + kind: :client + ) do + super + end + end + + private + + def _otel_execute_attributes(args, kwargs) + if config[:db_statement] == :include + { 'args' => args.to_s, 'kwargs' => kwargs.to_s } + else + {} + end + end + + def tracer + Mysql2::Instrumentation.instance.tracer + end + + def config + Mysql2::Instrumentation.instance.config + end + end + end + end + end +end diff --git a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb index 405b714ee..3e7e75aa8 100644 --- a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb +++ b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb @@ -133,6 +133,27 @@ _(span.events[0].attributes['exception.message'].slice(0, 37)).must_equal 'You have an error in your SQL syntax;' end + + describe 'execute statement' do + it 'simple execute statement' do + stmt = client.prepare('SELECT ?') + + args = ['abc'] + kwargs = { 'foo' => 'bar' } + + stmt.execute(*args, **kwargs) + finished_spans = exporter.finished_spans + + _(finished_spans[0].name).must_equal 'select' + _(finished_spans[0].attributes['db.system']).must_equal 'mysql' + _(finished_spans[0].attributes['db.name']).must_equal 'mysql' + _(finished_spans[0].attributes['db.statement']).must_equal 'SELECT ?' + + _(finished_spans[1].name).must_equal 'execute' + _(finished_spans[1].attributes['args']).must_equal '["abc"]' + _(finished_spans[1].attributes['kwargs']).must_equal '{"foo"=>"bar"}' + end + end end it 'after requests' do