Class: Datadog::CI::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/span.rb,
sig/datadog/ci/span.rbs

Overview

Represents a single part of a test run. Could be a session, suite, test, or any custom span.

Direct Known Subclasses

ConcurrentSpan, Test

Constant Summary collapse

ENVIRONMENT_RUNTIME_TAGS =

Returns:

  • (Hash[String, String])
{
  Ext::Test::TAG_OS_ARCHITECTURE => ::RbConfig::CONFIG["host_cpu"],
  Ext::Test::TAG_OS_PLATFORM => ::RbConfig::CONFIG["host_os"],
  Ext::Test::TAG_OS_VERSION => Core::Environment::Platform.kernel_release,
  Ext::Test::TAG_RUNTIME_NAME => Core::Environment::Ext::LANG_ENGINE,
  Ext::Test::TAG_RUNTIME_VERSION => Core::Environment::Ext::ENGINE_VERSION
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer_span) ⇒ Span

Returns a new instance of Span.

Parameters:

  • tracer_span (Datadog::Tracing::SpanOperation)


30
31
32
# File 'lib/datadog/ci/span.rb', line 30

def initialize(tracer_span)
  @tracer_span = tracer_span
end

Instance Attribute Details

#tracer_spanObject (readonly)

Returns the value of attribute tracer_span.



28
29
30
# File 'lib/datadog/ci/span.rb', line 28

def tracer_span
  @tracer_span
end

Instance Method Details

#base_commit_shaString?

Returns the base commit SHA for the pull request, if available.

Returns:

  • (String, nil)

    the base commit SHA or nil if not set.



214
215
216
# File 'lib/datadog/ci/span.rb', line 214

def base_commit_sha
  tracer_span.get_tag(Ext::Git::TAG_PULL_REQUEST_BASE_BRANCH_SHA)
end

#clear_tag(key) ⇒ void

This method returns an undefined value.

Removes tag by key.

Parameters:

  • key (String)

    the key of the tag.



132
133
134
# File 'lib/datadog/ci/span.rb', line 132

def clear_tag(key)
  tracer_span.clear_tag(key)
end

#failed!(exception: nil) ⇒ void

This method returns an undefined value.

Sets the status of the span to "fail".

Parameters:

  • exception (Exception) (defaults to: nil)

    the exception that caused the test to fail.



98
99
100
101
102
# File 'lib/datadog/ci/span.rb', line 98

def failed!(exception: nil)
  tracer_span.status = 1
  tracer_span.set_tag(Ext::Test::TAG_STATUS, Ext::Test::Status::FAIL)
  tracer_span.set_error(exception) unless exception.nil?
end

#failed?bool

Checks whether span status is FAIL.

Returns:

  • (bool)

    true if span status is FAIL



73
74
75
# File 'lib/datadog/ci/span.rb', line 73

def failed?
  tracer_span.get_tag(Ext::Test::TAG_STATUS) == Ext::Test::Status::FAIL
end

#finishvoid

This method returns an undefined value.

Finishes the span.



153
154
155
# File 'lib/datadog/ci/span.rb', line 153

def finish
  tracer_span.finish
end

#get_metric(key) ⇒ Numeric

Gets metric value by key.

Parameters:

  • key (String)

    the key of the metric.

Returns:

  • (Numeric)

    value the value of the metric.



139
140
141
# File 'lib/datadog/ci/span.rb', line 139

def get_metric(key)
  tracer_span.get_metric(key)
end

#get_tag(key) ⇒ String

Gets tag value by key.

Parameters:

  • key (String)

    the key of the tag.

Returns:

  • (String)

    the value of the tag.



117
118
119
# File 'lib/datadog/ci/span.rb', line 117

def get_tag(key)
  tracer_span.get_tag(key)
end

#git_branchString

Returns the git branch name extracted from the environment.

Returns:

  • (String)

    the branch.



202
203
204
# File 'lib/datadog/ci/span.rb', line 202

def git_branch
  tracer_span.get_tag(Ext::Git::TAG_BRANCH)
end

#git_commit_messageString

Returns the git commit message extracted from the environment.

Returns:

  • (String)

    the commit message.



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

def git_commit_message
  get_tag(Ext::Git::TAG_COMMIT_MESSAGE)
end

#git_commit_shaString

Returns the latest commit SHA extracted from the environment.

Returns:

  • (String)

    the commit SHA of the last commit.



172
173
174
# File 'lib/datadog/ci/span.rb', line 172

