Skip to content

Commit

Permalink
extract building span options and move global service and tags to Con…
Browse files Browse the repository at this point in the history
…text::Global
  • Loading branch information
anmarchenko committed Nov 30, 2023
1 parent a97ad49 commit 65c8cc7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 28 deletions.
19 changes: 19 additions & 0 deletions lib/datadog/ci/context/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ def active_test_session
@test_session
end

def service
@mutex.synchronize do
# thank you RBS for this weirdness
test_session = @test_session
test_session.service if test_session
end
end

def inheritable_session_tags
@mutex.synchronize do
test_session = @test_session
if test_session
test_session.inheritable_tags
else
{}
end
end
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?
Expand Down
50 changes: 23 additions & 27 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ def initialize(enabled: true, test_suite_level_visibility_enabled: false)
def start_test_session(service_name: nil, tags: {})
return skip_tracing unless test_suite_level_visibility_enabled

span_options = {
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST_SESSION
}

tracer_span = start_datadog_tracer_span("test.session", span_options)
tracer_span = start_datadog_tracer_span(
"test.session", build_span_options(service_name, Ext::AppTypes::TYPE_TEST_SESSION)
)

tags[Ext::Test::TAG_TEST_SESSION_ID] = tracer_span.id

Expand All @@ -56,19 +53,14 @@ def start_test_module(test_module_name, service_name: nil, tags: {})

test_session = active_test_session
if test_session
service_name ||= test_session.service

tags = test_session.inheritable_tags.merge(tags)

tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id
end

span_options = {
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST_MODULE
}

tracer_span = start_datadog_tracer_span(test_module_name, span_options)
tracer_span = start_datadog_tracer_span(
test_module_name, build_span_options(service_name, Ext::AppTypes::TYPE_TEST_MODULE)
)

tags[Ext::Test::TAG_TEST_MODULE_ID] = tracer_span.id
tags[Ext::Test::TAG_MODULE] = tracer_span.name
Expand All @@ -85,8 +77,6 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &

test_session = active_test_session
if test_session
service_name ||= test_session.service

tags = test_session.inheritable_tags.merge(tags)

tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id
Expand All @@ -100,13 +90,11 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &

tags[Ext::Test::TAG_NAME] = test_name

span_options = {
resource: test_name,
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST,
# this option is needed to force a new trace to be created
continue_from: Datadog::Tracing::TraceDigest.new
}
span_options = build_span_options(
service_name,
Ext::AppTypes::TYPE_TEST,
{resource: test_name, continue_from: Datadog::Tracing::TraceDigest.new}
)

if block
start_datadog_tracer_span(operation_name, span_options) do |tracer_span|
Expand All @@ -128,10 +116,11 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &
def trace(span_type, span_name, tags: {}, &block)
return skip_tracing(block) unless enabled

span_options = {
resource: span_name,
span_type: span_type
}
span_options = build_span_options(
nil, # service name is completely optional for custom spans
span_type,
{resource: span_name}
)

if block
start_datadog_tracer_span(span_name, span_options) do |tracer_span|
Expand Down Expand Up @@ -213,6 +202,13 @@ def build_span(tracer_span, tags)
span
end

def build_span_options(service_name, span_type, other_options = {})
other_options[:service] = service_name || @global_context.service
other_options[:span_type] = span_type

other_options
end

def set_initial_tags(ci_span, tags)
ci_span.set_default_tags
ci_span.set_environment_runtime_tags
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/ci/context/global.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module Datadog

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

def service: () -> String?

def inheritable_session_tags: () -> Hash[untyped, untyped]

def active_test_module: () -> Datadog::CI::TestModule?

def activate_test_session!: (Datadog::CI::TestSession test_session) -> void
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 @@ -52,6 +52,8 @@ module Datadog

def build_span: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Span

def build_span_options: (String? service_name, String span_type, ?Hash[Symbol, untyped] other_options) -> Hash[Symbol, untyped]

def set_initial_tags: (Datadog::CI::Span ci_span, Hash[untyped, untyped] tags) -> void

def null_span: () -> Datadog::CI::Span
Expand Down
41 changes: 40 additions & 1 deletion spec/datadog/ci/context/global_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe Datadog::CI::Context::Global do
subject { described_class.new }

let(:tracer_span) { double(Datadog::Tracing::SpanOperation, name: "test.session") }
let(:tracer_span) { double(Datadog::Tracing::SpanOperation, name: "test.session", service: "my-service") }
let(:session) { Datadog::CI::TestSession.new(tracer_span) }
let(:test_module) { Datadog::CI::TestModule.new(tracer_span) }

Expand Down Expand Up @@ -30,6 +30,45 @@
end
end

describe "#service" do
context "when a test session is active" do
before do
subject.activate_test_session!(session)
end

it "returns the service name" do
expect(subject.service).to eq("my-service")
end
end

context "when no test session is active" do
it "returns nil" do
expect(subject.service).to be_nil
end
end
end

describe "#inheritable_session_tags" do
context "when a test session is active" do
let(:inheritable_tags) { {"my.session.tag" => "my.session.tag.value"} }
before do
expect(session).to receive(:inheritable_tags).and_return(inheritable_tags)

subject.activate_test_session!(session)
end

it "returns the inheritable session tags" do
expect(subject.inheritable_session_tags).to eq(inheritable_tags)
end
end

context "when no test session is active" do
it "returns an empty hash" do
expect(subject.inheritable_session_tags).to eq({})
end
end
end

describe "active_test_session" do
context "when a test session is active" do
before do
Expand Down

0 comments on commit 65c8cc7

Please sign in to comment.