Skip to content

Commit

Permalink
Merge pull request #203 from DataDog/anmarchenko/telemetry_metrics_co…
Browse files Browse the repository at this point in the history
…de_coverage

[SDTEST-160] Implement code coverage metrics for internal telemetry
  • Loading branch information
anmarchenko authored Jul 29, 2024
2 parents 61ba977 + 5c61e76 commit c7883ab
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 63 deletions.
3 changes: 1 addition & 2 deletions lib/datadog/ci/ext/telemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module Telemetry
METRIC_CODE_COVERAGE_FINISHED = "code_coverage_finished"
METRIC_CODE_COVERAGE_IS_EMPTY = "code_coverage.is_empty"
METRIC_CODE_COVERAGE_FILES = "code_coverage.files"
METRIC_CODE_COVERAGE_ERRORS = "code_coverage.errors"

METRIC_TEST_SESSION = "test_session"

Expand Down Expand Up @@ -86,7 +85,7 @@ module EventType
end

module Library
BUILTIN = "builtin"
CUSTOM = "custom"
end

module Endpoint
Expand Down
12 changes: 10 additions & 2 deletions lib/datadog/ci/test_optimisation/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require_relative "coverage/event"
require_relative "skippable"
require_relative "telemetry"

module Datadog
module CI
Expand Down Expand Up @@ -104,15 +105,20 @@ def code_coverage?

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

Telemetry.code_coverage_started(test)
coverage_collector&.start
end

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

Telemetry.code_coverage_finished(test)

coverage = coverage_collector&.stop
return if coverage.nil? || coverage.empty?
if coverage.nil? || coverage.empty?
Telemetry.code_coverage_is_empty
return
end

return if test.skipped?

Expand All @@ -121,6 +127,8 @@ def stop_coverage(test)
# cucumber's gherkin files are not covered by the code coverage collector
ensure_test_source_covered(test_source_file, coverage) unless test_source_file.nil?

Telemetry.code_coverage_files(coverage.size)

event = Coverage::Event.new(
test_id: test.id.to_s,
test_suite_id: test.test_suite_id.to_s,
Expand Down
37 changes: 37 additions & 0 deletions lib/datadog/ci/test_optimisation/telemetry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative "../ext/telemetry"
require_relative "../ext/test"
require_relative "../utils/telemetry"

module Datadog
module CI
module TestOptimisation
# Telemetry for test optimisation component
module Telemetry
def self.code_coverage_started(test)
Utils::Telemetry.inc(Ext::Telemetry::METRIC_CODE_COVERAGE_STARTED, 1, tags_for_test(test))
end

def self.code_coverage_finished(test)
Utils::Telemetry.inc(Ext::Telemetry::METRIC_CODE_COVERAGE_FINISHED, 1, tags_for_test(test))
end

def self.code_coverage_is_empty
Utils::Telemetry.inc(Ext::Telemetry::METRIC_CODE_COVERAGE_IS_EMPTY, 1)
end

def self.code_coverage_files(count)
Utils::Telemetry.distribution(Ext::Telemetry::METRIC_CODE_COVERAGE_FILES, count.to_f)
end

def self.tags_for_test(test)
{
Ext::Telemetry::TAG_TEST_FRAMEWORK => test.get_tag(Ext::Test::TAG_FRAMEWORK),
Ext::Telemetry::TAG_LIBRARY => Ext::Telemetry::Library::CUSTOM
}
end
end
end
end
end
4 changes: 1 addition & 3 deletions sig/datadog/ci/ext/telemetry.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ module Datadog

METRIC_CODE_COVERAGE_FILES: "code_coverage.files"

METRIC_CODE_COVERAGE_ERRORS: "code_coverage.errors"

METRIC_TEST_SESSION: "test_session"

TAG_TEST_FRAMEWORK: "test_framework"
Expand Down Expand Up @@ -132,7 +130,7 @@ module Datadog
end

module Library
BUILTIN: "builtin"
CUSTOM: "custom"
end

module Endpoint
Expand Down
17 changes: 17 additions & 0 deletions sig/datadog/ci/test_optimisation/telemetry.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Datadog
module CI
module TestOptimisation
module Telemetry
def self.code_coverage_started: (Datadog::CI::Test test) -> void

def self.code_coverage_finished: (Datadog::CI::Test test) -> void

def self.code_coverage_is_empty: () -> void

def self.code_coverage_files: (Integer count) -> void

def self.tags_for_test: (Datadog::CI::Test test) -> ::Hash[String, String]
end
end
end
end
Loading

0 comments on commit c7883ab

Please sign in to comment.