def git_commit_sha
  tracer_span.get_tag(Ext::Git::TAG_COMMIT_SHA)
end

#git_repository_urlString

Returns the git repository URL extracted from the environment.

Returns:

  • (String)

    the repository URL.



166
167
168
# File 'lib/datadog/ci/span.rb', line 166

def git_repository_url
  tracer_span.get_tag(Ext::Git::TAG_REPOSITORY_URL)
end

#git_tagString?

Returns the git tag extracted from the environment.

Returns:

  • (String, nil)

    the tag or nil if not set.



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

def git_tag
  tracer_span.get_tag(Ext::Git::TAG_TAG)
end

#idInteger

Returns the ID of the span.

Returns:

  • (Integer)

    the ID of the span.



35
36
37
# File 'lib/datadog/ci/span.rb', line 35

def id
  tracer_span.id
end

#nameString

Returns the name of the span.

Returns:

  • (String)

    the name of the span.



45
46
47
# File 'lib/datadog/ci/span.rb', line 45

def name
  tracer_span.name
end

#original_git_commit_messageString

Returns the original last git commit message extracted from the environment. Original here means that it is the last commit that was pushed by the user, not the artifical merge commit created by CI provider

Returns:

  • (String)

    the commit message of the last commit.



196
197
198
# File 'lib/datadog/ci/span.rb', line 196

def original_git_commit_message
  get_tag(Ext::Git::TAG_COMMIT_HEAD_MESSAGE) || git_commit_message
end

#original_git_commit_shaString

Returns the original last git commit SHA extracted from the environment. Original here means that it is the last commit that was pushed by the user, not the artifical merge commit created by CI provider

Returns:

  • (String)

    the commit SHA of the last commit.



187
188
189
# File 'lib/datadog/ci/span.rb', line 187

def original_git_commit_sha
  get_tag(Ext::Git::TAG_COMMIT_HEAD_SHA) || git_commit_sha
end

#os_architectureString

Returns the OS architecture extracted from the environment.

Returns:

  • (String)

    OS arch.



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

def os_architecture
  tracer_span.get_tag(Ext::Test::TAG_OS_ARCHITECTURE)
end

#os_platformString

Returns the OS platform extracted from the environment.

Returns:

  • (String)

    OS platform.



226
227
228
# File 'lib/datadog/ci/span.rb', line 226

def os_platform
  tracer_span.get_tag(Ext::Test::TAG_OS_PLATFORM)
end

#os_versionString

Returns the OS version extracted from the environment.

Returns:

  • (String)

    OS version.



232
233
234
# File 'lib/datadog/ci/span.rb', line 232

def os_version
  tracer_span.get_tag(Ext::Test::TAG_OS_VERSION)
end

#passed!void

This method returns an undefined value.

Sets the status of the span to "pass".



91
92
93
# File 'lib/datadog/ci/span.rb', line 91

def passed!
  tracer_span.set_tag(Ext::Test::TAG_STATUS, Ext::Test::Status::PASS)
end

#passed?bool

Checks whether span status is PASS.

Returns:

  • (bool)

    true if span status is PASS



67
68
69
# File 'lib/datadog/ci/span.rb', line 67

def passed?
  tracer_span.get_tag(Ext::Test::TAG_STATUS) == Ext::Test::Status::PASS
end

#runtime_nameString

Returns the runtime name extracted from the environment.

Returns:

  • (String)

    runtime name.



238
239
240
# File 'lib/datadog/ci/span.rb', line 238

def runtime_name
  tracer_span.get_tag(Ext::Test::TAG_RUNTIME_NAME)
end

#runtime_versionString

Returns the runtime version extracted from the environment.

Returns:

  • (String)

    runtime version.



244
245
246
# File 'lib/datadog/ci/span.rb', line 244

def runtime_version
  tracer_span.get_tag(Ext::Test::TAG_RUNTIME_VERSION)
end

#serviceString

Returns the service name of the span.

Returns:

  • (String)

    the service name of the span.



50
51
52
# File 'lib/datadog/ci/span.rb', line 50

def service
  tracer_span.service
end

#set_default_tagsvoid

This method returns an undefined value.



260
261
262
# File 'lib/datadog/ci/span.rb', line 260

def set_default_tags
  tracer_span.set_tag(Ext::Test::TAG_SPAN_KIND, Ext::Test::SPAN_KIND_TEST)
end

#set_environment_runtime_tagsvoid

This method returns an undefined value.



