Skip to content
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

feat: create span for mysql2 execute #1221

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading