Skip to content

Commit 21acdab

Browse files
refactor: Use SQL helpers (#1271)
1 parent a08467a commit 21acdab

File tree

10 files changed

+13
-108
lines changed

10 files changed

+13
-108
lines changed

instrumentation/mysql2/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gemspec
1010

1111
group :test, :development do
1212
gem 'opentelemetry-instrumentation-base', path: '../base'
13+
gem 'opentelemetry-helpers-sql', path: '../../helpers/sql'
1314
gem 'opentelemetry-helpers-mysql', path: '../../helpers/mysql'
1415
gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation'
1516
end

instrumentation/mysql2/lib/opentelemetry/instrumentation/mysql2.rb

+2-39
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,13 @@
66

77
require 'opentelemetry'
88
require 'opentelemetry-instrumentation-base'
9+
require 'opentelemetry-helpers-sql'
910

1011
module OpenTelemetry
1112
module Instrumentation
1213
# Contains the OpenTelemetry instrumentation for the Mysql2 gem
1314
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
5316
end
5417
end
5518
end

instrumentation/mysql2/opentelemetry-instrumentation-mysql2.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727

2828
spec.add_dependency 'opentelemetry-api', '~> 1.0'
2929
spec.add_dependency 'opentelemetry-helpers-mysql'
30+
spec.add_dependency 'opentelemetry-helpers-sql'
3031
spec.add_dependency 'opentelemetry-helpers-sql-obfuscation'
3132
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.23.0'
3233

instrumentation/pg/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ gemspec
1111
group :test do
1212
gem 'activerecord', '>= 7.0.0'
1313
gem 'opentelemetry-instrumentation-base', path: '../base'
14+
gem 'opentelemetry-helpers-sql', path: '../../helpers/sql'
1415
gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation'
1516
end

instrumentation/pg/lib/opentelemetry/instrumentation/pg.rb

+2-29
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,13 @@
66

77
require 'opentelemetry'
88
require 'opentelemetry-instrumentation-base'
9+
require 'opentelemetry-helpers-sql'
910

1011
module OpenTelemetry
1112
module Instrumentation
1213
# Contains the OpenTelemetry instrumentation for the Pg gem
1314
module PG
14-
extend self
15-
16-
CURRENT_ATTRIBUTES_KEY = Context.create_key('pg-attributes-hash')
17-
18-
private_constant :CURRENT_ATTRIBUTES_KEY
19-
20-
# Returns the attributes hash representing the postgres client context found
21-
# in the optional context or the current context if none is provided.
22-
#
23-
# @param [optional Context] 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-
# Activates/deactivates the merged attributes hash within the current Context,
31-
# which makes the "current attributes hash" available implicitly.
32-
#
33-
# On exit, the attributes hash that was active before calling this method
34-
# will be reactivated.
35-
#
36-
# @param [Span] span the span to activate
37-
# @yield [Hash, Context] yields attributes hash and a context containing the
38-
# attributes hash to the block.
39-
def with_attributes(attributes_hash)
40-
attributes_hash = attributes.merge(attributes_hash)
41-
Context.with_value(CURRENT_ATTRIBUTES_KEY, attributes_hash) { |c, h| yield h, c }
42-
end
15+
extend ::OpenTelemetry::Helpers::Sql
4316
end
4417
end
4518
end

instrumentation/pg/opentelemetry-instrumentation-pg.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.required_ruby_version = '>= 3.1'
2727

2828
spec.add_dependency 'opentelemetry-api', '~> 1.0'
29+
spec.add_dependency 'opentelemetry-helpers-sql'
2930
spec.add_dependency 'opentelemetry-helpers-sql-obfuscation'
3031
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.23.0'
3132

instrumentation/que/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gemspec
1111
group :test do
1212
gem 'activerecord', '< 7.2.0', '> 7.0.0'
1313
gem 'pg'
14+
gem 'opentelemetry-helpers-sql', path: '../../helpers/sql'
1415
gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation'
1516
gem 'opentelemetry-instrumentation-base', path: '../base'
1617
gem 'opentelemetry-instrumentation-pg', path: '../pg'

instrumentation/trilogy/Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gemspec
1111
group :test do
1212
gem 'opentelemetry-instrumentation-base', path: '../base'
1313
gem 'opentelemetry-helpers-mysql', path: '../../helpers/mysql'
14+
gem 'opentelemetry-helpers-sql', path: '../../helpers/sql'
1415
gem 'opentelemetry-helpers-sql-obfuscation', path: '../../helpers/sql-obfuscation'
1516
gem 'opentelemetry-propagator-vitess', path: '../../propagator/vitess'
1617
end

instrumentation/trilogy/lib/opentelemetry/instrumentation/trilogy.rb

+2-39
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,13 @@
66

77
require 'opentelemetry'
88
require 'opentelemetry-instrumentation-base'
9+
require 'opentelemetry-helpers-sql'
910

1011
module OpenTelemetry
1112
module Instrumentation
1213
# Contains the OpenTelemetry instrumentation for the Trilogy gem
1314
module Trilogy
14-
extend self
15-
16-
CURRENT_ATTRIBUTES_KEY = Context.create_key('trilogy-attributes-hash')
17-
18-
private_constant :CURRENT_ATTRIBUTES_KEY
19-
20-
# Returns the attributes hash representing the Trilogy context found
21-
# in the optional context or the current context if none is provided.
22-
#
23-
# @param [optional Context] 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-
# Returns a context containing the merged attributes hash, derived from the
31-
# optional parent context, or the current context if one was not provided.
32-
#
33-
# @param [optional Context] context The context to use as the parent for
34-
# the returned context
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
5316
end
5417
end
5518
end

instrumentation/trilogy/opentelemetry-instrumentation-trilogy.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ Gem::Specification.new do |spec|
2727

2828
spec.add_dependency 'opentelemetry-api', '~> 1.0'
2929
spec.add_dependency 'opentelemetry-helpers-mysql'
30+
spec.add_dependency 'opentelemetry-helpers-sql'
3031
spec.add_dependency 'opentelemetry-helpers-sql-obfuscation'
3132
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.23.0'
3233
spec.add_dependency 'opentelemetry-semantic_conventions', '>= 1.8.0'
3334

3435
spec.add_development_dependency 'appraisal', '~> 2.5'
3536
spec.add_development_dependency 'bundler', '~> 2.4'
3637
spec.add_development_dependency 'minitest', '~> 5.0'
37-
spec.add_development_dependency 'opentelemetry-propagator-vitess', '~> 0.1'
3838
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
3939
spec.add_development_dependency 'opentelemetry-test-helpers', '~> 0.3'
4040
spec.add_development_dependency 'pry'

0 commit comments

Comments
 (0)