Skip to content

Commit

Permalink
rename TestVisibility::Recorder to TestVisibility::Component
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 2, 2024
1 parent 2e7f0ff commit a891eba
Show file tree
Hide file tree
Showing 31 changed files with 165 additions and 162 deletions.
26 changes: 13 additions & 13 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class << self
# @return [Datadog::CI::TestSession] the active, running {Datadog::CI::TestSession}.
# @return [nil] if test suite level visibility is disabled or CI mode is disabled.
def start_test_session(service: Utils::Configuration.fetch_service_name("test"), tags: {})
recorder.start_test_session(service: service, tags: tags)
test_visibility.start_test_session(service: service, tags: tags)
end

# The active, unfinished test session.
Expand All @@ -61,7 +61,7 @@ def start_test_session(service: Utils::Configuration.fetch_service_name("test"),
# @return [Datadog::CI::TestSession] the active test session
# @return [nil] if no test session is active
def active_test_session
recorder.active_test_session
test_visibility.active_test_session
end

# Starts a {Datadog::CI::TestModule ci_test_module} that represents a single test module (for most Ruby test frameworks
Expand Down Expand Up @@ -93,7 +93,7 @@ def active_test_session
# @return [Datadog::CI::TestModule] the active, running {Datadog::CI::TestModule}.
# @return [nil] if test suite level visibility is disabled or CI mode is disabled.
def start_test_module(test_module_name, service: nil, tags: {})
recorder.start_test_module(test_module_name, service: service, tags: tags)
test_visibility.start_test_module(test_module_name, service: service, tags: tags)
end

# The active, unfinished test module.
Expand All @@ -116,7 +116,7 @@ def start_test_module(test_module_name, service: nil, tags: {})
# @return [Datadog::CI::TestModule] the active test module
# @return [nil] if no test module is active
def active_test_module
recorder.active_test_module
test_visibility.active_test_module
end

# Starts a {Datadog::CI::TestSuite ci_test_suite} that represents a single test suite.
Expand Down Expand Up @@ -145,7 +145,7 @@ def active_test_module
# @return [Datadog::CI::TestSuite] the active, running {Datadog::CI::TestSuite}.
# @return [nil] if test suite level visibility is disabled or CI mode is disabled.
def start_test_suite(test_suite_name, service: nil, tags: {})
recorder.start_test_suite(test_suite_name, service: service, tags: tags)
test_visibility.start_test_suite(test_suite_name, service: service, tags: tags)
end

# The active, unfinished test suite.
Expand All @@ -168,7 +168,7 @@ def start_test_suite(test_suite_name, service: nil, tags: {})
# @return [Datadog::CI::TestSuite] the active test suite
# @return [nil] if no test suite with given name is active
def active_test_suite(test_suite_name)
recorder.active_test_suite(test_suite_name)
test_visibility.active_test_suite(test_suite_name)
end

# Return a {Datadog::CI::Test ci_test} that will trace a test called `test_name`.
Expand Down Expand Up @@ -222,7 +222,7 @@ def active_test_suite(test_suite_name)
# @yieldparam [Datadog::CI::Test] ci_test the newly created and active [Datadog::CI::Test]
# @yieldparam [nil] if CI mode is disabled
def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
recorder.trace_test(test_name, test_suite_name, service: service, tags: tags, &block)
test_visibility.trace_test(test_name, test_suite_name, service: service, tags: tags, &block)
end

# Same as {.trace_test} but it does not accept a block.
Expand All @@ -248,7 +248,7 @@ def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
# @return [Datadog::CI::Test] the active, unfinished {Datadog::CI::Test}.
# @return [nil] if CI mode is disabled.
def start_test(test_name, test_suite_name, service: nil, tags: {})
recorder.trace_test(test_name, test_suite_name, service: service, tags: tags)
test_visibility.trace_test(test_name, test_suite_name, service: service, tags: tags)
end

# Trace any custom span inside a test. For example, you could trace:
Expand Down Expand Up @@ -300,7 +300,7 @@ def trace(span_name, type: "span", tags: {}, &block)
)
end

recorder.trace(span_name, type: type, tags: tags, &block)
test_visibility.trace(span_name, type: type, tags: tags, &block)
end

# The active, unfinished custom (i.e. not test/suite/module/session) span.
Expand All @@ -326,7 +326,7 @@ def trace(span_name, type: "span", tags: {}, &block)
# @return [Datadog::CI::Span] the active span
# @return [nil] if no span is active, or if the active span is not a custom span
def active_span
span = recorder.active_span
span = test_visibility.active_span
span if span && !Ext::AppTypes::CI_SPAN_TYPES.include?(span.type)
end

Expand All @@ -352,7 +352,7 @@ def active_span
# @return [Datadog::CI::Test] the active test
# @return [nil] if no test is active
def active_test
recorder.active_test
test_visibility.active_test
end

