|
6 | 6 |
|
7 | 7 | require 'opentelemetry' |
8 | 8 | require 'opentelemetry-instrumentation-base' |
| 9 | +require 'opentelemetry-helpers-sql' |
9 | 10 |
|
10 | 11 | module OpenTelemetry |
11 | 12 | module Instrumentation |
12 | 13 | # Contains the OpenTelemetry instrumentation for the Mysql2 gem |
13 | 14 | module Mysql2 |
14 | | - extend self |
15 | | - |
16 | | - CURRENT_ATTRIBUTES_KEY = Context.create_key('mysql-attributes-hash') |
17 | | - |
18 | | - private_constant :CURRENT_ATTRIBUTES_KEY |
19 | | - |
20 | | - # Returns the attributes hash representing the Mysql2 context found |
21 | | - # in the optional context or the current context if none is provided. |
22 | | - # |
23 | | - # @param context [optional Context] The context to lookup the current |
24 | | - # attributes hash. Defaults to Context.current |
25 | | - def attributes(context = nil) |
26 | | - context ||= Context.current |
27 | | - context.value(CURRENT_ATTRIBUTES_KEY) || {} |
28 | | - end |
29 | | - |
30 | | - # @param attributes_hash [Hash] The attributes to add to the context |
31 | | - # @param parent_context [optional Context] The context to use as the parent for |
32 | | - # the returned context |
33 | | - # @return A context containing the merged attributes hash, derived from the |
34 | | - # optional parent context, or the current context if one was not provided. |
35 | | - def context_with_attributes(attributes_hash, parent_context: Context.current) |
36 | | - attributes_hash = attributes(parent_context).merge(attributes_hash) |
37 | | - parent_context.set_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) |
38 | | - end |
39 | | - |
40 | | - # Activates/deactivates the merged attributes hash within the current Context, |
41 | | - # which makes the "current attributes hash" available implicitly. |
42 | | - # |
43 | | - # On exit, the attributes hash that was active before calling this method |
44 | | - # will be reactivated. |
45 | | - # |
46 | | - # @param [Span] span the span to activate |
47 | | - # @yield [Hash, Context] yields attributes hash and a context containing the |
48 | | - # attributes hash to the block. |
49 | | - def with_attributes(attributes_hash) |
50 | | - attributes_hash = attributes.merge(attributes_hash) |
51 | | - Context.with_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) { |c, h| yield h, c } |
52 | | - end |
| 15 | + extend ::OpenTelemetry::Helpers::Sql |
53 | 16 | end |
54 | 17 | end |
55 | 18 | end |
|
0 commit comments