From dc8a625aa2f6ec9a4a7b4872b8ae24a053ad1101 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 13 Dec 2024 15:31:59 +0100 Subject: [PATCH 1/3] add Utils::Configuration.service_name_provided_by_user? global function --- lib/datadog/ci/utils/configuration.rb | 4 ++++ sig/datadog/ci/utils/configuration.rbs | 2 ++ spec/datadog/ci/utils/configuration_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/datadog/ci/utils/configuration.rb b/lib/datadog/ci/utils/configuration.rb index af559bae..31371418 100644 --- a/lib/datadog/ci/utils/configuration.rb +++ b/lib/datadog/ci/utils/configuration.rb @@ -9,6 +9,10 @@ module Configuration def self.fetch_service_name(default) Datadog.configuration.service_without_fallback || CI::Git::LocalRepository.repository_name || default end + + def self.service_name_provided_by_user? + !!Datadog.configuration.service_without_fallback + end end end end diff --git a/sig/datadog/ci/utils/configuration.rbs b/sig/datadog/ci/utils/configuration.rbs index 16977dac..7b02f6e6 100644 --- a/sig/datadog/ci/utils/configuration.rbs +++ b/sig/datadog/ci/utils/configuration.rbs @@ -3,6 +3,8 @@ module Datadog module Utils module Configuration def self.fetch_service_name: (String default) -> String + + def self.service_name_provided_by_user?: () -> bool end end end diff --git a/spec/datadog/ci/utils/configuration_spec.rb b/spec/datadog/ci/utils/configuration_spec.rb index 0901885a..853a3db6 100644 --- a/spec/datadog/ci/utils/configuration_spec.rb +++ b/spec/datadog/ci/utils/configuration_spec.rb @@ -38,4 +38,24 @@ end end end + + describe ".service_name_provided_by_user?" do + subject { described_class.service_name_provided_by_user? } + + before do + allow(::Datadog.configuration).to receive(:service_without_fallback).and_return(service) + end + + context "when service is set in Datadog config" do + let(:service) { "service_without_fallback" } + + it { is_expected.to be true } + end + + context "when service is not set" do + let(:service) { nil } + + it { is_expected.to be false } + end + end end From 7d4a0769089bf22e1f82e0e40d150e9f6bc4e258 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 16 Dec 2024 11:31:28 +0100 Subject: [PATCH 2/3] add constant for _dd.test.is_user_provided_service tag --- lib/datadog/ci/ext/test.rb | 3 +++ sig/datadog/ci/ext/test.rbs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lib/datadog/ci/ext/test.rb b/lib/datadog/ci/ext/test.rb index 17a15a6d..1cd046e0 100644 --- a/lib/datadog/ci/ext/test.rb +++ b/lib/datadog/ci/ext/test.rb @@ -74,6 +74,9 @@ module Test # common tags that are serialized directly in msgpack header in metadata field METADATA_TAG_TEST_SESSION_NAME = "test_session.name" + # internal tag indicating if datadog service was configured by the user + TAG_USER_PROVIDED_TEST_SERVICE = "_dd.test.is_user_provided_service" + # internal metric with the number of virtual CPUs METRIC_CPU_COUNT = "_dd.host.vcpu_count" diff --git a/sig/datadog/ci/ext/test.rbs b/sig/datadog/ci/ext/test.rbs index b466d82d..6861f323 100644 --- a/sig/datadog/ci/ext/test.rbs +++ b/sig/datadog/ci/ext/test.rbs @@ -98,6 +98,8 @@ module Datadog METADATA_TAG_TEST_SESSION_NAME: "test_session.name" + TAG_USER_PROVIDED_TEST_SERVICE: "_dd.test.is_user_provided_service" + METRIC_CPU_COUNT: "_dd.host.vcpu_count" TAG_CODE_COVERAGE_LINES_PCT: "test.code_coverage.lines_pct" From a2c6aeb98918a84d3de02633b6960e8e57394d06 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 16 Dec 2024 13:17:24 +0100 Subject: [PATCH 3/3] set _dd.test.is_user_provided_service for all spans if user has configured test_service --- lib/datadog/ci/test_visibility/context.rb | 5 +++++ .../contrib/minitest/instrumentation_spec.rb | 21 ++++++++++++++++--- spec/support/contexts/ci_mode.rb | 5 +++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/datadog/ci/test_visibility/context.rb b/lib/datadog/ci/test_visibility/context.rb index 37af17e0..d9045932 100644 --- a/lib/datadog/ci/test_visibility/context.rb +++ b/lib/datadog/ci/test_visibility/context.rb @@ -11,6 +11,7 @@ require_relative "../ext/environment" require_relative "../ext/test" +require_relative "../utils/configuration" require_relative "../utils/test_run" require_relative "../span" @@ -207,6 +208,10 @@ def set_initial_tags(ci_span, tags) ci_span.set_tags(@environment_tags) ci_span.set_metric(Ext::Test::METRIC_CPU_COUNT, Utils::TestRun.virtual_cpu_count) + ci_span.set_tag( + Ext::Test::TAG_USER_PROVIDED_TEST_SERVICE, + Utils::Configuration.service_name_provided_by_user?.to_s + ) end # PROPAGATING CONTEXT FROM TOP-LEVEL TO THE LOWER LEVELS diff --git a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb index 636715be..4464549a 100644 --- a/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/minitest/instrumentation_spec.rb @@ -35,10 +35,22 @@ def test_foo expect(span.service).to eq("datadog-ci-rb") end + + it "sets user_provided_service tag to false" do + klass = Class.new(Minitest::Test) do + def test_foo + end + end + + klass.new(:test_foo).run + + expect(span).to have_test_tag(:user_provided_test_service, "false") + end end context "with service name configured and code coverage enabled" do include_context "CI mode activated" do + let(:service_name) { "ltest" } let(:integration_name) { :minitest } let(:integration_options) { {service_name: "ltest"} } @@ -88,11 +100,14 @@ def test_foo :source_file, "spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(span).to have_test_tag(:source_start, "61") + expect(span).to have_test_tag(:source_start, "73") expect(span).to have_test_tag( :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" ) + + # test service is provided by user in the configuration + expect(span).to have_test_tag(:user_provided_test_service, "true") end it "creates spans for several tests" do @@ -455,7 +470,7 @@ def test_pass_other expect(test_session_span).to have_pass_status - # test_session metric has auto_injected false + # test_session telemetry metric has auto_injected false test_session_started_metric = telemetry_metric(:inc, "test_session") expect(test_session_started_metric.tags["auto_injected"]).to eq("false") end @@ -492,7 +507,7 @@ def test_pass_other :source_file, "spec/datadog/ci/contrib/minitest/instrumentation_spec.rb" ) - expect(first_test_suite_span).to have_test_tag(:source_start, "419") + expect(first_test_suite_span).to have_test_tag(:source_start, "434") expect(first_test_suite_span).to have_test_tag( :codeowners, "[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]" diff --git a/spec/support/contexts/ci_mode.rb b/spec/support/contexts/ci_mode.rb index f37da894..1b5070c0 100644 --- a/spec/support/contexts/ci_mode.rb +++ b/spec/support/contexts/ci_mode.rb @@ -13,6 +13,7 @@ RSpec.shared_context "CI mode activated" do include CoverageHelpers + let(:service_name) { nil } let(:test_command) { "command" } let(:logical_test_session_name) { nil } let(:integration_name) { :no_instrument } @@ -109,6 +110,10 @@ allow_any_instance_of(Datadog::CI::TestRetries::UniqueTestsClient).to receive(:fetch_unique_tests).and_return(unique_tests_set) Datadog.configure do |c| + if service_name + c.service = service_name + end + # library switch c.ci.enabled = ci_enabled