Skip to content

Commit a462533

Browse files
committed
moved to a nested module
1 parent c478025 commit a462533

File tree

2 files changed

+97
-97
lines changed

2 files changed

+97
-97
lines changed

opentelemetry-auto-instrumentation/lib/opentelemetry-auto-instrumentation.rb

Lines changed: 86 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,117 +6,106 @@
66

77
# OTelBundlerPatch
88
module OTelBundlerPatch
9-
# Nested module to handle OpenTelemetry initialization
10-
module Initializer
11-
# this is used for case when user particularly want to enable single instrumentation
12-
unless defined?(OTEL_INSTRUMENTATION_MAP)
13-
OTEL_INSTRUMENTATION_MAP = {
14-
'gruf' => 'OpenTelemetry::Instrumentation::Gruf',
15-
'trilogy' => 'OpenTelemetry::Instrumentation::Trilogy',
16-
'active_support' => 'OpenTelemetry::Instrumentation::ActiveSupport',
17-
'action_pack' => 'OpenTelemetry::Instrumentation::ActionPack',
18-
'active_job' => 'OpenTelemetry::Instrumentation::ActiveJob',
19-
'active_record' => 'OpenTelemetry::Instrumentation::ActiveRecord',
20-
'action_view' => 'OpenTelemetry::Instrumentation::ActionView',
21-
'action_mailer' => 'OpenTelemetry::Instrumentation::ActionMailer',
22-
'aws_sdk' => 'OpenTelemetry::Instrumentation::AwsSdk',
23-
'aws_lambda' => 'OpenTelemetry::Instrumentation::AwsLambda',
24-
'bunny' => 'OpenTelemetry::Instrumentation::Bunny',
25-
'lmdb' => 'OpenTelemetry::Instrumentation::LMDB',
26-
'http' => 'OpenTelemetry::Instrumentation::HTTP',
27-
'koala' => 'OpenTelemetry::Instrumentation::Koala',
28-
'active_model_serializers' => 'OpenTelemetry::Instrumentation::ActiveModelSerializers',
29-
'concurrent_ruby' => 'OpenTelemetry::Instrumentation::ConcurrentRuby',
30-
'dalli' => 'OpenTelemetry::Instrumentation::Dalli',
31-
'delayed_job' => 'OpenTelemetry::Instrumentation::DelayedJob',
32-
'ethon' => 'OpenTelemetry::Instrumentation::Ethon',
33-
'excon' => 'OpenTelemetry::Instrumentation::Excon',
34-
'faraday' => 'OpenTelemetry::Instrumentation::Faraday',
35-
'grape' => 'OpenTelemetry::Instrumentation::Grape',
36-
'graphql' => 'OpenTelemetry::Instrumentation::GraphQL',
37-
'http_client' => 'OpenTelemetry::Instrumentation::HttpClient',
38-
'mongo' => 'OpenTelemetry::Instrumentation::Mongo',
39-
'mysql2' => 'OpenTelemetry::Instrumentation::Mysql2',
40-
'net_http' => 'OpenTelemetry::Instrumentation::Net::HTTP',
41-
'pg' => 'OpenTelemetry::Instrumentation::PG',
42-
'que' => 'OpenTelemetry::Instrumentation::Que',
43-
'racecar' => 'OpenTelemetry::Instrumentation::Racecar',
44-
'rack' => 'OpenTelemetry::Instrumentation::Rack',
45-
'rails' => 'OpenTelemetry::Instrumentation::Rails',
46-
'rake' => 'OpenTelemetry::Instrumentation::Rake',
47-
'rdkafka' => 'OpenTelemetry::Instrumentation::Rdkafka',
48-
'redis' => 'OpenTelemetry::Instrumentation::Redis',
49-
'restclient' => 'OpenTelemetry::Instrumentation::RestClient',
50-
'resque' => 'OpenTelemetry::Instrumentation::Resque',
51-
'ruby_kafka' => 'OpenTelemetry::Instrumentation::RubyKafka',
52-
'sidekiq' => 'OpenTelemetry::Instrumentation::Sidekiq',
53-
'sinatra' => 'OpenTelemetry::Instrumentation::Sinatra'
54-
}.freeze
55-
end
56-
57-
class << self
58-
def detect_resource_from_env
59-
env = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s
60-
additional_resource = ::OpenTelemetry::SDK::Resources::Resource.create({})
61-
62-
env.split(',').each do |detector|
63-
case detector
64-
when 'container'
65-
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Container.detect) if defined? ::OpenTelemetry::Resource::Detector::Container
66-
when 'google_cloud_platform'
67-
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::GoogleCloudPlatform.detect) if defined? ::OpenTelemetry::Resource::Detector::GoogleCloudPlatform
68-
when 'azure'
69-
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Azure.detect) if defined? ::OpenTelemetry::Resource::Detector::Azure
70-
when 'aws'
71-
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::AWS.detect(%i[ec2 ecs eks lambda])) if defined? ::OpenTelemetry::Resource::Detector::AWS
72-
end
9+
# Nested module to handle OpenTelemetry initialization logic
10+
module OTelInitializer
11+
OTEL_INSTRUMENTATION_MAP = {
12+
'gruf' => 'OpenTelemetry::Instrumentation::Gruf',
13+
'trilogy' => 'OpenTelemetry::Instrumentation::Trilogy',
14+
'active_support' => 'OpenTelemetry::Instrumentation::ActiveSupport',
15+
'action_pack' => 'OpenTelemetry::Instrumentation::ActionPack',
16+
'active_job' => 'OpenTelemetry::Instrumentation::ActiveJob',
17+
'active_record' => 'OpenTelemetry::Instrumentation::ActiveRecord',
18+
'action_view' => 'OpenTelemetry::Instrumentation::ActionView',
19+
'action_mailer' => 'OpenTelemetry::Instrumentation::ActionMailer',
20+
'aws_sdk' => 'OpenTelemetry::Instrumentation::AwsSdk',
21+
'aws_lambda' => 'OpenTelemetry::Instrumentation::AwsLambda',
22+
'bunny' => 'OpenTelemetry::Instrumentation::Bunny',
23+
'lmdb' => 'OpenTelemetry::Instrumentation::LMDB',
24+
'http' => 'OpenTelemetry::Instrumentation::HTTP',
25+
'koala' => 'OpenTelemetry::Instrumentation::Koala',
26+
'active_model_serializers' => 'OpenTelemetry::Instrumentation::ActiveModelSerializers',
27+
'concurrent_ruby' => 'OpenTelemetry::Instrumentation::ConcurrentRuby',
28+
'dalli' => 'OpenTelemetry::Instrumentation::Dalli',
29+
'delayed_job' => 'OpenTelemetry::Instrumentation::DelayedJob',
30+
'ethon' => 'OpenTelemetry::Instrumentation::Ethon',
31+
'excon' => 'OpenTelemetry::Instrumentation::Excon',
32+
'faraday' => 'OpenTelemetry::Instrumentation::Faraday',
33+
'grape' => 'OpenTelemetry::Instrumentation::Grape',
34+
'graphql' => 'OpenTelemetry::Instrumentation::GraphQL',
35+
'http_client' => 'OpenTelemetry::Instrumentation::HttpClient',
36+
'mongo' => 'OpenTelemetry::Instrumentation::Mongo',
37+
'mysql2' => 'OpenTelemetry::Instrumentation::Mysql2',
38+
'net_http' => 'OpenTelemetry::Instrumentation::Net::HTTP',
39+
'pg' => 'OpenTelemetry::Instrumentation::PG',
40+
'que' => 'OpenTelemetry::Instrumentation::Que',
41+
'racecar' => 'OpenTelemetry::Instrumentation::Racecar',
42+
'rack' => 'OpenTelemetry::Instrumentation::Rack',
43+
'rails' => 'OpenTelemetry::Instrumentation::Rails',
44+
'rake' => 'OpenTelemetry::Instrumentation::Rake',
45+
'rdkafka' => 'OpenTelemetry::Instrumentation::Rdkafka',
46+
'redis' => 'OpenTelemetry::Instrumentation::Redis',
47+
'restclient' => 'OpenTelemetry::Instrumentation::RestClient',
48+
'resque' => 'OpenTelemetry::Instrumentation::Resque',
49+
'ruby_kafka' => 'OpenTelemetry::Instrumentation::RubyKafka',
50+
'sidekiq' => 'OpenTelemetry::Instrumentation::Sidekiq',
51+
'sinatra' => 'OpenTelemetry::Instrumentation::Sinatra'
52+
}.freeze
53+
54+
def self.detect_resource_from_env
55+
env = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s
56+
additional_resource = ::OpenTelemetry::SDK::Resources::Resource.create({})
57+
58+
env.split(',').each do |detector|
59+
case detector
60+
when 'container'
61+
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Container.detect) if defined? ::OpenTelemetry::Resource::Detector::Container
62+
when 'google_cloud_platform'
63+
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::GoogleCloudPlatform.detect) if defined? ::OpenTelemetry::Resource::Detector::GoogleCloudPlatform
64+
when 'azure'
65+
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Azure.detect) if defined? ::OpenTelemetry::Resource::Detector::Azure
66+
when 'aws'
67+
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::AWS.detect(%i[ec2 ecs eks lambda])) if defined? ::OpenTelemetry::Resource::Detector::AWS
7368
end
74-
75-
additional_resource
7669
end
7770

