diff --git a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2.rb b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2.rb index 186523915..a10bcc1d6 100644 --- a/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2.rb +++ b/instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2.rb @@ -6,50 +6,13 @@ require 'opentelemetry' require 'opentelemetry-instrumentation-base' +require 'opentelemetry-helpers-sql' module OpenTelemetry module Instrumentation # Contains the OpenTelemetry instrumentation for the Mysql2 gem module Mysql2 - extend self - - CURRENT_ATTRIBUTES_KEY = Context.create_key('mysql-attributes-hash') - - private_constant :CURRENT_ATTRIBUTES_KEY - - # Returns the attributes hash representing the Mysql2 context found - # in the optional context or the current context if none is provided. - # - # @param context [optional Context] The context to lookup the current - # attributes hash. Defaults to Context.current - def attributes(context = nil) - context ||= Context.current - context.value(CURRENT_ATTRIBUTES_KEY) || {} - end - - # @param attributes_hash [Hash] The attributes to add to the context - # @param parent_context [optional Context] The context to use as the parent for - # the returned context - # @return A context containing the merged attributes hash, derived from the - # optional parent context, or the current context if one was not provided. - def context_with_attributes(attributes_hash, parent_context: Context.current) - attributes_hash = attributes(parent_context).merge(attributes_hash) - parent_context.set_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) - end - - # Activates/deactivates the merged attributes hash within the current Context, - # which makes the "current attributes hash" available implicitly. - # - # On exit, the attributes hash that was active before calling this method - # will be reactivated. - # - # @param [Span] span the span to activate - # @yield [Hash, Context] yields attributes hash and a context containing the - # attributes hash to the block. - def with_attributes(attributes_hash) - attributes_hash = attributes.merge(attributes_hash) - Context.with_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) { |c, h| yield h, c } - end + extend ::OpenTelemetry::Helpers::Sql end end end diff --git a/instrumentation/pg/Gemfile b/instrumentation/pg/Gemfile index 6c8072b49..5b86b1544 100644 --- a/instrumentation/pg/Gemfile +++ b/instrumentation/pg/Gemfile @@ -10,5 +10,6 @@ gemspec group :test do gem 'opentelemetry-instrumentation-base', path: '../base' + gem 'opentelemetry-helpers-sql', path: '../../helpers/sql' gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation' end diff --git a/instrumentation/pg/lib/opentelemetry/instrumentation/pg.rb b/instrumentation/pg/lib/opentelemetry/instrumentation/pg.rb index 43b9263c0..72b2d3e37 100644 --- a/instrumentation/pg/lib/opentelemetry/instrumentation/pg.rb +++ b/instrumentation/pg/lib/opentelemetry/instrumentation/pg.rb @@ -6,40 +6,13 @@ require 'opentelemetry' require 'opentelemetry-instrumentation-base' +require 'opentelemetry-helpers-sql' module OpenTelemetry module Instrumentation # Contains the OpenTelemetry instrumentation for the Pg gem module PG - extend self - - CURRENT_ATTRIBUTES_KEY = Context.create_key('pg-attributes-hash') - - private_constant :CURRENT_ATTRIBUTES_KEY - - # Returns the attributes hash representing the postgres client context found - # in the optional context or the current context if none is provided. - # - # @param [optional Context] context The context to lookup the current - # attributes hash. Defaults to Context.current - def attributes(context = nil) - context ||= Context.current - context.value(CURRENT_ATTRIBUTES_KEY) || {} - end - - # Activates/deactivates the merged attributes hash within the current Context, - # which makes the "current attributes hash" available implicitly. - # - # On exit, the attributes hash that was active before calling this method - # will be reactivated. - # - # @param [Span] span the span to activate - # @yield [Hash, Context] yields attributes hash and a context containing the - # attributes hash to the block. - def with_attributes(attributes_hash) - attributes_hash = attributes.merge(attributes_hash) - Context.with_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) { |c, h| yield h, c } - end + extend ::OpenTelemetry::Helpers::Sql end end end