Class: RSpec::Trace::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/trace/consumer.rb

Instance Method Summary collapse

Constructor Details

#initialize(input, traceparent_string = nil) ⇒ Consumer

Returns a new instance of Consumer.



10
11
12
13
14
15
16
17
18
# File 'lib/rspec/trace/consumer.rb', line 10

def initialize(input, traceparent_string = nil)
  @input = input
  @tracer_provider = OpenTelemetry.tracer_provider
  @tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers::ALWAYS_ON
  @tracer = @tracer_provider.tracer("rspec-trace-formatter", RSpec::Trace::VERSION)
  @spans = []
  @contexts = [load_context_from_traceparent(traceparent_string)]
  @tokens = []
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rspec/trace/consumer.rb', line 20

def run
  @input.each_line do |line|
    next if line.strip.empty?

    begin
      event = parse_event(line)
    rescue
      warn "invalid line: #{line}"
      next
    end

    case event[:event].to_sym
    when :initiated
      root_span_name = ENV.fetch("RSPEC_TRACE_FORMATTER_ROOT_SPAN_NAME", "rspec")
      create_span(name: root_span_name, timestamp: event[:timestamp])
      create_span(name: "examples loading", timestamp: event[:timestamp]) do |span|
        span.add_attributes("rspec.type" => "loading")
      end
    when :start
      complete_span(timestamp: event[:timestamp])
      create_span(name: "examples running", timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: {count: event[:count], type: "suite"},
          attribute_prefix: "rspec"
        )
      end
    when :example_group_started
      create_span(name: event.dig(:group, :description), timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: event[:group].merge(type: "example_group"),
          attribute_prefix: "rspec",
          exclude_attributes: [:description]
        )
      end
    when :example_group_finished
      complete_span(timestamp: event[:timestamp])
    when :example_started
      create_span(name: event.dig(:example, :description), timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: event[:example].merge(type: "example"),
          attribute_prefix: "rspec",
          exclude_attributes: [:description]
        )
      end
    when :example_passed
      complete_span(timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: event.dig(:example, :result),
          attribute_prefix: "rspec.result"
        )
      end
    when :example_pending
      complete_span(timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: event.dig(:example, :result),
          attribute_prefix: "rspec.result"
        )
      end
    when :example_failed
      complete_span(timestamp: event[:timestamp]) do |span|
        add_attributes_to_span(
          span: span,
          attributes: event.dig(:example, :result),
          attribute_prefix: "rspec.result"
        )
        event_attributes = {
          "exception.type" => event.dig(:exception, :type),
          "exception.message" => event.dig(:exception, :message),
          "exception.stacktrace" => event.dig(:exception, :backtrace)
        }
        span.add_event("exception", attributes: event_attributes, timestamp: event[:timestamp])
        span.status = OpenTelemetry::Trace::Status.error
      end
    when :stop
      complete_span(timestamp: event[:timestamp]) until @spans.empty?
      @tracer_provider.force_flush
      exit
    end
  end
end