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

Instrument gems that are required after Datadog.configure #3113

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ end
#
# TODO: Remove this once the issue is resolved: https://github.com/ffi/ffi/issues/1107
gem 'ffi', '~> 1.16.3', require: false


gem 'faraday'
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,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 an environment where the instrumented gem has
# not been loaded yet nor modules have been modified.
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 'ddtrace'

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 'ddtrace'

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

require 'faraday'

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

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

# rubocop:enable
1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_activesupport.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions gemfiles/jruby_9.2_activesupport.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_aws.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions gemfiles/jruby_9.2_aws.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_contrib.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions gemfiles/jruby_9.2_contrib.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gemfiles/jruby_9.2_contrib_old.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_core_old.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions gemfiles/jruby_9.2_core_old.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_elasticsearch_7.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_elasticsearch_7.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_elasticsearch_8.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_elasticsearch_8.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gemfiles/jruby_9.2_graphql_2.0.gemfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading