Class: Buildkite::TestCollector::RSpecPlugin::Trace

Inherits:
Trace
  • Object
show all
Defined in:
lib/buildkite/test_collector/rspec_plugin/trace.rb

Constant Summary collapse

FILE_PATH_REGEX =
/^(.*?\.(rb|feature))/

Instance Attribute Summary collapse

Attributes inherited from Trace

#external_id

Instance Method Summary collapse

Methods inherited from Trace

#as_hash

Constructor Details

#initialize(example, history:, failure_reason: nil, failure_expanded: [], tags: nil, location_prefix: nil, external_id: nil) ⇒ Trace

Returns a new instance of Trace.



17
18
19
20
21
22
23
24
25
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 17

def initialize(example, history:, failure_reason: nil, failure_expanded: [], tags: nil, location_prefix: nil, external_id: nil)
  @example = example
  @history = history
  @failure_reason = failure_reason
  @failure_expanded = failure_expanded
  @tags = tags
  @location_prefix = location_prefix
  @external_id = external_id
end

Instance Attribute Details

#exampleObject

Returns the value of attribute example.



5
6
7
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 5

def example
  @example
end

#failure_expandedObject

Returns the value of attribute failure_expanded.



5
6
7
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 5

def failure_expanded
  @failure_expanded
end

#failure_reasonObject

Returns the value of attribute failure_reason.



5
6
7
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 5

def failure_reason
  @failure_reason
end

#historyObject

Returns the value of attribute history.



11
12
13
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 11

def history
  @history
end

#location_prefixObject (readonly)

Returns the value of attribute location_prefix.



13
14
15
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 13

def location_prefix
  @location_prefix
end

#otel_end_timestampObject

Left open by the around hook; Reporter finishes it once RSpec settles the example's result.



9
10
11
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 9

def otel_end_timestamp
  @otel_end_timestamp
end

#otel_spanObject

Left open by the around hook; Reporter finishes it once RSpec settles the example's result.



9
10
11
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 9

def otel_span
  @otel_span
end

#tagsObject (readonly)

Returns the value of attribute tags.



12
13
14
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 12

def tags
  @tags
end

Instance Method Details

#otel_attributesObject

What the span says about the test itself. OTel adds the submission marker and run metadata that describe every test span.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 40

def otel_attributes
  attributes = {
    "buildkite.test.scope" => strip_invalid_utf8_chars(scope),
    "buildkite.test.name" => strip_invalid_utf8_chars(name),
    "test.case.name" => strip_invalid_utf8_chars(example.full_description),
    "test.suite.name" => strip_invalid_utf8_chars(scope),
    "code.file.path" => strip_invalid_utf8_chars(prepend_location_prefix(file_name)),
    "code.line.number" => source_line_number,
  }
  attributes["buildkite.test.execution.external_id"] = external_id if external_id
  prefix = Buildkite::TestCollector::OTel::TAG_ATTRIBUTE_PREFIX
  tags&.each do |key, value|
    attributes["#{prefix}#{key}"] = strip_invalid_utf8_chars(value.to_s)
  end
  attributes
end

#otel_exception_eventsObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 61

def otel_exception_events
  (failure_expanded || []).filter_map do |failure|
    message = Array(failure[:expanded]).join("\n")
    stacktrace = Array(failure[:backtrace]).join("\n")
    attributes = {}
    attributes["exception.message"] = strip_invalid_utf8_chars(message) unless message.empty?
    attributes["exception.stacktrace"] = strip_invalid_utf8_chars(stacktrace) unless stacktrace.empty?
    attributes unless attributes.empty?
  end
end

#otel_failure_reasonObject



57
58
59
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 57

def otel_failure_reason
  strip_invalid_utf8_chars(failure_reason) if failure_reason
end

#resultObject Also known as: otel_result



27
28
29
30
31
32
33
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 27

def result
  case example.execution_result.status
  when :passed; "passed"
  when :failed; "failed"
  when :pending; "skipped"
  end
end