Class: RspecOtel::Matchers::EmitSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_otel/matchers/emit_span.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ EmitSpan

Returns a new instance of EmitSpan.



8
9
10
11
12
13
14
15
# File 'lib/rspec_otel/matchers/emit_span.rb', line 8

def initialize(name = nil)
  @name = name
  @filters = []
  @before_spans = []
  @closest_span = nil

  @filters << name_filter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rspec_otel/matchers/emit_span.rb', line 6

def name
  @name
end

Instance Method Details

#as_childObject



34
35
36
37
38
39
40
# File 'lib/rspec_otel/matchers/emit_span.rb', line 34

def as_child
  @filters << lambda do |span|
    span.parent_span_id && span.parent_span_id != OpenTelemetry::Trace::INVALID_SPAN_ID
  end

  self
end

#as_rootObject



42
43
44
45
46
47
48
# File 'lib/rspec_otel/matchers/emit_span.rb', line 42

def as_root
  @filters << lambda do |span|
    span.parent_span_id == OpenTelemetry::Trace::INVALID_SPAN_ID
  end

  self
end

#failure_messageObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rspec_otel/matchers/emit_span.rb', line 117

def failure_message
  closest = closest_span
  expect_content = "expected span #{failure_match_description} #{printable_name} to have been emitted"

  case closest
  when nil
    "#{expect_content}, but there were no spans emitted at all"
  when OpenTelemetry::SDK::Trace::SpanData
    "#{expect_content}, but it couldn't be found. Found a close matching span named `#{closest.name}`"
  else
    raise "I don't know what to do with a #{closest.class} span"
  end
end

#failure_message_when_negatedObject



131
132
133
# File 'lib/rspec_otel/matchers/emit_span.rb', line 131

def failure_message_when_negated
  "expected span #{failure_match_description} #{printable_name} to not have been emitted"
end

#matches?(block) ⇒ Boolean

rubocop:disable Metrics/MethodLength

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rspec_otel/matchers/emit_span.rb', line 17

def matches?(block) # rubocop:disable Metrics/MethodLength
  if block.respond_to?(:call)
    @before_spans = RspecOtel.exporter.finished_spans
    block.call
  end

  closest_count = 0
  (RspecOtel.exporter.finished_spans - @before_spans).each do |span|
    count = @filters.count { |f| f.call(span) }
    @closest_span = span if count > closest_count
    closest_count = count
    return true if count == @filters.count
  end

  false
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/rspec_otel/matchers/emit_span.rb', line 135

def supports_block_expectations?
  true
end

#with_attributes(attributes) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rspec_otel/matchers/emit_span.rb', line 50

def with_attributes(attributes)
  @filters << lambda do |span|
    attributes_match?(span.attributes, attributes)
  end

  self
end

#with_event(name, attributes = {}) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/rspec_otel/matchers/emit_span.rb', line 84

def with_event(name, attributes = {})
  @filters << lambda do |span|
    span.events &&
      event_match?(span.events, OpenTelemetry::SDK::Trace::Event.new(name, attributes))
  end

  self
end

#with_exception(exception = nil) ⇒ Object



109
110
111
# File 'lib/rspec_otel/matchers/emit_span.rb', line 109

def with_exception(exception = nil)
  with_event('exception', exception_attributes(exception))
end


66
67
68
69
70
71
72
73
# File 'lib/rspec_otel/matchers/emit_span.rb', line 66

def with_link(attributes = {})
  @filters << lambda do |span|
    span.links &&
      link_match?(span.links, attributes)
  end

  self
end

#with_status(code, description) ⇒ Object



102
103
104
105
106
107
# File 'lib/rspec_otel/matchers/emit_span.rb', line 102

def with_status(code, description)
  @filters << lambda do |span|
    status_match?(span.status, code, description)
  end
  self
end

#without_attributes(attributes) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rspec_otel/matchers/emit_span.rb', line 58

def without_attributes(attributes)
  @filters << lambda do |span|
    !attributes_match?(span.attributes, attributes)
  end

  self
end

#without_event(name, attributes = {}) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rspec_otel/matchers/emit_span.rb', line 93

def without_event(name, attributes = {})
  @filters << lambda do |span|
    span.events.nil? ||
      !event_match?(span.events, OpenTelemetry::SDK::Trace::Event.new(name, attributes))
  end

  self
end

#without_exception(exception = nil) ⇒ Object



113
114
115
# File 'lib/rspec_otel/matchers/emit_span.rb', line 113

def without_exception(exception = nil)
  without_event('exception', exception_attributes(exception))
end


75
76
77
78
79
80
81
82
# File 'lib/rspec_otel/matchers/emit_span.rb', line 75

def without_link(attributes = {})
  @filters << lambda do |span|
    span.links.nil? ||
      !link_match?(span.links, attributes)
  end

  self
end