255
256
257
258
# File 'lib/datadog/ci/span.rb', line 255

def set_environment_runtime_tags
  tracer_span.set_tags(ENVIRONMENT_RUNTIME_TAGS)
  tracer_span.set_tag(Ext::Test::TAG_COMMAND, Utils::TestRun.command)
end

#set_metric(key, value) ⇒ void

This method returns an undefined value.

Sets metric value by key.

Parameters:

  • key (String)

    the key of the metric.

  • value (Numeric)

    the value of the metric.



147
148
149
# File 'lib/datadog/ci/span.rb', line 147

def set_metric(key, value)
  tracer_span.set_metric(key, value)
end

#set_parametersvoid

This method returns an undefined value.

Parameters:

  • arguments (Hash[String, Object])
  • metadata (Hash[String, Object])


56
# File 'sig/datadog/ci/span.rbs', line 56

def set_parameters: (Hash[String, Object] arguments, ?Hash[String, Object] metadata) -> void

#set_tag(key, value) ⇒ void

This method returns an undefined value.

Sets tag value by key.

Parameters:

  • key (String)

    the key of the tag.

  • value (String)

    the value of the tag.



125
126
127
# File 'lib/datadog/ci/span.rb', line 125

def set_tag(key, value)
  tracer_span.set_tag(key, value)
end

#set_tags(tags) ⇒ void

This method returns an undefined value.

Sets multiple tags at once.

Parameters:

  • tags (Hash[String, String])

    the tags to set.



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

def set_tags(tags)
  tracer_span.set_tags(tags)
end

#skipped!(exception: nil, reason: nil) ⇒ void

This method returns an undefined value.

Sets the status of the span to "skip".

Parameters:

  • exception (Exception) (defaults to: nil)

    the exception that caused the test to fail.

  • reason (String) (defaults to: nil)

    the reason why the test was skipped.



108
109
110
111
112
# File 'lib/datadog/ci/span.rb', line 108

def skipped!(exception: nil, reason: nil)
  tracer_span.set_tag(Ext::Test::TAG_STATUS, Ext::Test::Status::SKIP)
  tracer_span.set_error(exception) unless exception.nil?
  tracer_span.set_tag(Ext::Test::TAG_SKIP_REASON, reason) unless reason.nil?
end

#skipped?bool

Checks whether span status is SKIP.

Returns:

  • (bool)

    true if span status is SKIP



79
80
81
# File 'lib/datadog/ci/span.rb', line 79

def skipped?
  tracer_span.get_tag(Ext::Test::TAG_STATUS) == Ext::Test::Status::SKIP
end

#source_fileString?

Source file path where the test or test suite defined (relative to git repository root).

Returns:

  • (String)

    the source file path of the test

  • (nil)

    if the source file path is not found



251
252
253
# File 'lib/datadog/ci/span.rb', line 251

def source_file
  get_tag(Ext::Test::TAG_SOURCE_FILE)
end

#statusString?

Returns the current status of the span.

Returns:

  • (String, nil)

    the status of the span ("pass", "fail", "skip"), or nil if not set



85
86
87
# File 'lib/datadog/ci/span.rb', line 85

def status
  tracer_span.get_tag(Ext::Test::TAG_STATUS)
end

#test_tracingObject

provides access to the test tracing component for CI models to deactivate themselves



271
272
273
# File 'lib/datadog/ci/span.rb', line 271

def test_tracing
  Datadog.send(:components).test_tracing
end

#to_sObject



264
265
266
# File 'lib/datadog/ci/span.rb', line 264

def to_s
  "#{self.class}(name:#{name},tracer_span:#{@tracer_span})"
end

#trace_idInteger

Returns the trace ID of the trace this span belongs to.

Returns:

  • (Integer)

    the trace ID of the trace this span belongs to



40
41
42
# File 'lib/datadog/ci/span.rb', line 40

def trace_id
  tracer_span.trace_id
end

#typeString

Returns the type of the span (for example "test" or type that was provided to [Datadog::CI.trace]).

Returns:

  • (String)

    the type of the span (for example "test" or type that was provided to [Datadog::CI.trace]).



55
56
57
# File 'lib/datadog/ci/span.rb', line 55

def type
  tracer_span.type
end

#undefined?bool

Checks whether span status is not set yet.

Returns:

  • (bool)

    true if span does not have status



61
62
63
# File 'lib/datadog/ci/span.rb', line 61

def undefined?
  tracer_span.get_tag(Ext::Test::TAG_STATUS).nil?
end