|
6 | 6 |
|
7 | 7 | # OTelBundlerPatch |
8 | 8 | 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 |
73 | 68 | end |
74 | | - |
75 | | - additional_resource |
76 | 69 | end |
77 | 70 |
|
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 |
89 | 73 |
|
90 | | - def initialize_opentelemetry |
91 | | - setup_load_path |
| 74 | + def self.determine_enabled_instrumentation |
| 75 | + env = ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'].to_s |
92 | 76 |
|
93 | | - begin |
94 | | - required_instrumentation = determine_enabled_instrumentation |
| 77 | + env.split(',').map { |instrumentation| OTEL_INSTRUMENTATION_MAP[instrumentation] } |
| 78 | + end |
95 | 79 |
|
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 |
104 | 95 | end |
105 | 96 | end |
106 | | - OpenTelemetry.logger.info { 'Auto-instrumentation initialized' } |
107 | | - rescue StandardError => e |
108 | | - warn "Auto-instrumentation failed to initialize. Error: #{e.message}" |
109 | 97 | end |
| 98 | + OpenTelemetry.logger.info { 'Auto-instrumentation initialized' } |
| 99 | + rescue StandardError => e |
| 100 | + warn "Auto-instrumentation failed to initialize. Error: #{e.message}" |
110 | 101 | end |
111 | 102 | end |
112 | 103 | end |
113 | 104 |
|
114 | 105 | # 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 |
120 | 109 | end |
121 | 110 | end |
122 | 111 |
|
|
0 commit comments