Skip to content

Commit

Permalink
first version of cuprite integration
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jan 21, 2025
1 parent aac60e3 commit b44f9a9
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 1 deletion.
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ target :lib do
library "timecop"
library "webmock"
library "simplecov"
library "cuprite"
end
1 change: 1 addition & 0 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def test_optimisation

# Additional test libraries (auto instrumented later on test session start)
require_relative "ci/contrib/selenium/integration"
require_relative "ci/contrib/cuprite/integration"
require_relative "ci/contrib/simplecov/integration"

# Configuration extensions
Expand Down
34 changes: 34 additions & 0 deletions lib/datadog/ci/contrib/cuprite/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "datadog/core"

require_relative "../ext"
require_relative "../../settings"

require_relative "../../../ext/rum"

module Datadog
module CI
module Contrib
module Cuprite
module Configuration
# Custom settings for the Cuprite integration
# @public_api
class Settings < Datadog::CI::Contrib::Settings
option :enabled do |o|
o.type :bool
o.env Ext::ENV_ENABLED
o.default true
end

option :rum_flush_wait_millis do |o|
o.type :int
o.env CI::Ext::RUM::ENV_RUM_FLUSH_WAIT_MILLIS
o.default 500
end
end
end
end
end
end
end
90 changes: 90 additions & 0 deletions lib/datadog/ci/contrib/cuprite/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

require_relative "../patcher"

require_relative "../../ext/rum"
require_relative "../../ext/test"
require_relative "../../utils/rum"

module Datadog
module CI
module Contrib
module Cuprite
# instruments Capybara::Cuprite::Driver
module Driver
def self.included(base)
base.prepend(InstanceMethods)
end

module InstanceMethods
def visit(url)
result = super

return result unless datadog_configuration[:enabled]

Datadog.logger.debug("[Cuprite] Navigation to #{url}")

# on session reset Capybara navigates to about:blank
return result if url == "about:blank"

active_test = Datadog::CI.active_test
Datadog.logger.debug("[Cuprite] Active test: #{active_test}")

return result unless active_test

# Set the test's trace id as a cookie in browser session
Datadog.logger.debug do
"[Cuprite] Setting cookie #{CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID} to #{active_test.trace_id}"
end
set_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID, active_test.trace_id.to_s)

# set the test type to browser
active_test.set_tag(CI::Ext::Test::TAG_TYPE, CI::Ext::Test::Type::BROWSER)

# set the tags specific to the browser test
active_test.set_tag(CI::Ext::Test::TAG_BROWSER_DRIVER, "cuprite")
active_test.set_tag(CI::Ext::Test::TAG_BROWSER_DRIVER_VERSION, datadog_integration.version)
active_test.set_tag(CI::Ext::Test::TAG_BROWSER_NAME, browser.options.browser_name)
active_test.set_tag(CI::Ext::Test::TAG_BROWSER_VERSION, browser.version)

result
end

def reset!
datadog_end_rum_session

super
end

def quit
datadog_end_rum_session

super
end

private

def datadog_integration
CI::Contrib::Instrumentation.fetch_integration(:cuprite)
end

def datadog_configuration
Datadog.configuration.ci[:cuprite]
end

def datadog_end_rum_session
return unless datadog_configuration[:enabled]

Datadog.logger.debug("[Cuprite] Driver quit event")

Utils::RUM.stop_rum_session(self)

Datadog.logger.debug("[Cuprite] RUM session stopped, deleting cookie")
remove_cookie(CI::Ext::RUM::COOKIE_TEST_EXECUTION_ID)
end
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/datadog/ci/contrib/cuprite/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Datadog
module CI
module Contrib
module Cuprite
# Cuprite integration constants
# @public_api
module Ext
ENV_ENABLED = "DD_CIVISIBILITY_CUPRITE_ENABLED"
end
end
end
end
end
44 changes: 44 additions & 0 deletions lib/datadog/ci/contrib/cuprite/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require_relative "../integration"
require_relative "configuration/settings"
require_relative "patcher"

