Class: Datadog::CI::TestTracing::KnownTests

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/test_tracing/known_tests.rb

Overview

fetches and stores a list of known tests from the backend

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initialize(dd_env:, api: nil, config_tags: {}) ⇒ KnownTests

Returns a new instance of KnownTests.



90
91
92
93
94
# File 'lib/datadog/ci/test_tracing/known_tests.rb', line 90

def initialize(dd_env:, api: nil, config_tags: {})
  @api = api
  @dd_env = dd_env
  @config_tags = config_tags
end

Instance Method Details

#fetch(test_session) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/datadog/ci/test_tracing/known_tests.rb', line 96

def fetch(test_session)
  api = @api
  return Set.new unless api

  result = Set.new
  total_request_ms = 0.0
  page_state = nil
  page_number = 1

  fetch_start_time = Core::Utils::Time.get_time(:float_millisecond)

  loop do
    Datadog.logger.debug { "Fetching known tests page ##{page_number}#{" with cursor" if page_state}" }

    response = fetch_page(api, test_session, page_state: page_state)

    unless response.ok?
      # mark the test session so that all events emitted in this session are tagged
      # with the hidden _dd.ci.library_configuration_error.known_tests tag
      test_session.set_tag(Ext::Test::LibraryConfigurationError::TAG_KNOWN_TESTS, "true")

      Datadog.logger.debug(
        "Failed to fetch known tests page ##{page_number}, bailing out of known tests fetch. " \
        "Early flake detection will not work."
      )
      return Set.new
    end

    # @type var response: Datadog::CI::TestTracing::KnownTests::Response
    http_response = response.http_response
    total_request_ms += http_response.duration_ms if http_response

    page_tests = response.tests
    result.merge(page_tests)
    Datadog.logger.debug { "Received #{page_tests.size} known tests from page ##{page_number} (total so far: #{result.size})" }

    unless response.has_next?
      Datadog.logger.debug { "Stopping known tests fetch: no more pages after page ##{page_number}" }
      break
    end

    page_state = response.cursor
    page_number += 1
  end

  fetch_duration_ms = Core::Utils::Time.get_time(:float_millisecond) - fetch_start_time
  # @type var fetch_duration_ms: Float

  Datadog.logger.debug { "Finished fetching known tests: #{result.size} tests total from #{page_number} page(s)" }

  # Emit pagination aggregate metrics
  Utils::Telemetry.distribution(Ext::Telemetry::METRIC_KNOWN_TESTS_PAGES_FETCHED, page_number.to_f)
  Utils::Telemetry.distribution(Ext::Telemetry::METRIC_KNOWN_TESTS_TOTAL_FETCH_MS, fetch_duration_ms)
  Utils::Telemetry.distribution(Ext::Telemetry::METRIC_KNOWN_TESTS_TOTAL_REQUEST_MS, total_request_ms)

  result
end