Class: Datadog::CI::TestTracing::Component

Inherits:
Object
  • Object
show all
Includes:
Core::Utils::Forking, Utils::Stateful
Defined in:
lib/datadog/ci/test_tracing/component.rb

Overview

Core functionality of the library: tracing tests' execution

Constant Summary collapse

FILE_STORAGE_KEY =
"test_tracing_component_state"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Stateful

#load_cached_known_tests, #load_cached_settings, #load_cached_skippable_tests, #load_cached_test_management, #load_component_state, #store_component_state, #test_optimization_cache

Constructor Details

#initialize(known_tests_client:, test_suite_level_visibility_enabled: false, codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse, logical_test_session_name: nil, runtime_tags_overrides: {}, context_service_uri: nil) ⇒ Component

Returns a new instance of Component.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/ci/test_tracing/component.rb', line 36

def initialize(
  known_tests_client:,
  test_suite_level_visibility_enabled: false,
  codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse,
  logical_test_session_name: nil,
  runtime_tags_overrides: {},
  context_service_uri: nil
)
  @test_suite_level_visibility_enabled = test_suite_level_visibility_enabled

  @context = Context.new(test_tracing_component: self, runtime_tags_overrides: runtime_tags_overrides)

  @codeowners = codeowners
  @logical_test_session_name = logical_test_session_name

  # "Known tests" feature fetches a list of all tests known to Datadog for this repository
  # and uses this list to determine if a test is new or not. New tests are marked with "test.is_new" tag.
  @known_tests_enabled = false
  @known_tests_client = known_tests_client
  @known_tests = Set.new

  # this is used for parallel test runners such as parallel_tests
  if context_service_uri
    @context_service_uri = context_service_uri
    @is_client_process = true
  end

  # This is used for parallel test runners such as parallel_tests.
  # If true, then test suites are created in the worker process, not the parent.
  #
  # The only test runner that requires creating test suites in the remote process is rails test runner because
  # it splits workload by test, not by test suite.
  #
  # Another test runner that splits workload by test is knapsack_pro, but we lack distributed test sessions/test suties
  # support for that one (as of 2025-03).
  @local_test_suites_mode = true
end

Instance Attribute Details

#context_service_uriObject (readonly)

Returns the value of attribute context_service_uri.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def context_service_uri
  @context_service_uri
end

#known_testsObject (readonly)

Returns the value of attribute known_tests.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def known_tests
  @known_tests
end

#known_tests_enabledObject (readonly)

Returns the value of attribute known_tests_enabled.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def known_tests_enabled
  @known_tests_enabled
end

#local_test_suites_modeObject (readonly)

Returns the value of attribute local_test_suites_mode.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def local_test_suites_mode
  @local_test_suites_mode
end

#logical_test_session_nameObject (readonly)

Returns the value of attribute logical_test_session_name.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def logical_test_session_name
  @logical_test_session_name
end

#test_suite_level_visibility_enabledObject (readonly)

Returns the value of attribute test_suite_level_visibility_enabled.



33
34
35
# File 'lib/datadog/ci/test_tracing/component.rb', line 33

def test_suite_level_visibility_enabled
  @test_suite_level_visibility_enabled
end

Instance Method Details

#active_spanObject



152
153
154
# File 'lib/datadog/ci/test_tracing/component.rb', line 152

def active_span
  @context.active_span
end

#active_testObject



156
157
158
# File 'lib/datadog/ci/test_tracing/component.rb', line 156

def active_test
  @context.active_test
end

#active_test_moduleObject



164
165
166
# File 'lib/datadog/ci/test_tracing/component.rb', line 164

def active_test_module
  maybe_remote_context.active_test_module
end

#active_test_sessionObject



160
161
162
# File 'lib/datadog/ci/test_tracing/component.rb', line 160

def active_test_session
  maybe_remote_context.active_test_session
end

#active_test_suite(test_suite_name) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/datadog/ci/test_tracing/component.rb', line 168

def active_test_suite(test_suite_name)
  # when test_suite_name is not provided (from public API most likely)
  # we return the single active test suite because most of the time there is only one test suite running
  return single_active_test_suite if test_suite_name.nil?

  test_suite_name = Utils::TestName.normalize(test_suite_name)

  # when fetching test_suite to use as test's context, try local context instance first
  local_test_suite = @context.active_test_suite(test_suite_name)
  return local_test_suite if local_test_suite

  maybe_remote_context.active_test_suite(test_suite_name)
end

#client_process?Boolean

Returns:

  • (Boolean)


228
229
230
231
232
233
234
# File 'lib/datadog/ci/test_tracing/component.rb', line 228

def client_process?
  # We cannot assume here that every forked process is a client process
  # there are examples of custom test runners that run tests in forks but don't have a test session
  # started in the parent process.
  # So we need to check if the process is forked and if the context service URI is not empty.
  (forked? && !@context_service_uri.nil? && !@context_service_uri.empty?) || @is_client_process
end

#configure(library_configuration, test_session) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/datadog/ci/test_tracing/component.rb', line 74

def configure(library_configuration, test_session)
  return unless test_suite_level_visibility_enabled
  return unless library_configuration.known_tests_enabled?

  @known_tests_enabled = true
  return if load_component_state

  fetch_known_tests(test_session)
  store_component_state if test_session.distributed
