Class: RSpecHoneycombFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_honeycomb_formatter.rb,
lib/rspec_honeycomb_formatter/version.rb

Overview

A custom formatter for RSpec that posts messages to honeycomb.io for analysis

Constant Summary collapse

VERSION =
'0.2.1'

Instance Method Summary collapse

Constructor Details

#initialize(_output) ⇒ RSpecHoneycombFormatter



38
39
40
# File 'lib/rspec_honeycomb_formatter.rb', line 38

def initialize(_output)
  @group_stack = []
end

Instance Method Details

#example_failed(notification) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/rspec_honeycomb_formatter.rb', line 87

def example_failed(notification)
  @example_span.add_field('rspec.result', 'failed')
  @example_span.add_field('name', notification.example.description)
  @example_span.add_field('rspec.description', notification.example.description)
  @example_span.add_field('rspec.message', strip_ansi(notification.fully_formatted(0, RSpec::Core::Notifications::NullColorizer)))
  @example_span.add_field('rspec.backtrace', notification.formatted_backtrace.join("\n"))
  @example_span.send
end

#example_group_finished(_notification) ⇒ Object



67
68
69
70
# File 'lib/rspec_honeycomb_formatter.rb', line 67

def example_group_finished(_notification)
  group_span = @group_stack.pop
  group_span.send
end

#example_group_started(notification) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rspec_honeycomb_formatter.rb', line 59

def example_group_started(notification)
  @group_stack.push(group_span = Honeycomb.start_span(name: notification.group.description))
  ENV['HTTP_X_HONEYCOMB_TRACE'] = group_span.to_trace_header unless ENV['HTTP_X_HONEYCOMB_TRACE']
  group_span.add_field('rspec.type', 'group')
  group_span.add_field('rspec.file_path', notification.group.file_path)
  group_span.add_field('rspec.location', notification.group.location)
end

#example_passed(notification) ⇒ Object



80
81
82
83
84
85
# File 'lib/rspec_honeycomb_formatter.rb', line 80

def example_passed(notification)
  @example_span.add_field('rspec.result', 'passed')
  @example_span.add_field('name', notification.example.description)
  @example_span.add_field('rspec.description', notification.example.description)
  @example_span.send
end

#example_pending(notification) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec_honeycomb_formatter.rb', line 96

def example_pending(notification)
  @example_span.add_field('rspec.result', 'pending')
  @example_span.add_field('name', notification.example.description)
  @example_span.add_field('rspec.description', notification.example.description)

  case notification
  when RSpec::Core::Notifications::FailedExampleNotification
    @example_span.add_field('rspec.message', strip_ansi(notification.fully_formatted(0, RSpec::Core::Notifications::NullColorizer)))
    @example_span.add_field('rspec.backtrace', notification.formatted_backtrace.join("\n"))
  when RSpec::Core::Notifications::SkippedExampleNotification
    @example_span.add_field('rspec.message', strip_ansi(notification.fully_formatted(0, RSpec::Core::Notifications::NullColorizer)))
  else
    @example_span.add_field('rspec.result', notification.class)
  end
  @example_span.send
end

#example_started(notification) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rspec_honeycomb_formatter.rb', line 72

def example_started(notification)
  @example_span = Honeycomb.start_span(name: 'unknown')
  ENV['HTTP_X_HONEYCOMB_TRACE'] = @example_span.to_trace_header unless ENV['HTTP_X_HONEYCOMB_TRACE']
  @example_span.add_field('rspec.type', 'example')
  @example_span.add_field('rspec.file_path', notification.example.file_path)
  @example_span.add_field('rspec.location', notification.example.location)
end

#message(notification) ⇒ Object



117
118
119
# File 'lib/rspec_honeycomb_formatter.rb', line 117

def message(notification)
  # puts "message: #{notification}"
end

#seed(notification) ⇒ Object



113
114
115
# File 'lib/rspec_honeycomb_formatter.rb', line 113

def seed(notification)
  @start_span.add_field('rspec.seed', notification.seed) if notification.seed_used?
end

#start(notification) ⇒ Object



42
43
44
45
46
47
# File 'lib/rspec_honeycomb_formatter.rb', line 42

def start(notification)
  @start_span = Honeycomb.start_span(name: 'rspec', serialized_trace: ENV['HTTP_X_HONEYCOMB_TRACE'])
  ENV['HTTP_X_HONEYCOMB_TRACE'] = @start_span.to_trace_header unless ENV['HTTP_X_HONEYCOMB_TRACE']
  @start_span.add_field('rspec.example_count', notification.count)
  @start_span.add_field('rspec.load_time_ms', notification.load_time * 1000)
end

#start_dump(notification) ⇒ Object



55
56
57
# File 'lib/rspec_honeycomb_formatter.rb', line 55

def start_dump(notification)
  # puts "start_dump: #{notification}"
end

#stop(notification) ⇒ Object



49
50
51
52
53
# File 'lib/rspec_honeycomb_formatter.rb', line 49

def stop(notification)
  @start_span.add_field('rspec.failed_count', notification.failed_examples.size)
  @start_span.add_field('rspec.pending_count', notification.pending_examples.size)
  @start_span.send
end