Skip to content

Commit

Permalink
inherit tags from the running test session when starting a test
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 22, 2023
1 parent 076d24c commit 2413f61
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module Test
TAG_TRAITS = "test.traits"
TAG_TYPE = "test.type"

# tags that are inherited from the test session
INHERITED_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION, TAG_TYPE].freeze
# tags that can be inherited from the test session
INHERITABLE_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION, TAG_TYPE].freeze

# Environment runtime tags
TAG_OS_ARCHITECTURE = "os.architecture"
Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ 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)
end

span_options = {
Expand Down
9 changes: 9 additions & 0 deletions lib/datadog/ci/test_session.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "concurrent_span"
require_relative "ext/test"

module Datadog
module CI
Expand All @@ -16,6 +17,14 @@ def finish

CI.deactivate_test_session
end

def inheritable_tags
res = {}
Ext::Test::INHERITABLE_TAGS.each do |tag|
res[tag] = get_tag(tag)
end
res
end
end
end
end
2 changes: 1 addition & 1 deletion sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Datadog

TAG_TYPE: String

INHERITED_TAGS: Array[String]
INHERITABLE_TAGS: Array[String]

TAG_OS_ARCHITECTURE: String

Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/test_session.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Datadog
module CI
class TestSession < ConcurrentSpan
def inheritable_tags: () -> Hash[untyped, untyped]
end
end
end
22 changes: 18 additions & 4 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:operation_name) { "span name" }
let(:test_name) { "test name" }
let(:tags) { {} }
let(:expected_tags) { tags }
let(:expected_tags) { {} }
let(:environment_tags) { Datadog::CI::Ext::Environment.tags(ENV) }

let(:ci_span) do
Expand Down Expand Up @@ -36,6 +36,8 @@
end

describe "#trace_test" do
let(:expected_tags) { {"test.name" => test_name} }

context "when given a block" do
subject(:trace) do
recorder.trace_test(
Expand Down Expand Up @@ -118,13 +120,19 @@
end

context "when test session is active" do
let(:inheritable_tags) { {"test.framework" => "my framework"} }
let(:test_session_service) { "my-test-service" }
let(:test_session) { instance_double(Datadog::CI::TestSession, service: test_session_service) }
let(:test_session) do
instance_double(Datadog::CI::TestSession, service: test_session_service, inheritable_tags: inheritable_tags)
end

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

context "when service name is not given" do
context "when service name and tags are not given" do
let(:expected_tags) { {"test.framework" => "my framework", "test.name" => test_name} }

subject(:trace) do
recorder.trace_test(
test_name,
Expand All @@ -151,10 +159,14 @@
trace
end

it_behaves_like "initialize ci span with tags"
it { is_expected.to be(ci_span) }
end

context "when service name is given" do
context "when service name and tags are given" do
let(:tags) { {"test.framework" => "special test framework"} }
let(:expected_tags) { {"test.framework" => "special test framework", "test.name" => test_name} }

subject(:trace) do
recorder.trace_test(
test_name,
Expand Down Expand Up @@ -182,6 +194,7 @@
trace
end

it_behaves_like "initialize ci span with tags"
it { is_expected.to be(ci_span) }
end
end
Expand All @@ -190,6 +203,7 @@

describe "#trace" do
let(:tags) { {"my_tag" => "my_value"} }
let(:expected_tags) { {"my_tag" => "my_value"} }
let(:span_type) { "step" }
let(:span_name) { "span name" }

Expand Down

0 comments on commit 2413f61

Please sign in to comment.