From 65c8cc7923ff380bab676d26d19a19c36da0b2d4 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Thu, 30 Nov 2023 15:44:34 +0100 Subject: [PATCH] extract building span options and move global service and tags to Context::Global --- lib/datadog/ci/context/global.rb | 19 ++++++++++ lib/datadog/ci/recorder.rb | 50 ++++++++++++-------------- sig/datadog/ci/context/global.rbs | 4 +++ sig/datadog/ci/recorder.rbs | 2 ++ spec/datadog/ci/context/global_spec.rb | 41 ++++++++++++++++++++- 5 files changed, 88 insertions(+), 28 deletions(-) diff --git a/lib/datadog/ci/context/global.rb b/lib/datadog/ci/context/global.rb index a5f60065..9de6aa32 100644 --- a/lib/datadog/ci/context/global.rb +++ b/lib/datadog/ci/context/global.rb @@ -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? diff --git a/lib/datadog/ci/recorder.rb b/lib/datadog/ci/recorder.rb index 4f47b540..6c99777b 100644 --- a/lib/datadog/ci/recorder.rb +++ b/lib/datadog/ci/recorder.rb @@ -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 @@ -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 @@ -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 @@ -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| @@ -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| @@ -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 diff --git a/sig/datadog/ci/context/global.rbs b/sig/datadog/ci/context/global.rbs index 874285e9..f0a2ed93 100644 --- a/sig/datadog/ci/context/global.rbs +++ b/sig/datadog/ci/context/global.rbs @@ -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 diff --git a/sig/datadog/ci/recorder.rbs b/sig/datadog/ci/recorder.rbs index aa78f58c..ea59a90d 100644 --- a/sig/datadog/ci/recorder.rbs +++ b/sig/datadog/ci/recorder.rbs @@ -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 diff --git a/spec/datadog/ci/context/global_spec.rb b/spec/datadog/ci/context/global_spec.rb index 75900c5b..21573f79 100644 --- a/spec/datadog/ci/context/global_spec.rb +++ b/spec/datadog/ci/context/global_spec.rb @@ -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) } @@ -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