Skip to content

Instrument gems that are required after Datadog.configure #3113

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

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions .github/scripts/update_supported_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def process_integrations
integrations.each do |integration|
next if EXCLUDED_INTEGRATIONS.include?(integration)

integration_name = resolve_integration_name(integration)
integration_name = resolve_integration_names(integration)[0]

@integration_json_mapping[integration] = [
@min_gems['ruby'][integration_name],
Expand Down Expand Up @@ -128,11 +128,11 @@ def include_hardcoded_versions
]
end

def resolve_integration_name(integration)
def resolve_integration_names(integration)
mod_name = SPECIAL_CASES[integration] || integration.split('_').map(&:capitalize).join
module_name = "Datadog::Tracing::Contrib::#{mod_name}"
integration_module = Object.const_get(module_name)::Integration
integration_module.respond_to?(:gem_name) ? integration_module.gem_name : integration
integration_module.respond_to?(:gem_names) ? integration_module.gem_names : [integration]
rescue NameError, NoMethodError
puts "Fallback for #{integration}: module or gem_name not found."
integration
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
eval_gemfile("#{RUBY_ENGINE}-#{RUBY_ENGINE_VERSION.split('.').take(2).join('.')}.gemfile")

gem 'faraday'
# gem 'rails', '4.0.13'
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,19 @@ namespace :spec do
:suite,
:trilogy
].each do |contrib|
# Test for each integration. These tests will pollute the environment (e.g. load gems, monkey-patch modules).
desc '' # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/#{contrib}/**/*_spec.rb"
t.exclude_pattern = "spec/datadog/tracing/contrib/#{contrib}/integration_spec.rb"
t.rspec_opts = args.to_a.join(' ')
end

# Tests that have to run with a clean environment, where instrumented gems have
# not yet been loaded, and 3rd-party classes and modules have not been patched.
desc '' # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new("#{contrib}_clean") do |t, args|
t.pattern = "spec/datadog/tracing/contrib/#{contrib}/integration_spec.rb"
t.rspec_opts = args.to_a.join(' ')
end
end
Expand Down
74 changes: 74 additions & 0 deletions benchmarks/devweek.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# rubocop:disable
require 'datadog'

# module Datadog
# module Tracing
# module Contrib
# module Kernel
# def require(name)
# just_loaded = super
#
# @@dd_instance.require(name) if just_loaded
#
# just_loaded
# end
#
# class Instance
# def initialize
# @on_require = {}
# end
#
# def require(name)
# if @on_require.include?(name)
# Datadog.logger.debug { "Gem '#{name}' loaded. Invoking callback." }
#
# @on_require[name].call
# end
# rescue => e
# Datadog.logger.debug do
# "Failed to execute callback for gem '#{name}': #{e.class.name} #{e.message} at #{Array(e.backtrace).join("\n")}"
# end
# end
#
# def on_require(gem, &block)
# @on_require[gem] = block
# end
# end
#
# @@dd_instance = Instance.new
#
# def self.on_require(gem, &block)
# @@dd_instance.on_require(gem, &block)
# end
#
# def self.patch!
# ::Kernel.prepend(self)
# end
#
# DD_PATCH_ONLY_ONCE = Datadog::Core::Utils::OnlyOnce.new
# private_constant :DD_PATCH_ONLY_ONCE
# end
# end
# end
# end
#
# Datadog::Tracing::Contrib::Kernel.patch! # TODO: I think this stays here actually!

ENV['DD_TRACE_DEBUG'] = 'true'

# Setup
require 'datadog'

Datadog.configure do |c|
c.tracing.instrument :faraday
end

require 'faraday'

# User application
Faraday.get('http://example.com')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Code Vulnerability

Do not use cleartext protocol http:// (...read more)

This rule is designed to prevent the use of the insecure HTTP protocol in your Ruby code. The HTTP protocol does not encrypt the data that is sent between the client and the server, which can lead to sensitive information being intercepted by malicious parties. This is particularly risky when dealing with sensitive data such as API keys, user credentials, or personal user information.

The importance of this rule lies in the security and integrity of your application. By using an unsecured protocol like HTTP, you expose your application and its users to potential data breaches. A breach can lead to loss of trust, legal liability, and significant remediation costs.

