Skip to content

Commit

Permalink
Datadog::CI public methods for test session tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 22, 2023
1 parent b73d971 commit 25feec6
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,54 @@ module Datadog
# @public_api
module CI
class << self
# Return a {Datadog::CI::TestSesstion ci_test_session} that represents the whole test session run.
# Raises an error if a session is already active.
#
#
# The {#start_test_session} method is used to mark the start :
# ```
# Datadog::CI.start_test_session(
# service: "my-web-site-tests",
# tags: { Datadog::CI::Ext::Test::TAG_FRAMEWORK => "my-test-framework" }
# )
#
# # Somewhere else after test run has ended
# Datadog::CI.active_test_session.finish
# ```
#
# Remember that calling {Datadog::CI::TestSession#finish} is mandatory.
#
# @param [String] service_name the service name for this session
# @param [Hash<String,String>] tags extra tags which should be added to the test.
# @return [Datadog::CI::TestSession] returns the active, running {Datadog::CI::TestSession}.
#
# @public_api
def start_test_session(service_name: nil, tags: {})
recorder.start_test_session(service_name: service_name, tags: tags)
end

# The active, unfinished test session span.
#
# Usage:
#
# ```
# # start a test session
# Datadog::CI.start_test_session(
# service: "my-web-site-tests",
# tags: { Datadog::CI::Ext::Test::TAG_FRAMEWORK => "my-test-framework" }
# )
#
# # somewhere else, access the session
# test_session = Datadog::CI.active_test_session
# test_session.finish
# ```
#
# @return [Datadog::CI::TestSession] the active test session
# @return [nil] if no test session is active
def active_test_session
recorder.active_test_session
end

# Return a {Datadog::CI::Test ci_test} that will trace a test called `test_name`.
# Raises an error if a test is already active.
#
Expand Down Expand Up @@ -189,6 +237,11 @@ def deactivate_test(test)
recorder.deactivate_test(test)
end

# Internal only, to finish a test session use Datadog::CI::TestSession#finish
def deactivate_test_session
recorder.deactivate_test_session
end

private

def components
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/ci.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ module Datadog

def self.start_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Test

def self.start_test_session: (?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::TestSession

def self.trace: (String span_type, String span_name, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.active_test_session: () -> Datadog::CI::TestSession?

def self.active_test: () -> Datadog::CI::Test?

def self.active_span: (String span_type) -> Datadog::CI::Span?

def self.deactivate_test: (Datadog::CI::Test test) -> void

def self.deactivate_test_session: () -> void

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

def self.recorder: () -> Datadog::CI::Recorder
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Datadog

def start_test_session: (?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::TestSession

def active_test_session: () -> Datadog::CI::TestSession?

def active_test: () -> Datadog::CI::Test?

def active_span: () -> Datadog::CI::Span?
Expand Down
34 changes: 34 additions & 0 deletions spec/datadog/ci_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,38 @@
it { is_expected.to be_nil }
end
end

describe "::start_test_session" do
subject(:start_test_session) { described_class.start_test_session }

let(:ci_test_session) { instance_double(Datadog::CI::TestSession) }

before do
allow(recorder).to receive(:start_test_session).and_return(ci_test_session)
end

it { is_expected.to be(ci_test_session) }
end

describe "::active_test_session" do
subject(:active_test_session) { described_class.active_test_session }

let(:ci_test_session) { instance_double(Datadog::CI::TestSession) }

before do
allow(recorder).to receive(:active_test_session).and_return(ci_test_session)
end

it { is_expected.to be(ci_test_session) }
end

describe "::deactivate_test_session" do
subject(:deactivate_test_session) { described_class.deactivate_test_session }

before do
allow(recorder).to receive(:deactivate_test_session)
end

it { is_expected.to be_nil }
end
end

0 comments on commit 25feec6

Please sign in to comment.