Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gem for zero-code-instrumentation in otel-operator #1384

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lint
  • Loading branch information
xuan-cao-swi committed Jan 29, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 444959aad3e85ac7343b5a6cb81eb236f9d822a1
4 changes: 2 additions & 2 deletions zero-code-instrumentation/Gemfile
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@ gem 'opentelemetry-resource-detector-container', path: '../resources/container'

gem 'bundler'
gem 'minitest'
gem 'rake'
gem 'rake'
gem 'rubocop'
gem 'rubocop-performance'
gem 'simplecov'
gem 'simplecov'
gem 'yard'
104 changes: 51 additions & 53 deletions zero-code-instrumentation/lib/zero-code-instrumentation.rb
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
#
# SPDX-License-Identifier: Apache-2.0

# OTelBundlerPatch
module OTelBundlerPatch

# ref: https://github.com/newrelic/newrelic-ruby-agent/blob/dev/lib/boot/strap.rb
def require(*_groups)
super
@@ -14,47 +14,47 @@ def require(*_groups)

# this is used for case when user particularly want to enable single instrumentation
OTEL_INSTRUMENTATION_MAP = {
"gruf" => "OpenTelemetry::Instrumentation::Gruf",
"trilogy" => "OpenTelemetry::Instrumentation::Trilogy",
"active_support" => "OpenTelemetry::Instrumentation::ActiveSupport",
"action_pack" => "OpenTelemetry::Instrumentation::ActionPack",
"active_job" => "OpenTelemetry::Instrumentation::ActiveJob",
"active_record" => "OpenTelemetry::Instrumentation::ActiveRecord",
"action_view" => "OpenTelemetry::Instrumentation::ActionView",
"action_mailer" => "OpenTelemetry::Instrumentation::ActionMailer",
"aws_sdk" => "OpenTelemetry::Instrumentation::AwsSdk",
"aws_lambda" => "OpenTelemetry::Instrumentation::AwsLambda",
"bunny" => "OpenTelemetry::Instrumentation::Bunny",
"lmdb" => "OpenTelemetry::Instrumentation::LMDB",
"http" => "OpenTelemetry::Instrumentation::HTTP",
"koala" => "OpenTelemetry::Instrumentation::Koala",
"active_model_serializers" => "OpenTelemetry::Instrumentation::ActiveModelSerializers",
"concurrent_ruby" => "OpenTelemetry::Instrumentation::ConcurrentRuby",
"dalli" => "OpenTelemetry::Instrumentation::Dalli",
"delayed_job" => "OpenTelemetry::Instrumentation::DelayedJob",
"ethon" => "OpenTelemetry::Instrumentation::Ethon",
"excon" => "OpenTelemetry::Instrumentation::Excon",
"faraday" => "OpenTelemetry::Instrumentation::Faraday",
"grape" => "OpenTelemetry::Instrumentation::Grape",
"graphql" => "OpenTelemetry::Instrumentation::GraphQL",
"http_client" => "OpenTelemetry::Instrumentation::HttpClient",
"mongo" => "OpenTelemetry::Instrumentation::Mongo",
"mysql2" => "OpenTelemetry::Instrumentation::Mysql2",
"net_http" => "OpenTelemetry::Instrumentation::Net::HTTP",
"pg" => "OpenTelemetry::Instrumentation::PG",
"que" => "OpenTelemetry::Instrumentation::Que",
"racecar" => "OpenTelemetry::Instrumentation::Racecar",
"rack" => "OpenTelemetry::Instrumentation::Rack",
"rails" => "OpenTelemetry::Instrumentation::Rails",
"rake" => "OpenTelemetry::Instrumentation::Rake",
"rdkafka" => "OpenTelemetry::Instrumentation::Rdkafka",
"redis" => "OpenTelemetry::Instrumentation::Redis",
"restclient" => "OpenTelemetry::Instrumentation::RestClient",
"resque" => "OpenTelemetry::Instrumentation::Resque",
"ruby_kafka" => "OpenTelemetry::Instrumentation::RubyKafka",
"sidekiq" => "OpenTelemetry::Instrumentation::Sidekiq",
"sinatra" => "OpenTelemetry::Instrumentation::Sinatra"
}
'gruf' => 'OpenTelemetry::Instrumentation::Gruf',
'trilogy' => 'OpenTelemetry::Instrumentation::Trilogy',
'active_support' => 'OpenTelemetry::Instrumentation::ActiveSupport',
'action_pack' => 'OpenTelemetry::Instrumentation::ActionPack',
'active_job' => 'OpenTelemetry::Instrumentation::ActiveJob',
'active_record' => 'OpenTelemetry::Instrumentation::ActiveRecord',
'action_view' => 'OpenTelemetry::Instrumentation::ActionView',
'action_mailer' => 'OpenTelemetry::Instrumentation::ActionMailer',
'aws_sdk' => 'OpenTelemetry::Instrumentation::AwsSdk',
'aws_lambda' => 'OpenTelemetry::Instrumentation::AwsLambda',
'bunny' => 'OpenTelemetry::Instrumentation::Bunny',
'lmdb' => 'OpenTelemetry::Instrumentation::LMDB',
'http' => 'OpenTelemetry::Instrumentation::HTTP',
'koala' => 'OpenTelemetry::Instrumentation::Koala',
'active_model_serializers' => 'OpenTelemetry::Instrumentation::ActiveModelSerializers',
'concurrent_ruby' => 'OpenTelemetry::Instrumentation::ConcurrentRuby',
'dalli' => 'OpenTelemetry::Instrumentation::Dalli',
'delayed_job' => 'OpenTelemetry::Instrumentation::DelayedJob',
'ethon' => 'OpenTelemetry::Instrumentation::Ethon',
'excon' => 'OpenTelemetry::Instrumentation::Excon',
'faraday' => 'OpenTelemetry::Instrumentation::Faraday',
'grape' => 'OpenTelemetry::Instrumentation::Grape',
'graphql' => 'OpenTelemetry::Instrumentation::GraphQL',
'http_client' => 'OpenTelemetry::Instrumentation::HttpClient',
'mongo' => 'OpenTelemetry::Instrumentation::Mongo',
'mysql2' => 'OpenTelemetry::Instrumentation::Mysql2',
'net_http' => 'OpenTelemetry::Instrumentation::Net::HTTP',
'pg' => 'OpenTelemetry::Instrumentation::PG',
'que' => 'OpenTelemetry::Instrumentation::Que',
'racecar' => 'OpenTelemetry::Instrumentation::Racecar',
'rack' => 'OpenTelemetry::Instrumentation::Rack',
'rails' => 'OpenTelemetry::Instrumentation::Rails',
'rake' => 'OpenTelemetry::Instrumentation::Rake',
'rdkafka' => 'OpenTelemetry::Instrumentation::Rdkafka',
'redis' => 'OpenTelemetry::Instrumentation::Redis',
'restclient' => 'OpenTelemetry::Instrumentation::RestClient',
'resque' => 'OpenTelemetry::Instrumentation::Resque',
'ruby_kafka' => 'OpenTelemetry::Instrumentation::RubyKafka',
'sidekiq' => 'OpenTelemetry::Instrumentation::Sidekiq',
'sinatra' => 'OpenTelemetry::Instrumentation::Sinatra'
}.freeze

