Skip to content

Commit

Permalink
wip test session to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 21, 2023
1 parent a1d2e4b commit 1ab459c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 4 deletions.
34 changes: 34 additions & 0 deletions lib/datadog/ci/context/global.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Datadog
module CI
module Context
class Global
def initialize
@mutex = Mutex.new
@test_session = nil
end

def activate_test_session!(test_session)
@mutex.synchronize do
raise "Nested test sessions are not supported. Currently active test session: #{@test_session}" unless @test_session.nil?

@test_session = test_session
end
end

def deactivate_test_session!(test_session)
@mutex.synchronize do
return if @test_session.nil?

if @test_session == test_session
@test_session = nil
else
raise "Trying to deactivate test session #{test_session}, but currently active test session is #{@test_session}"
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/datadog/ci/ext/app_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module CI
module Ext
module AppTypes
TYPE_TEST = "test"
TYPE_TEST_SESSION = "test_session_end"
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

require_relative "span"
require_relative "test"
require_relative "test_session"

module Datadog
module CI
Expand All @@ -22,6 +23,24 @@ class Recorder
def initialize
@environment_tags = Ext::Environment.tags(ENV).freeze
@local_context = Context::Local.new
@global_context = Context::Global.new
end

def start_test_session(service_name: nil, tags: {})
span_options = {
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST_SESSION
}

tracer_span = Datadog::Tracing.trace("test.session", **span_options)
trace = Datadog::Tracing.active_trace

set_trace_origin(trace)

test_session = build_test_session(tracer_span, tags)
@global_context.activate_test_session!(test_session)

test_session
end

# Creates a new span for a CI test
Expand Down Expand Up @@ -93,6 +112,18 @@ def set_trace_origin(trace)
trace.origin = Ext::Test::CONTEXT_ORIGIN if trace
end

def build_test_session(tracer_span, tags)
test_session = TestSession.new(tracer_span)

test_session.set_default_tags
test_session.set_environment_runtime_tags

test_session.set_tags(tags)
test_session.set_tags(environment_tags)

test_session
end

def build_test(tracer_span, tags)
test = Test.new(tracer_span)

Expand Down
1 change: 0 additions & 1 deletion lib/datadog/ci/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
module Datadog
module CI
# Represents a single part of a test run.
# Could be a session, suite, test, or any custom span.
#
# @public_api
class Test < Span
Expand Down
19 changes: 19 additions & 0 deletions lib/datadog/ci/test_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require_relative "span"

module Datadog
module CI
# Represents the whole test command process.
# This object can be shared between multiple threads.
#
# @public_api
class TestSession < Span
def initialize(tracer_span)
super

@mutex = Mutex.new
end
end
end
end
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_visibility/serializers/test_v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def content_map_size
end

def type
"test"
Ext::AppTypes::TYPE_TEST
end

def name
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/ci/ext/app_types.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Datadog
module CI
module Ext
module AppTypes
TYPE_TEST: String
TYPE_TEST: "test"
TYPE_TEST_SESSION: "test_session_end"
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/ci/test_session.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Datadog
module CI
class TestSession < Span
end
end
end
2 changes: 1 addition & 1 deletion sig/datadog/ci/test_visibility/serializers/test_v1.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Datadog
def content_fields: () -> Array[String | Hash[String, String]]
def content_map_size: () -> Integer

def type: () -> "test"
def type: () -> ::String

def name: () -> ::String

Expand Down

0 comments on commit 1ab459c

Please sign in to comment.