Class: Datadog::CI::TestVisibility::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/test_visibility/recorder.rb

Overview

Common behavior for CI tests Note: this class has too many responsibilities and should be split into multiple classes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(itr:, remote_settings_api:, git_tree_upload_worker: DummyWorker.new, test_suite_level_visibility_enabled: false, codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse) ⇒ Recorder

Returns a new instance of Recorder.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 34

def initialize(
  itr:,
  remote_settings_api:,
  git_tree_upload_worker: DummyWorker.new,
  test_suite_level_visibility_enabled: false,
  codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse
)
  @test_suite_level_visibility_enabled = test_suite_level_visibility_enabled

  @environment_tags = Ext::Environment.tags(ENV).freeze
  @local_context = Context::Local.new
  @global_context = Context::Global.new

  @codeowners = codeowners

  @itr = itr
  @remote_settings_api = remote_settings_api
  @git_tree_upload_worker = git_tree_upload_worker
end

Instance Attribute Details

#environment_tagsObject (readonly)

Returns the value of attribute environment_tags.



32
33
34
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 32

def environment_tags
  @environment_tags
end

#test_suite_level_visibility_enabledObject (readonly)

Returns the value of attribute test_suite_level_visibility_enabled.



32
33
34
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 32

def test_suite_level_visibility_enabled
  @test_suite_level_visibility_enabled
end

Instance Method Details

#active_spanObject



169
170
171
172
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 169

def active_span
  tracer_span = Datadog::Tracing.active_span
  Span.new(tracer_span) if tracer_span
end

#active_testObject



174
175
176
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 174

def active_test
  @local_context.active_test
end

#active_test_moduleObject



182
183
184
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 182

def active_test_module
  @global_context.active_test_module
end

#active_test_sessionObject



178
179
180
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 178

def active_test_session
  @global_context.active_test_session
end

#active_test_suite(test_suite_name) ⇒ Object



186
187
188
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 186

def active_test_suite(test_suite_name)
  @global_context.active_test_suite(test_suite_name)
end

#deactivate_testObject



190
191
192
193
194
195
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 190

def deactivate_test
  test = active_test
  on_test_finished(test) if test

  @local_context.deactivate_test
end

#deactivate_test_moduleObject



204
205
206
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 204

def deactivate_test_module
  @global_context.deactivate_test_module!
end

#deactivate_test_sessionObject



197
198
199
200
201
202
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 197

def deactivate_test_session
  test_session = active_test_session
  on_test_session_finished(test_session) if test_session

  @global_context.deactivate_test_session!
end

#deactivate_test_suite(test_suite_name) ⇒ Object



208
209
210
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 208

def deactivate_test_suite(test_suite_name)
  @global_context.deactivate_test_suite!(test_suite_name)
end

#itr_enabled?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 212

def itr_enabled?
  @itr.enabled?
end

#shutdown!Object



54
55
56
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 54

def shutdown!
  @git_tree_upload_worker.stop
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 79

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

  @global_context.fetch_or_activate_test_module do
    set_inherited_globals(tags)
    set_session_context(tags)

    tracer_span = start_datadog_tracer_span(
      test_module_name, build_span_options(service, Ext::AppTypes::TYPE_TEST_MODULE)
    )
    set_module_context(tags, tracer_span)

    build_test_module(tracer_span, tags)
  end
end

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 58

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

  # finds and instruments additional test libraries that we support (ex: selenium-webdriver)
  Contrib.auto_instrument_on_session_start!

  @global_context.fetch_or_activate_test_session do
    tracer_span = start_datadog_tracer_span(
      "test.session", build_span_options(service, Ext::AppTypes::TYPE_TEST_SESSION)
    )
    set_session_context(tags, tracer_span)

    test_session = build_test_session(tracer_span, tags)

    @git_tree_upload_worker.perform(test_session.git_repository_url)
    configure_library(test_session)

    test_session
  end
end

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 95

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

  @global_context.fetch_or_activate_test_suite(test_suite_name) do
    set_inherited_globals(tags)
    set_session_context(tags)
    set_module_context(tags)

    tracer_span = start_datadog_tracer_span(
      test_suite_name, build_span_options(service, Ext::AppTypes::TYPE_TEST_SUITE)
    )
    set_suite_context(tags, span: tracer_span)

    build_test_suite(tracer_span, tags)
  end
end

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 151

def trace(span_name, type: "span", tags: {}, &block)
  span_options = build_span_options(
    nil, # service name is completely optional for custom spans
    type,
    {resource: span_name}
  )

  if block
    start_datadog_tracer_span(span_name, span_options) do |tracer_span|
      block.call(build_span(tracer_span, tags))
    end
  else
    tracer_span = start_datadog_tracer_span(span_name, span_options)

    build_span(tracer_span, tags)
  end
end

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



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
# File 'lib/datadog/ci/test_visibility/recorder.rb', line 112

def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
  set_inherited_globals(tags)
  set_session_context(tags)
  set_module_context(tags)
  set_suite_context(tags, name: test_suite_name)

  tags[Ext::Test::TAG_NAME] = test_name
  tags[Ext::Test::TAG_TYPE] ||= Ext::Test::Type::TEST

  span_options = build_span_options(
    service,
    Ext::AppTypes::TYPE_TEST,
    # :resource is needed for the agent APM protocol to work correctly (for older agent versions)
    # :continue_from is required to start a new trace for each test
    {resource: test_name, continue_from: Datadog::Tracing::TraceDigest.new}
  )

  if block
    start_datadog_tracer_span(test_name, span_options) do |tracer_span|
      test = build_test(tracer_span, tags)

      @local_context.activate_test(test) do
        on_test_started(test)
        res = block.call(test)
        on_test_finished(test)
        res
      end
    end
  else
    tracer_span = start_datadog_tracer_span(test_name, span_options)
    test = build_test(tracer_span, tags)

    @local_context.activate_test(test)
    on_test_started(test)

    test
  end
end