module Datadog
module CI
module Contrib
module Cuprite
# Description of Cuprite integration
class Integration < Contrib::Integration
MINIMUM_VERSION = Gem::Version.new("0.15.0")

def version
Gem.loaded_specs["cuprite"]&.version
end

def loaded?
!defined?(::Capybara).nil? && !defined?(::Capybara::Cuprite).nil? &&
!defined?(::Capybara::Cuprite::Driver).nil?
end

def compatible?
super && version >= MINIMUM_VERSION
end

# additional instrumentations for test libraries are late instrumented on test session start
def late_instrument?
true
end

def new_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/datadog/ci/contrib/cuprite/patcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative "../patcher"

require_relative "driver"

module Datadog
module CI
module Contrib
module Cuprite
# Patcher enables patching of 'Capybara::Cuprite::Driver' class.
module Patcher
include Datadog::CI::Contrib::Patcher

module_function

def patch
::Capybara::Cuprite::Driver.include(Driver)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/datadog/ci/utils/rum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.is_rum_active?(script_executor)
Utils::Parsing.convert_to_bool(is_rum_active_script_result)
end

def self.stop_rum_session(script_executor)
def self.stop_rum_session(script_executor, rum_flush_wait_millis: 500)
config = Datadog.configuration.ci[:selenium]
if is_rum_active?(script_executor)
Datadog::CI.active_test&.set_tag(
Expand Down
12 changes: 12 additions & 0 deletions sig/datadog/ci/contrib/cuprite/configuration/settings.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Datadog
module CI
module Contrib
module Cuprite
module Configuration
class Settings < Datadog::CI::Contrib::Settings
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions sig/datadog/ci/contrib/cuprite/driver.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Datadog
module CI
module Contrib
module Cuprite
module Driver
def self.included: (untyped base) -> untyped

module InstanceMethods
include Capybara::Cuprite::Driver

def visit: (untyped url) -> untyped

def reset!: () -> untyped

def quit: () -> untyped

private

def datadog_integration: () -> untyped

def datadog_configuration: () -> untyped

def datadog_end_rum_session: () -> (nil | untyped)
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions sig/datadog/ci/contrib/cuprite/ext.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Datadog
module CI
module Contrib
module Cuprite
module Ext
ENV_ENABLED: "DD_CIVISIBILITY_CUPRITE_ENABLED"
end
end
end
end
end
23 changes: 23 additions & 0 deletions sig/datadog/ci/contrib/cuprite/integration.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Datadog
module CI
module Contrib
module Cuprite
class Integration < Contrib::Integration
MINIMUM_VERSION: Gem::Version

def version: () -> untyped

def loaded?: () -> bool

def compatible?: () -> bool

def late_instrument?: () -> true

def new_configuration: () -> untyped

def patcher: () -> untyped
end
end
end
end
end
13 changes: 13 additions & 0 deletions sig/datadog/ci/contrib/cuprite/patcher.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Datadog
module CI
module Contrib
module Cuprite
module Patcher
include Datadog::CI::Contrib::Patcher

def self?.patch: () -> untyped
end
end
end
end
end
34 changes: 34 additions & 0 deletions vendor/rbs/cuprite/0/driver.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Capybara
end

module Capybara::Cuprite
end

module Ferrum
end

class Ferrum::Browser
def version: () -> String

def options: () -> Ferrum::Browser::Options
end

class Ferrum::Browser::Options
def browser_name: () -> String
end

module Capybara::Cuprite::Driver
def visit: (String url) -> untyped

def reset!: () -> void

def quit: () -> void

def set_cookie: (String name, String value, ?Hash[Symbol, untyped] options) -> void

def remove_cookie: (String name, ?Hash[Symbol, untyped] options) -> void

def execute_script: (String script, ?untyped args) -> untyped

def browser: () -> Ferrum::Browser
end

0 comments on commit b44f9a9

Please sign in to comment.