Skip to content

Commit

Permalink
Merge pull request #226 from DataDog/anmarchenko/itr_fix_code_coverag…
Browse files Browse the repository at this point in the history
…e_for_retries_and_telemetry

[SDTEST-740] Minor telemetry fixes
  • Loading branch information
anmarchenko authored Aug 28, 2024
2 parents 1da7397 + c82db0d commit c8cb890
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/datadog/ci/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def skipped_by_itr?
get_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR) == "true"
end

# Returns "true" if test span represents a retry.
# @return [Boolean] true if this test is a retry, false otherwise.
def is_retry?
get_tag(Ext::Test::TAG_IS_RETRY) == "true"
end

# Marks this test as unskippable by the intelligent test runner.
# This must be done before the test execution starts.
#
Expand Down
7 changes: 5 additions & 2 deletions lib/datadog/ci/test_optimisation/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def code_coverage?

def start_coverage(test)
return if !enabled? || !code_coverage?

Telemetry.code_coverage_started(test)
coverage_collector&.start
end
Expand All @@ -109,13 +110,15 @@ def stop_coverage(test)
Telemetry.code_coverage_finished(test)

coverage = coverage_collector&.stop

# if test was skipped, we discard coverage data
return if test.skipped?

if coverage.nil? || coverage.empty?
Telemetry.code_coverage_is_empty
return
end

return if test.skipped?

test_source_file = test.source_file

# cucumber's gherkin files are not covered by the code coverage collector
Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Datadog
def itr_unskippable!: () -> void
def source_file: () -> String?
def parameters: () -> String?
def is_retry?: () -> bool

private

Expand Down
19 changes: 19 additions & 0 deletions spec/datadog/ci/test_optimisation/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@
expect(subject).to be_nil
expect(writer).not_to have_received(:write)
end

it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_CODE_COVERAGE_IS_EMPTY
end

context "when test is skipped and coverage is not collected" do
before do
test_span.skipped!
end

it "does not write coverage event" do
expect(subject).to be_nil
expect(writer).not_to have_received(:write)
end

it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_CODE_COVERAGE_IS_EMPTY
end

context "when coverage was not collected" do
Expand Down Expand Up @@ -333,6 +348,8 @@
expect { subject }
.not_to change { component.skipped_tests_count }
end

it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_ITR_SKIPPED
end

context "test is skipped by ITR" do
Expand Down Expand Up @@ -363,6 +380,8 @@
expect { subject }
.not_to change { component.skipped_tests_count }
end

it_behaves_like "emits no metric", :inc, Datadog::CI::Ext::Telemetry::METRIC_ITR_SKIPPED
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/datadog/ci/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,24 @@
expect(ci_test.parameters).to eq(parameters)
end
end

describe "#is_retry?" do
subject(:is_retry) { ci_test.is_retry? }

context "when tag is set" do
before do
allow(tracer_span).to(
receive(:get_tag).with(Datadog::CI::Ext::Test::TAG_IS_RETRY).and_return("true")
)
end

it { is_expected.to be true }
end

context "when tag is not set" do
before { allow(tracer_span).to receive(:get_tag).with(Datadog::CI::Ext::Test::TAG_IS_RETRY).and_return(nil) }

it { is_expected.to be false }
end
end
end
9 changes: 9 additions & 0 deletions spec/support/contexts/telemetry_spy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ def telemetry_spy_value_suffix(value)
end
end

shared_examples_for "emits no metric" do |metric_type, metric_name|
it "emits no :#{metric_type} metric #{metric_name}" do
subject

metric = telemetry_metric(metric_type, metric_name)
expect(metric).to be_nil
end
end

def telemetry_metric(type, name)
@metrics[type].find { |m| m.name == name }
end
Expand Down

0 comments on commit c8cb890

Please sign in to comment.