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, #trace_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_id: nil) ⇒ Trace

Returns a new instance of Trace.



17
18
19
20
21
22
23
24
25
26
# 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, trace_id: nil)
  @example = example
  @history = history
  @failure_reason = failure_reason
  @failure_expanded = failure_expanded
  @tags = tags
  @location_prefix = location_prefix
  @external_id = external_id
  @trace_id = trace_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 (readonly)

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. The two OTel modes use the same attributes; buildkite.execution.via is the sole synthesis opt-in.



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

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.execution.via"] = "otlp" if Buildkite::TestCollector.otel_only?
  attributes["buildkite.test.execution.external_id"] = external_id if external_id
  tags&.each do |key, value|
    attributes["buildkite.tag.#{key}"] = strip_invalid_utf8_chars(value.to_s)
  end
  attributes
end

#otel_exception_eventsObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 64

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



60
61
62
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 60

def otel_failure_reason
  strip_invalid_utf8_chars(failure_reason) if failure_reason
end

#otel_resultObject

Read at reporter time, when the result is final.



37
38
39
# File 'lib/buildkite/test_collector/rspec_plugin/trace.rb', line 37

def otel_result
  result
end

#resultObject



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

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