To avoid violating this rule, always use the HTTPS protocol when making network requests. HTTPS encrypts the data sent between the client and server, protecting it from interception. By using libraries like Faraday, HTTPX, HTTParty, RestClient, or Ruby's built-in Net::HTTP, you can specify HTTPS by simply replacing 'http://' with 'https://'. For example, instead of HTTP.get("http://example.org"), use HTTP.get("https://example.org"). Always ensure that any third-party services your application interacts with support HTTPS.

View in Datadog  Leave us feedback  Documentation


# Tear down
Datadog.shutdown! # Ensure traces have been flushed

# rubocop:enable
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/action_cable/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :action_cable, auto_patch: false
def self.gem_name
'actioncable'


def self.gems
['actioncable']
end

def self.gem_load_paths
['action_cable']
end

def self.version
Expand Down
8 changes: 6 additions & 2 deletions lib/datadog/tracing/contrib/action_mailer/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :action_mailer, auto_patch: false

def self.gem_name
'actionmailer'
def self.gems
['actionmailer']
end

def self.gem_load_paths
['action_mailer']
end

def self.version
Expand Down
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/action_pack/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :action_pack, auto_patch: false
def self.gem_name
'actionpack'


def self.gems
['actionpack']
end

def self.gem_load_paths
['action_pack']
end

def self.version
Expand Down
9 changes: 7 additions & 2 deletions lib/datadog/tracing/contrib/action_view/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :action_view, auto_patch: false
def self.gem_name
'actionview'

def self.gem_load_paths
['action_view']
end

def self.gems
['actionview', 'actionpack']
end

def self.version
Expand Down
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/active_job/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :active_job, auto_patch: false
def self.gem_name
'activejob'


def self.gems
['activejob']
end

def self.gem_load_paths
['active_job']
end

def self.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :active_model_serializers

def self.gems
['active_model_serializers']
end

def self.gem_load_paths
['active_model_serializers']
end

def self.version
Gem.loaded_specs['active_model_serializers'] \
&& Gem.loaded_specs['active_model_serializers'].version
Expand Down
8 changes: 6 additions & 2 deletions lib/datadog/tracing/contrib/active_record/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :active_record, auto_patch: false

def self.gem_name
'activerecord'
def self.gems
['activerecord']
end

def self.gem_load_paths
['active_record']
end

def self.version
Expand Down
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/active_support/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :active_support, auto_patch: false
def self.gem_name
'activesupport'


def self.gems
['activesupport']
end

def self.gem_load_paths
['active_support']
end

def self.version
Expand Down
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/aws/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :aws, auto_patch: true
def self.gem_name
'aws-sdk'


def self.gems
['aws-sdk', 'aws-sdk-core']
end

def self.gem_load_paths
['aws-sdk', 'aws-sdk-core']
end

def self.version
Expand Down
10 changes: 8 additions & 2 deletions lib/datadog/tracing/contrib/concurrent_ruby/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ class Integration

# @public_api Changing the integration name or integration options can cause breaking changes
register_as :concurrent_ruby
def self.gem_name
'concurrent-ruby'


def self.gems
['concurrent-ruby']
end

def self.gem_load_paths
['concurrent']
end

def self.version
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/tracing/contrib/dalli/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :dalli, auto_patch: true

def self.gems
['dalli']
end

def self.gem_load_paths
['dalli']
end

def self.version
Gem.loaded_specs['dalli'] && Gem.loaded_specs['dalli'].version
end
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/tracing/contrib/delayed_job/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :delayed_job

def self.gems
['delayed_job']
end

def self.gem_load_paths
['delayed_job']
end

def self.version
Gem.loaded_specs['delayed_job'] && Gem.loaded_specs['delayed_job'].version
end
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/tracing/contrib/elasticsearch/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :elasticsearch, auto_patch: true

def self.gems
['elastic-transport', 'elasticsearch-transport']
end

def self.gem_load_paths
['elastic-transport', 'elasticsearch-transport']
end

def self.version
# elastic-transport gem for version >= 8.0.0
# elasticsearch-transport gem for version < 8.0.0
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/tracing/contrib/ethon/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :ethon

def self.gems
['ethon']
end

def self.gem_load_paths
['ethon']
end

def self.version
Gem.loaded_specs['ethon'] && Gem.loaded_specs['ethon'].version
end
Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/tracing/contrib/excon/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class Integration
# @public_api Changing the integration name or integration options can cause breaking changes
register_as :excon

def self.gems
['excon']
end

def self.gem_load_paths
['excon']
end

def self.version
Gem.loaded_specs['excon'] && Gem.loaded_specs['excon'].version
end
Expand Down
Loading
Loading