From 1d992f1eaf6ea047ee00a61c2f3712ec4f37816f Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Fri, 1 Nov 2024 15:31:56 -0400 Subject: [PATCH 1/2] feat: create span for mysql2 execute --- .../instrumentation/mysql2/instrumentation.rb | 2 + .../mysql2/patches/statement.rb | 44 +++++++++++++++++++ .../mysql2/instrumentation_test.rb | 21 +++++++++ 3 files changed, 67 insertions(+) create mode 100644 instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb 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..0ec8ff52d --- /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..58a4b8cf6 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 From b0da5a841e2674b99cd696e2ee9fcd9b895cbe0d Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Mon, 4 Nov 2024 11:38:38 -0500 Subject: [PATCH 2/2] lint --- .../opentelemetry/instrumentation/mysql2/patches/statement.rb | 2 +- .../instrumentation/mysql2/instrumentation_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb index 0ec8ff52d..088fbbfa2 100644 --- a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2/patches/statement.rb @@ -24,7 +24,7 @@ def execute(*args, **kwargs) def _otel_execute_attributes(args, kwargs) if config[:db_statement] == :include - {'args' => args.to_s, 'kwargs' => kwargs.to_s} + { 'args' => args.to_s, 'kwargs' => kwargs.to_s } else {} end diff --git a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb index 58a4b8cf6..3e7e75aa8 100644 --- a/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb +++ b/instrumentation/mysql2/test/opentelemetry/instrumentation/mysql2/instrumentation_test.rb @@ -139,7 +139,7 @@ stmt = client.prepare('SELECT ?') args = ['abc'] - kwargs = {'foo' => 'bar'} + kwargs = { 'foo' => 'bar' } stmt.execute(*args, **kwargs) finished_spans = exporter.finished_spans