private
Expand All @@ -361,8 +361,8 @@ def components
Datadog.send(:components)
end

def recorder
components.ci_recorder
def test_visibility
components.test_visibility
end

def itr_runner
Expand Down
13 changes: 6 additions & 7 deletions lib/datadog/ci/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
require_relative "../itr/runner"
require_relative "../itr/coverage/transport"
require_relative "../itr/coverage/writer"
require_relative "../test_visibility/component"
require_relative "../test_visibility/flush"
require_relative "../test_visibility/recorder"
require_relative "../test_visibility/null_recorder"
require_relative "../test_visibility/null_component"
require_relative "../test_visibility/serializers/factories/test_level"
require_relative "../test_visibility/serializers/factories/test_suite_level"
require_relative "../test_visibility/transport"
Expand All @@ -21,11 +21,11 @@ module CI
module Configuration
# Adds CI behavior to Datadog trace components
module Components
attr_reader :ci_recorder, :itr
attr_reader :test_visibility, :itr

def initialize(settings)
@itr = nil
@ci_recorder = TestVisibility::NullRecorder.new
@test_visibility = TestVisibility::NullComponent.new

# Activate CI mode if enabled
if settings.ci.enabled
Expand All @@ -38,7 +38,7 @@ def initialize(settings)
def shutdown!(replacement = nil)
super

@ci_recorder&.shutdown!
@test_visibility&.shutdown!
@itr&.shutdown!
end

Expand Down Expand Up @@ -110,8 +110,7 @@ def activate_ci!(settings)
use_single_threaded_coverage: settings.ci.itr_code_coverage_use_single_threaded_mode
)

