File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
lib/opentelemetry/instrumentation/mysql2
test/opentelemetry/instrumentation/mysql2 Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -28,10 +28,12 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2828
2929 def require_dependencies
3030 require_relative 'patches/client'
31+ require_relative 'patches/statement'
3132 end
3233
3334 def patch_client
3435 ::Mysql2 ::Client . prepend ( Patches ::Client )
36+ ::Mysql2 ::Statement . prepend ( Patches ::Statement )
3537 end
3638 end
3739 end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ # Copyright The OpenTelemetry Authors
4+ #
5+ # SPDX-License-Identifier: Apache-2.0
6+
7+ module OpenTelemetry
8+ module Instrumentation
9+ module Mysql2
10+ module Patches
11+ # Module to prepend to Mysql2::Client for instrumentation
12+ module Statement
13+ def execute ( *args , **kwargs )
14+ tracer . in_span (
15+ 'execute' ,
16+ attributes : _otel_execute_attributes ( args , kwargs ) ,
17+ kind : :client
18+ ) do
19+ super
20+ end
21+ end
22+
23+ private
24+
25+ def _otel_execute_attributes ( args , kwargs )
26+ if config [ :db_statement ] == :include
27+ { 'args' => args . to_s , 'kwargs' => kwargs . to_s }
28+ else
29+ { }
30+ end
31+ end
32+
33+ def tracer
34+ Mysql2 ::Instrumentation . instance . tracer
35+ end
36+
37+ def config
38+ Mysql2 ::Instrumentation . instance . config
39+ end
40+ end
41+ end
42+ end
43+ end
44+ end
Original file line number Diff line number Diff line change 133133
134134 _ ( span . events [ 0 ] . attributes [ 'exception.message' ] . slice ( 0 , 37 ) ) . must_equal 'You have an error in your SQL syntax;'
135135 end
136+
137+ describe 'execute statement' do
138+ it 'simple execute statement' do
139+ stmt = client . prepare ( 'SELECT ?' )
140+
141+ args = [ 'abc' ]
142+ kwargs = { 'foo' => 'bar' }
143+
144+ stmt . execute ( *args , **kwargs )
145+ finished_spans = exporter . finished_spans
146+
147+ _ ( finished_spans [ 0 ] . name ) . must_equal 'select'
148+ _ ( finished_spans [ 0 ] . attributes [ 'db.system' ] ) . must_equal 'mysql'
149+ _ ( finished_spans [ 0 ] . attributes [ 'db.name' ] ) . must_equal 'mysql'
150+ _ ( finished_spans [ 0 ] . attributes [ 'db.statement' ] ) . must_equal 'SELECT ?'
151+
152+ _ ( finished_spans [ 1 ] . name ) . must_equal 'execute'
153+ _ ( finished_spans [ 1 ] . attributes [ 'args' ] ) . must_equal '["abc"]'
154+ _ ( finished_spans [ 1 ] . attributes [ 'kwargs' ] ) . must_equal '{"foo"=>"bar"}'
155+ end
156+ end
136157 end
137158
138159 it 'after requests' do
You can’t perform that action at this time.
0 commit comments