78-
def determine_enabled_instrumentation
79-
env = ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'].to_s
80-
81-
env.split(',').map { |instrumentation| OTEL_INSTRUMENTATION_MAP[instrumentation] }
82-
end
83-
84-
def setup_load_path
85-
lib = File.expand_path('..', __dir__)
86-
$LOAD_PATH.reject! { |path| path.include?('opentelemetry-auto-instrumentation') }
87-
$LOAD_PATH.unshift(lib)
88-
end
71+
additional_resource
72+
end
8973

90-
def initialize_opentelemetry
91-
setup_load_path
74+
def self.determine_enabled_instrumentation
75+
env = ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'].to_s
9276

93-
begin
94-
required_instrumentation = determine_enabled_instrumentation
77+
env.split(',').map { |instrumentation| OTEL_INSTRUMENTATION_MAP[instrumentation] }
78+
end
9579

96-
OpenTelemetry::SDK.configure do |c|
97-
c.resource = detect_resource_from_env
98-
if required_instrumentation.empty?
99-
c.use_all # enables all instrumentation!
100-
else
101-
required_instrumentation.each do |instrumentation|
102-
c.use instrumentation
103-
end
80+
def self.require_otel
81+
lib = File.expand_path('..', __dir__)
82+
$LOAD_PATH.reject! { |path| path.include?('opentelemetry-auto-instrumentation') }
83+
$LOAD_PATH.unshift(lib)
84+
85+
begin
86+
required_instrumentation = determine_enabled_instrumentation
87+
88+
OpenTelemetry::SDK.configure do |c|
89+
c.resource = detect_resource_from_env
90+
if required_instrumentation.empty?
91+
c.use_all # enables all instrumentation!
92+
else
93+
required_instrumentation.each do |instrumentation|
94+
c.use instrumentation
10495
end
10596
end
106-
OpenTelemetry.logger.info { 'Auto-instrumentation initialized' }
107-
rescue StandardError => e
108-
warn "Auto-instrumentation failed to initialize. Error: #{e.message}"
10997
end
98+
OpenTelemetry.logger.info { 'Auto-instrumentation initialized' }
99+
rescue StandardError => e
100+
warn "Auto-instrumentation failed to initialize. Error: #{e.message}"
110101
end
111102
end
112103
end
113104

