Module: Mysql2::Instrumentation
- Defined in:
- lib/mysql2/instrumentation.rb,
lib/mysql2/instrumentation/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.tracer ⇒ Object
Returns the value of attribute tracer.
Class Method Summary collapse
Class Attribute Details
.tracer ⇒ Object
Returns the value of attribute tracer.
9 10 11 |
# File 'lib/mysql2/instrumentation.rb', line 9 def tracer @tracer end |
Class Method Details
.instrument(tracer: OpenTracing.global_tracer) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mysql2/instrumentation.rb', line 11 def instrument(tracer: OpenTracing.global_tracer) begin require 'mysql2' rescue LoadError return end @tracer = tracer patch_query unless @instrumented @instrumented = true end |
.patch_query ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mysql2/instrumentation.rb', line 25 def patch_query ::Mysql2::Client.class_eval do alias_method :query_original, :query def query(sql, = {}) = { 'component' => 'mysql2', 'db.instance' => .fetch(:database, ''), 'db.statement' => sql, 'db.user' => .fetch(:username, ''), 'db.type' => 'mysql', 'span.kind' => 'client', } operation_name = 'sql.query' begin candidate = sql.split(' ')[0] unless candidate == nil or candidate.empty? operation_name = candidate end rescue end span = ::Mysql2::Instrumentation.tracer.start_span(operation_name, tags: ) query_original(sql, ) rescue => error span.set_tag("error", true) span.log_kv(key: "message", value: error.) raise error ensure span.finish if span end end # class_eval end |