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

[SDTEST-1129] Internal: decouple patcher from tracing, update ruby 3.4 compatibility #252

Merged
merged 6 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Copy link
Member

Choose a reason for hiding this comment

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

I know that it might be cumbersome to fill in, but maybe we can shape it a bit? Like if it's Array, we can state it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hopefully, while building auto instrumentation feature I will have a chance to rethink all of it and remove this all. Generally, all this stuff was created for APM use case and most of it isn't required for this more focused library and narrower use case.


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
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
Loading