114105
# ref: https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/boot/strap.rb
115-
unless method_defined?(:require)
116-
def require(...)
117-
super
118-
Initializer.initialize_opentelemetry
119-
end
106+
def require(...)
107+
super
108+
OTelInitializer.require_otel
120109
end
121110
end
122111

opentelemetry-auto-instrumentation/test/opentelemetry-auto-instrumentation_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@
1515
# Clean up constants and methods if they exist
1616
OTelBundlerPatch::Initializer.send(:remove_const, :OTEL_INSTRUMENTATION_MAP) if defined?(OTelBundlerPatch::Initializer::OTEL_INSTRUMENTATION_MAP)
1717

18+
# Remove singleton methods from Initializer
19+
if defined?(OTelBundlerPatch::Initializer)
20+
%i[detect_resource_from_env determine_enabled_instrumentation require_otel].each do |method|
21+
OTelBundlerPatch::Initializer.singleton_class.send(:undef_method, method) if OTelBundlerPatch::Initializer.respond_to?(method)
22+
end
23+
end
24+
25+
# Remove the Initializer module
1826
OTelBundlerPatch.send(:remove_const, :Initializer) if defined?(OTelBundlerPatch::Initializer)
1927

28+
# Remove instance methods from OTelBundlerPatch
2029
%i[require].each do |method|
2130
OTelBundlerPatch.send(:undef_method, method) if OTelBundlerPatch.method_defined?(method)
2231
end
32+
33+
# Reset instrumentation installation state
2334
[
2435
OpenTelemetry::Instrumentation::Net::HTTP::Instrumentation,
2536
OpenTelemetry::Instrumentation::Rake::Instrumentation

0 commit comments

Comments
 (0)