Skip to content

Commit

Permalink
Merge pull request #252 from DataDog/anmarchenko/decouple_patcher_fro…
Browse files Browse the repository at this point in the history
…m_tracing

[SDTEST-1129] Internal: decouple patcher from tracing, update ruby 3.4 compatibility
  • Loading branch information
anmarchenko authored Nov 6, 2024
2 parents cb3de5c + 3e297cd commit acc48cb
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 39 deletions.
16 changes: 8 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ TEST_METADATA = {
"" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby"
},
"cucumber" => {
"cucumber-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-4" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-5" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-6" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-7" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-8" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-9" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
"cucumber-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-4" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-5" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-6" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-7" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-8" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby",
"cucumber-9" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
},
"rspec" => {
"rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby"
Expand Down Expand Up @@ -80,7 +80,7 @@ TEST_METADATA = {
"knapsack_pro-7-rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ❌ jruby"
},
"selenium" => {
"selenium-4-capybara-3" => "❌ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
"selenium-4-capybara-3" => "❌ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / 3.4 / ✅ jruby"
},
"timecop" => {
"timecop-0" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby"
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/cucumber/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "instrumentation"

Expand All @@ -10,7 +10,7 @@ module Contrib
module Cucumber
# Patches 'cucumber' gem.
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

module_function

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/minitest/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Contrib
module Minitest
# Patcher enables patching of 'minitest' module.
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

module_function

Expand Down
64 changes: 64 additions & 0 deletions lib/datadog/ci/contrib/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require "datadog/core/utils/only_once"
require "datadog/core/telemetry/logger"

module Datadog
module CI
module Contrib
# Common behavior for patcher modules.
module Patcher
def self.included(base)
base.singleton_class.prepend(CommonMethods)
end

# Prepended instance methods for all patchers
module CommonMethods
attr_accessor \
:patch_error_result,
:patch_successful

def patch_name
(self.class != Class && self.class != Module) ? self.class.name : name
end

def patched?
patch_only_once.ran?
end

def patch
return unless defined?(super)

patch_only_once.run do
super.tap do
@patch_successful = true
end
rescue => e
on_patch_error(e)
end
end

# Processes patching errors. This default implementation logs the error and reports relevant metrics.
# @param e [Exception]
def on_patch_error(e)
Datadog.logger.error("Failed to apply #{patch_name} patch. Cause: #{e} Location: #{Array(e.backtrace).first}")
Datadog::Core::Telemetry::Logger.report(e, description: "Failed to apply #{patch_name} patch")

@patch_error_result = {
type: e.class.name,
message: e.message,
line: Array(e.backtrace).first
}
end

private

def patch_only_once
# NOTE: This is not thread-safe
@patch_only_once ||= Datadog::Core::Utils::OnlyOnce.new
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/rspec/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "example"
require_relative "example_group"
Expand All @@ -12,7 +12,7 @@ module Contrib
module RSpec
# Patcher enables patching of 'rspec' module.
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

module_function

Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/selenium/capybara_driver.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "ext"
require_relative "rum"
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/selenium/driver.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "ext"
require_relative "rum"
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/contrib/selenium/navigation.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "ext"
require_relative "../../ext/test"
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/selenium/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "capybara_driver"
require_relative "driver"
Expand All @@ -12,7 +12,7 @@ module Contrib
module Selenium
# Patcher enables patching of 'Selenium::WebDriver' module.
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

module_function

Expand Down
2 changes: 0 additions & 2 deletions lib/datadog/ci/contrib/selenium/rum.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"

require_relative "ext"
require_relative "../../ext/test"
require_relative "../../utils/parsing"
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/ci/contrib/simplecov/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "datadog/tracing/contrib/patcher"
require_relative "../patcher"

require_relative "result_extractor"

Expand All @@ -10,7 +10,7 @@ module Contrib
module Simplecov
# Patcher enables patching of 'SimpleCov' module.
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

module_function

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/cucumber/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Contrib
module Cucumber
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

def self?.target_version: () -> untyped

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/minitest/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Contrib
module Minitest
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

def self?.target_version: () -> untyped

Expand Down
33 changes: 33 additions & 0 deletions sig/datadog/ci/contrib/patcher.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Datadog
module CI
module Contrib
module Patcher
def self.included: (untyped base) -> untyped

module CommonMethods
attr_accessor patch_error_result: untyped

attr_accessor patch_successful: untyped

def patch_name: () -> String?

def name: () -> String

def patched?: () -> bool

def patch: () -> void

def on_patch_error: (untyped e) -> untyped

def default_tags: () -> untyped

private

def patch_only_once: () -> untyped

@patch_only_once: Datadog::Core::Utils::OnlyOnce
end
end
end
end
end
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/rspec/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Contrib
module RSpec
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

def self?.target_version: () -> String

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/selenium/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Contrib
module Selenium
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

def self?.target_version: () -> untyped

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/simplecov/patcher.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Datadog
module Contrib
module Simplecov
module Patcher
include Datadog::Tracing::Contrib::Patcher
include Datadog::CI::Contrib::Patcher

def self?.target_version: () -> untyped

Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/ci/git/search_commits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
expect { subject }
.to raise_error(
Datadog::CI::Git::SearchCommits::ApiError,
"Invalid commit type response {\"id\"=>\"c7f893648f656339f62fb7b4d8a6ecdf7d063835\", \"type\"=>\"invalid\"}"
/Invalid commit type response/
)
end
end
Expand Down Expand Up @@ -169,7 +169,7 @@
expect { subject }
.to raise_error(
Datadog::CI::Git::SearchCommits::ApiError,
"Invalid commit type response {\"id\"=>\"c7f893648f656339f62fb7b4d8a6ecdf7d063835\"}"
/Invalid commit type response/
)
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/datadog/ci/test_optimisation/coverage/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@
subject

expect(Datadog.logger).to have_received(:warn).with(
"citestcov event is invalid: [test_suite_id] is nil. " \
"Event: Coverage::Event[test_id=4, test_suite_id=, test_session_id=6, " \
"coverage={\"file.rb\"=>true, \"file2.rb\"=>true}]"
/\[test_suite_id\] is nil/
)
end

Expand Down
6 changes: 1 addition & 5 deletions spec/datadog/ci/test_visibility/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@
it "logs warning that events were filtered out" do
subject

expect(Datadog.logger).to have_received(:warn).with(
"Invalid event skipped: " \
"Datadog::CI::TestVisibility::Serializers::Span(id:#{http_span.id},name:#{http_span.name}) " \
"Errors: {\"start\"=>#<Set: {\"must be greater than or equal to 946684800000000000\"}>}"
)
expect(Datadog.logger).to have_received(:warn).with(/must be greater than or equal to 946684800000000000/)
end

it_behaves_like "emits telemetry metric", :inc, "events_enqueued_for_serialization", 3
Expand Down
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ def self.load_plugins

# Raise error when patching an integration fails.
# This can be disabled by unstubbing +CommonMethods#on_patch_error+
require "datadog/tracing/contrib/patcher"
config.before do
allow_any_instance_of(Datadog::Tracing::Contrib::Patcher::CommonMethods).to(receive(:on_patch_error)) { |_, e| raise e }
allow_any_instance_of(Datadog::CI::Contrib::Patcher::CommonMethods).to(receive(:on_patch_error)) { |_, e| raise e }
end

# Ensure tracer environment is clean before running tests.
Expand Down
15 changes: 15 additions & 0 deletions vendor/rbs/ddtrace/0/datadog/core/telemetry/logger.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Datadog
module Core
module Telemetry
module Logger
def self.report: (Exception exception, ?level: Symbol, ?description: String?) -> void

def self.error: (String description) -> void

private

def self.instance: () -> Datadog::Core::Telemetry::Component?
end
end
end
end

0 comments on commit acc48cb

Please sign in to comment.