Skip to content

Commit

Permalink
feat: All V3 services emit traces
Browse files Browse the repository at this point in the history
  • Loading branch information
jterapin committed Sep 5, 2024
1 parent f817d6b commit 7fb750c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module AwsSdk
# Instrumentation class that detects and installs the AwsSdk instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('2.0.0')
CURRENT_MAJOR_VERSION = Gem::Version.new('3.0.0')

install do |_config|
require_dependencies
add_plugin(Seahorse::Client::Base, *loaded_constants)
add_plugins(Seahorse::Client::Base, *loaded_constants)
end

present do
Expand Down Expand Up @@ -46,14 +47,42 @@ def require_dependencies
require_relative 'messaging_helper'
end

def add_plugin(*targets)
def add_plugins(*targets)
targets.each { |klass| klass.add_plugin(AwsSdk::Plugin) }
end

def loaded_constants
if gem_version >= CURRENT_MAJOR_VERSION
load_v3_constants
else
load_legacy_constants
end
end

def load_v3_constants
::Aws.constants.each_with_object([]) do |c, constants|
m = ::Aws.const_get(c)
next unless unloaded_service?(c, m)

begin
constants << m.const_get(:Client)
rescue StandardError => e
OpenTelemetry.logger.warn("Constant could not be loaded: #{e}")
end
end
end

def unloaded_service?(constant, service_module)
!::Aws.autoload?(constant) &&
service_module.is_a?(Module) &&
service_module.const_defined?(:Client) &&
(service_module.const_get(:Client).superclass == Seahorse::Client::Base)
end

def load_legacy_constants
# Cross-check services against loaded AWS constants
# Module#const_get can return a constant from ancestors when there's a miss.
# If this conincidentally matches another constant, it will attempt to patch
# If this coincidentally matches another constant, it will attempt to patch
# the wrong constant, resulting in patch failure.
available_services = ::Aws.constants & SERVICES.map(&:to_sym)
available_services.each_with_object([]) do |service, constants|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ Gem::Specification.new do |spec|
spec.license = 'Apache-2.0'

spec.files = Dir.glob('lib/**/*.rb') +
Dir.glob('*.md') +
['LICENSE', '.yardopts']
Dir.glob('*.md') +
['LICENSE', '.yardopts']
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.22.1'

spec.add_development_dependency 'appraisal', '~> 2.5'
spec.add_development_dependency 'aws-sdk-dynamodb'
spec.add_development_dependency 'aws-sdk-s3'
spec.add_development_dependency 'aws-sdk-sns'
spec.add_development_dependency 'aws-sdk-sqs'
spec.add_development_dependency 'bundler', '~> 2.4'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'opentelemetry-sdk', '~> 1.1'
Expand Down

0 comments on commit 7fb750c

Please sign in to comment.