end

#deactivate_testObject



182
183
184
185
186
187
# File 'lib/datadog/ci/test_tracing/component.rb', line 182

def deactivate_test
  test = active_test
  on_test_finished(test) if test

  @context.deactivate_test
end

#deactivate_test_moduleObject



196
197
198
199
200
201
# File 'lib/datadog/ci/test_tracing/component.rb', line 196

def deactivate_test_module
  test_module = active_test_module
  on_test_module_finished(test_module) if test_module

  @context.deactivate_test_module
end

#deactivate_test_sessionObject



189
190
191
192
193
194
# File 'lib/datadog/ci/test_tracing/component.rb', line 189

def deactivate_test_session
  test_session = active_test_session
  on_test_session_finished(test_session) if test_session

  @context.deactivate_test_session
end

#deactivate_test_suite(test_suite_name) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/datadog/ci/test_tracing/component.rb', line 203

def deactivate_test_suite(test_suite_name)
  test_suite_name = Utils::TestName.normalize(test_suite_name)
  test_suite = active_test_suite(test_suite_name)
  on_test_suite_finished(test_suite) if test_suite

  # deactivation always happens on the same process where test suite is located
  @context.deactivate_test_suite(test_suite_name)
end

#itr_enabled?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/datadog/ci/test_tracing/component.rb', line 220

def itr_enabled?
  test_impact_analysis.enabled?
end

#restore_state_from_datadog_test_runnerObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/datadog/ci/test_tracing/component.rb', line 236

def restore_state_from_datadog_test_runner
  Datadog.logger.debug { "Restoring known tests from Test Optimization cache" }

  known_tests_data = load_cached_known_tests
  if known_tests_data.nil?
    Datadog.logger.debug { "Restoring known tests failed, will request again" }
    return false
  end

  Datadog.logger.debug { "Restored known tests from Test Optimization: #{known_tests_data}" }

  @known_tests = KnownTests::Response.from_json(known_tests_data).tests
  @known_tests_enabled = !@known_tests.empty?

  unless @known_tests_enabled
    Datadog.logger.debug("Empty set of known tests from the Test Optimization cache file")
  end

  Datadog.logger.debug { "Found [#{@known_tests.size}] known tests from context" }

  true
end

#shutdown!Object



224
225
226
# File 'lib/datadog/ci/test_tracing/component.rb', line 224

def shutdown!
  # noop, there is no thread owned by test visibility component
end

#start_test_module(test_module_name, service: nil, tags: {}) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/datadog/ci/test_tracing/component.rb', line 101

def start_test_module(test_module_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  test_module = maybe_remote_context.start_test_module(test_module_name, service: service, tags: tags)
  on_test_module_started(test_module)

  test_module
end

#start_test_session(service: nil, tags: {}, estimated_total_tests_count: 0, distributed: false, local_test_suites_mode: true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/datadog/ci/test_tracing/component.rb', line 85

def start_test_session(service: nil, tags: {}, estimated_total_tests_count: 0, distributed: false, local_test_suites_mode: true)
  return skip_tracing unless test_suite_level_visibility_enabled

  @local_test_suites_mode = local_test_suites_mode

  start_drb_service

  test_session = maybe_remote_context.start_test_session(service: service, tags: tags)
  test_session.estimated_total_tests_count = estimated_total_tests_count
  test_session.distributed = distributed

  on_test_session_started(test_session)

  test_session
end

#start_test_suite(test_suite_name, service: nil, tags: {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/datadog/ci/test_tracing/component.rb', line 110

def start_test_suite(test_suite_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  test_suite_name = Utils::TestName.normalize(test_suite_name)
  context = @local_test_suites_mode ? @context : maybe_remote_context

  test_suite = context.start_test_suite(test_suite_name, service: service, tags: tags)
  on_test_suite_started(test_suite)
  test_suite
end

#tests_skipped_by_tia_countObject



216
217
218
# File 'lib/datadog/ci/test_tracing/component.rb', line 216

def tests_skipped_by_tia_count
  maybe_remote_context.tests_skipped_by_tia_count
end

#total_tests_countObject



212
213
214
# File 'lib/datadog/ci/test_tracing/component.rb', line 212

def total_tests_count
  maybe_remote_context.total_tests_count
end

#trace(span_name, type: "span", tags: {}, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/datadog/ci/test_tracing/component.rb', line 142

def trace(span_name, type: "span", tags: {}, &block)
  if block
    @context.trace(span_name, type: type, tags: tags) do |span|
      block.call(span)
    end
  else
    @context.trace(span_name, type: type, tags: tags)
  end
end

#trace_test(test_name, test_suite_name, service: nil, tags: {}, &block) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/datadog/ci/test_tracing/component.rb', line 121

def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
  test_name = Utils::TestName.normalize(test_name)
  test_suite_name = Utils::TestName.normalize(test_suite_name)

  test_suite = active_test_suite(test_suite_name)
  tags[Ext::Test::TAG_SUITE] ||= test_suite_name

  if block
    @context.trace_test(test_name, test_suite, service: service, tags: tags) do |test|
      on_test_started(test)
      res = block.call(test)
      on_test_finished(test)
      res
    end
  else
    test = @context.trace_test(test_name, test_suite, service: service, tags: tags)
    on_test_started(test)
    test
  end
end