def detect_resource_from_env
env = ENV['OTEL_RUBY_RESOURCE_DETECTORS'].to_s
@@ -63,11 +63,11 @@ def detect_resource_from_env
env.split(',').each do |detector|
case detector
when 'container'
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Container.detect) if defined? (::OpenTelemetry::Resource::Detector::Container)
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Container.detect) if defined? ::OpenTelemetry::Resource::Detector::Container
when 'google_cloud_platform'
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::GoogleCloudPlatform.detect) if defined? (::OpenTelemetry::Resource::Detector::GoogleCloudPlatform)
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::GoogleCloudPlatform.detect) if defined? ::OpenTelemetry::Resource::Detector::GoogleCloudPlatform
when 'azure'
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Azure.detect) if defined? (::OpenTelemetry::Resource::Detector::Azure)
additional_resource = additional_resource.merge(::OpenTelemetry::Resource::Detector::Azure.detect) if defined? ::OpenTelemetry::Resource::Detector::Azure
end
end

@@ -77,13 +77,11 @@ def detect_resource_from_env
def determine_enabled_instrumentation
env = ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'].to_s

enabled_instrumentation = []
env.split(',').each { |instrumentation| enabled_instrumentation.push(OTEL_INSTRUMENTATION_MAP[instrumentation]) }
enabled_instrumentation
env.split(',').map { |instrumentation| OTEL_INSTRUMENTATION_MAP[instrumentation] }
end

