diff --git a/lib/datadog/ci/recorder.rb b/lib/datadog/ci/recorder.rb index 38562dbf..e70ef6a1 100644 --- a/lib/datadog/ci/recorder.rb +++ b/lib/datadog/ci/recorder.rb @@ -93,8 +93,18 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, & service_name ||= test_session.service tags = test_session.inheritable_tags.merge(tags) + + tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id + end + + test_module = active_test_module + if test_module + tags[Ext::Test::TAG_TEST_MODULE_ID] = test_module.id + tags[Ext::Test::TAG_MODULE] = test_module.name end + tags[Ext::Test::TAG_NAME] = test_name + span_options = { resource: test_name, service: service_name, @@ -103,9 +113,6 @@ def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, & continue_from: Datadog::Tracing::TraceDigest.new } - tags[Ext::Test::TAG_NAME] = test_name - tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id if test_session - if block Datadog::Tracing.trace(operation_name, **span_options) do |tracer_span, trace| set_trace_origin(trace) diff --git a/spec/datadog/ci/recorder_spec.rb b/spec/datadog/ci/recorder_spec.rb index c8fa88a3..d1b2a715 100644 --- a/spec/datadog/ci/recorder_spec.rb +++ b/spec/datadog/ci/recorder_spec.rb @@ -250,36 +250,66 @@ test_session end - it "returns a new CI test span using service from the test session" do - expect(subject).to be_kind_of(Datadog::CI::Test) - expect(subject.name).to eq(test_name) - expect(subject.service).to eq(session_service_name) - end + context "when there is no active test module" do + it "returns a new CI test span using service from the test session" do + expect(subject).to be_kind_of(Datadog::CI::Test) + expect(subject.name).to eq(test_name) + expect(subject.service).to eq(session_service_name) + end - it "sets the provided tags correctly while inheriting some tags from the session" do - expect(subject.get_tag("test.framework")).to eq("my-framework") - expect(subject.get_tag("test.framework_version")).to eq("1.0") - expect(subject.get_tag("my.tag")).to eq("my_value") - expect(subject.get_tag("my.session.tag")).to be_nil - end + it "sets the provided tags correctly while inheriting some tags from the session" do + expect(subject.get_tag("test.framework")).to eq("my-framework") + expect(subject.get_tag("test.framework_version")).to eq("1.0") + expect(subject.get_tag("my.tag")).to eq("my_value") + expect(subject.get_tag("my.session.tag")).to be_nil + end - it "connects the test span to the test session" do - expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session.id.to_s) - end + it "connects the test span to the test session" do + expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session.id.to_s) + end + + it "starts a new trace" do + expect(subject.tracer_span.trace_id).not_to eq(test_session.tracer_span.trace_id) + end - it "starts a new trace" do - expect(subject.tracer_span.trace_id).not_to eq(test_session.tracer_span.trace_id) + it_behaves_like "span with environment tags" + it_behaves_like "span with default tags" + it_behaves_like "span with runtime tags" + + it_behaves_like "trace with ciapp-test origin" do + let(:trace_under_test) do + subject.finish + + trace + end + end end - it_behaves_like "span with environment tags" - it_behaves_like "span with default tags" - it_behaves_like "span with runtime tags" + context "when there is an active test module" do + let(:module_name) { "my-module" } - it_behaves_like "trace with ciapp-test origin" do - let(:trace_under_test) do - subject.finish + let(:test_module) do + recorder.start_test_module(module_name) + end - trace + before do + test_module + end + + it "returns a new CI test span" do + expect(subject).to be_kind_of(Datadog::CI::Test) + expect(subject.name).to eq(test_name) + end + + it "sets the provided tags correctly while inheriting some tags from the session" do + expect(subject.get_tag("test.framework")).to eq("my-framework") + expect(subject.get_tag("test.framework_version")).to eq("1.0") + expect(subject.get_tag("my.tag")).to eq("my_value") + end + + it "connects the test span to the test module" do + expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(test_module.id.to_s) + expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(module_name) end end end