Skip to content

Commit

Permalink
Merge pull request #269 from DataDog/anmarchenko/send_info_if_user_co…
Browse files Browse the repository at this point in the history
…nfigured_service

[SDTEST-1245] Internal: send _dd.test.is_user_provided_service tag to track if test_service is configured
  • Loading branch information
anmarchenko authored Dec 16, 2024
2 parents c2b203f + a2c6aeb commit 2f38d79
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions lib/datadog/ci/test_visibility/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require_relative "../ext/environment"
require_relative "../ext/test"

require_relative "../utils/configuration"
require_relative "../utils/test_run"

require_relative "../span"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/utils/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/utils/configuration.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"} }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\"]"
Expand Down
20 changes: 20 additions & 0 deletions spec/datadog/ci/utils/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/support/contexts/ci_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2f38d79

Please sign in to comment.