Class: Mergify::RSpec::Formatter

Inherits:
RSpec::Core::Formatters::BaseFormatter
  • Object
show all
Defined in:
lib/mergify/rspec/formatter.rb

Overview

RSpec formatter that creates OpenTelemetry spans for Mergify Test Insights and prints a terminal report. It is purely observational and does not modify test execution. rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Instance Method Details

#example_finished(notification) ⇒ Object

rubocop:disable Metrics/MethodLength



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mergify/rspec/formatter.rb', line 55

def example_finished(notification)
  return unless @example_spans

  example = notification.example
  span = @example_spans.delete(example.id)
  return unless span

  result = example.execution_result
  status = result.status.to_s
  span.set_attribute('test.case.result.status', status)
  set_flaky_attributes(span, example)

  if result.status == :failed
    set_error_attributes(span, result.exception)
    @has_error = true
  else
    span.status = OpenTelemetry::Trace::Status.ok
  end

  span.finish
end

#example_pending(notification) ⇒ Object

rubocop:enable Metrics/MethodLength



78
79
80
81
82
83
84
85
86
87
# File 'lib/mergify/rspec/formatter.rb', line 78

def example_pending(notification)
  return unless @example_spans

  example = notification.example
  span = @example_spans.delete(example.id)
  return unless span

  span.set_attribute('test.case.result.status', 'skipped')
  span.finish
end

#example_started(notification) ⇒ Object

rubocop:enable Metrics/MethodLength



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mergify/rspec/formatter.rb', line 39

def example_started(notification)
  return unless @ci_insights&.tracer && @session_span

  example = notification.example
  parent_context = OpenTelemetry::Trace.context_with_span(@session_span)
  quarantined = @ci_insights.mark_test_as_quarantined_if_needed(example.id)

  span = @ci_insights.tracer.start_span(
    example.id,
    with_parent: parent_context,
    attributes: build_example_attributes(example, quarantined)
  )
  @example_spans[example.id] = span
end

#start(notification) ⇒ Object

rubocop:disable Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mergify/rspec/formatter.rb', line 21

def start(notification)
  super

  @ci_insights = Mergify::RSpec.ci_insights
  return unless @ci_insights&.tracer

  extract_distributed_trace_context

  @session_span = @ci_insights.tracer.start_span(
    'rspec session start',
    with_parent: @parent_context,
    attributes: { 'test.scope' => 'session' }
  )
  @has_error = false
  @example_spans = {}
end

#stop(_notification) ⇒ Object



89
90
91
92
93
# File 'lib/mergify/rspec/formatter.rb', line 89

def stop(_notification)
  finish_session_span
  print_report
  flush_and_shutdown
end