# CI visibility recorder global instance
@ci_recorder = TestVisibility::Recorder.new(
@test_visibility = TestVisibility::Component.new(
test_suite_level_visibility_enabled: !settings.ci.force_test_level_visibility,
itr: @itr,
remote_settings_api: build_remote_settings_client(settings, test_visibility_api),
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ def to_s

private

# provides access to global CI recorder for CI models to deactivate themselves
def recorder
Datadog.send(:components).ci_recorder
# provides access to the test visibility component for CI models to deactivate themselves
def test_visibility
Datadog.send(:components).test_visibility
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def name
# Finishes the current test.
# @return [void]
def finish
recorder.deactivate_test
test_visibility.deactivate_test

super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestModule < ConcurrentSpan
# Finishes this test module.
# @return [void]
def finish
recorder.deactivate_test_module
test_visibility.deactivate_test_module

super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestSession < ConcurrentSpan
# Finishes the current test session.
# @return [void]
def finish
recorder.deactivate_test_session
test_visibility.deactivate_test_session

super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def finish
# we try to derive test suite status from execution stats if no status was set explicitly
set_status_from_stats! if undefined?

recorder.deactivate_test_suite(name)
test_visibility.deactivate_test_suite(name)

super
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module CI
module TestVisibility
# Common behavior for CI tests
# Note: this class has too many responsibilities and should be split into multiple classes
class Recorder
class Component
attr_reader :environment_tags, :test_suite_level_visibility_enabled

def initialize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# frozen_string_literal: true

require_relative "recorder"

module Datadog
module CI
module TestVisibility
# Special recorder that does not record anything
class NullRecorder
# Special test visibility component that does not record anything
class NullComponent
def start_test_session(service: nil, tags: {})
skip_tracing
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Datadog

def self.components: () -> Datadog::CI::Configuration::Components

def self.recorder: () -> (Datadog::CI::TestVisibility::Recorder | Datadog::CI::TestVisibility::NullRecorder)
def self.test_visibility: () -> (Datadog::CI::TestVisibility::Component | Datadog::CI::TestVisibility::NullComponent)

def self.itr_runner: () -> Datadog::CI::ITR::Runner?
end
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/ci/configuration/components.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module Datadog
module CI
module Configuration
module Components : Datadog::Core::Configuration::Components
@ci_recorder: Datadog::CI::TestVisibility::Recorder | Datadog::CI::TestVisibility::NullRecorder
@test_visibility: Datadog::CI::TestVisibility::Component | Datadog::CI::TestVisibility::NullComponent
@itr: Datadog::CI::ITR::Runner?
@custom_configuration: Hash[String, String]

attr_reader ci_recorder: Datadog::CI::TestVisibility::Recorder | Datadog::CI::TestVisibility::NullRecorder
attr_reader test_visibility: Datadog::CI::TestVisibility::Component | Datadog::CI::TestVisibility::NullComponent
attr_reader itr: Datadog::CI::ITR::Runner?

def initialize: (untyped settings) -> void
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module Datadog

private

def recorder: () -> Datadog::CI::TestVisibility::Recorder
def test_visibility: () -> Datadog::CI::TestVisibility::Component
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Datadog
module CI
module TestVisibility
class Recorder
class Component
@test_suite_level_visibility_enabled: bool

@environment_tags: Hash[String, String]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Datadog
module CI
module TestVisibility
class NullRecorder
class NullComponent
def initialize: (?untyped args) -> void

def trace_test: (String span_name, String test_suite_name, ?service: String?, ?tags: Hash[untyped, untyped]) ?{ (nil span) -> untyped } -> untyped
Expand All @@ -26,6 +26,8 @@ module Datadog

def shutdown!: () -> nil

def itr_enabled?: () -> bool

private

def skip_tracing: (?untyped block) -> nil
Expand Down
24 changes: 12 additions & 12 deletions spec/datadog/ci/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@
let(:evp_proxy_v2_supported) { true }

context "is false" do
it "creates a CI recorder with test_suite_level_visibility_enabled=true" do
expect(components.ci_recorder).to be_kind_of(Datadog::CI::TestVisibility::Recorder)
expect(components.ci_recorder.test_suite_level_visibility_enabled).to eq(true)
it "creates test visibility component with test_suite_level_visibility_enabled=true" do
expect(components.test_visibility).to be_kind_of(Datadog::CI::TestVisibility::Component)
expect(components.test_visibility.test_suite_level_visibility_enabled).to eq(true)
end
end

context "is true" do
let(:force_test_level_visibility) { true }

it "creates a CI recorder with test_suite_level_visibility_enabled=false" do
expect(components.ci_recorder).to be_kind_of(Datadog::CI::TestVisibility::Recorder)
expect(components.ci_recorder.test_suite_level_visibility_enabled).to eq(false)
it "creates test visibility component with test_suite_level_visibility_enabled=false" do
expect(components.test_visibility).to be_kind_of(Datadog::CI::TestVisibility::Component)
expect(components.test_visibility.test_suite_level_visibility_enabled).to eq(false)
end
end
end
Expand Down Expand Up @@ -190,7 +190,7 @@
expect(options[:transport]).to be_nil
end

expect(components.ci_recorder.itr_enabled?).to eq(false)
expect(components.test_visibility.itr_enabled?).to eq(false)
end
end
end
Expand Down Expand Up @@ -239,16 +239,16 @@
context "when ITR is disabled" do
let(:itr_enabled) { false }

it "creates a CI recorder with ITR disabled" do
expect(components.ci_recorder.itr_enabled?).to eq(false)
it "creates test visibility component with ITR disabled" do
expect(components.test_visibility.itr_enabled?).to eq(false)
end
end

context "when ITR is enabled" do
let(:itr_enabled) { true }

it "creates a CI recorder with ITR enabled" do
expect(components.ci_recorder.itr_enabled?).to eq(true)
it "creates test visibility component with ITR enabled" do
expect(components.test_visibility.itr_enabled?).to eq(true)
end
end
end
Expand All @@ -261,7 +261,7 @@
expect(Datadog.logger).to have_received(:error)

expect(settings.ci.enabled).to eq(false)
expect(components.ci_recorder.itr_enabled?).to eq(false)
expect(components.test_visibility.itr_enabled?).to eq(false)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/datadog/ci/test_module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

RSpec.describe Datadog::CI::TestModule do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, finish: true) }
let(:recorder) { spy("recorder") }
let(:test_visibility) { spy("test_visibility") }

before { allow_any_instance_of(described_class).to receive(:recorder).and_return(recorder) }
before { allow_any_instance_of(described_class).to receive(:test_visibility).and_return(test_visibility) }

describe "#finish" do
subject(:ci_test_module) { described_class.new(tracer_span) }

it "deactivates the test module" do
ci_test_module.finish

expect(recorder).to have_received(:deactivate_test_module)
expect(test_visibility).to have_received(:deactivate_test_module)
end
end
end
6 changes: 3 additions & 3 deletions spec/datadog/ci/test_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

RSpec.describe Datadog::CI::TestSession do
let(:tracer_span) { Datadog::Tracing::SpanOperation.new("session") }
let(:recorder) { spy("recorder") }
let(:test_visibility) { spy("test_visibility") }

before { allow_any_instance_of(described_class).to receive(:recorder).and_return(recorder) }
before { allow_any_instance_of(described_class).to receive(:test_visibility).and_return(test_visibility) }
subject(:ci_test_session) { described_class.new(tracer_span) }

describe "#finish" do
it "deactivates the test session" do
ci_test_session.finish

expect(recorder).to have_received(:deactivate_test_session)
expect(test_visibility).to have_received(:deactivate_test_session)
end
end

Expand Down
Loading

0 comments on commit a891eba

Please sign in to comment.