def require_otel
lib = File.expand_path('../..', __FILE__)
lib = File.expand_path('..', __dir__)
$LOAD_PATH.reject! { |path| path.include?('zero-code-instrumentation') }
$LOAD_PATH.unshift(lib)

@@ -92,17 +90,17 @@ def require_otel

OpenTelemetry::SDK.configure do |c|
c.resource = detect_resource_from_env
if required_instrumentation.size == 0
if required_instrumentation.empty?
c.use_all # enables all instrumentation!
else
required_instrumentation.each do |instrumentation|
c.use instrumentation
end
end
end
OpenTelemetry.logger.info {"Auto-instrumentation initialized"}
OpenTelemetry.logger.info { 'Auto-instrumentation initialized' }
rescue StandardError => e
OpenTelemetry.logger.info {"Auto-instrumentation failed to initialize. Error: #{e.message}"}
OpenTelemetry.logger.info { "Auto-instrumentation failed to initialize. Error: #{e.message}" }
end
end
end
@@ -118,7 +116,7 @@ def require_otel
# assume the operator ruby image will amount the additional otel gem to this folder
operator_gem_path = ENV['OTEL_OPERATOR'].to_s == 'true' ? '/otel-auto-instrumentation-ruby' : nil
additional_gem_path = operator_gem_path || ENV['ADDITIONAL_GEM_PATH'] || Gem.dir
OpenTelemetry.logger.info { "Loading the additional gem path from #{additional_gem_path}"}
OpenTelemetry.logger.info { "Loading the additional gem path from #{additional_gem_path}" }

# google-protobuf is used for otel trace exporter
Dir.glob("#{additional_gem_path}/gems/*").each do |file|
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
require 'test_helper'

describe 'ZeroCodeInstrumentation' do

before do
OpenTelemetry::TestHelpers.reset_opentelemetry
end
@@ -23,7 +22,6 @@
end

it 'simple_load_test' do

load './lib/zero-code-instrumentation.rb'
Bundler.require

@@ -42,7 +40,6 @@
end

it 'simple_load_with_net_http_disabled' do

ENV['OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED'] = 'false'

load './lib/zero-code-instrumentation.rb'
@@ -57,7 +54,6 @@
end

it 'simple_load_with_desired_instrument_only' do

ENV['OTEL_RUBY_ENABLED_INSTRUMENTATIONS'] = 'net_http'

load './lib/zero-code-instrumentation.rb'
@@ -72,7 +68,6 @@
end

it 'simple_load_with_additional_resource' do

ENV['OTEL_RUBY_RESOURCE_DETECTORS'] = 'container'

load './lib/zero-code-instrumentation.rb'
6 changes: 3 additions & 3 deletions zero-code-instrumentation/zero-code-instrumentation.gemspec
Original file line number Diff line number Diff line change
@@ -26,14 +26,14 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.1'

spec.add_dependency 'opentelemetry-sdk', '~> 1.0'
spec.add_dependency 'opentelemetry-instrumentation-all', '~> 0.72.0'
spec.add_dependency 'opentelemetry-exporter-otlp', '~> 0.29.1'
spec.add_dependency 'opentelemetry-helpers-mysql', '~> 0.2.0'
spec.add_dependency 'opentelemetry-helpers-sql-obfuscation', '~> 0.3.0'
spec.add_dependency 'opentelemetry-resource-detector-google_cloud_platform', '~> 0.2.0'
spec.add_dependency 'opentelemetry-instrumentation-all', '~> 0.72.0'
spec.add_dependency 'opentelemetry-resource-detector-azure', '~> 0.2.0'
spec.add_dependency 'opentelemetry-resource-detector-container', '~> 0.2.0'
spec.add_dependency 'opentelemetry-resource-detector-google_cloud_platform', '~> 0.2.0'
spec.add_dependency 'opentelemetry-sdk', '~> 1.0'

spec.add_development_dependency 'bundler', '~> 2.4'
spec.add_development_dependency 'minitest', '~> 5.0'