Class: RspecOtel::Matchers::EmitSpan

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

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



22
23
24
25
# File 'lib/rspec_otel/matchers/emit_span.rb', line 22

def as_child
  @filters << ->(span) { span.parent_span_id && span.parent_span_id != OpenTelemetry::Trace::INVALID_SPAN_ID }
  self
end

#as_rootObject



27
28
29
30
# File 'lib/rspec_otel/matchers/emit_span.rb', line 27

def as_root
  @filters << ->(span) { span.parent_span_id == OpenTelemetry::Trace::INVALID_SPAN_ID }
  self
end

#failure_messageObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/rspec_otel/matchers/emit_span.rb', line 77

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

  case closest
  when nil then "#{prefix}, but there were no spans emitted at all"
  when OpenTelemetry::SDK::Trace::SpanData then closest_not_found_message(prefix, closest)
  else raise "I don't know what to do with a #{closest.class} span"
  end
end

#failure_message_when_negatedObject



88
89
90
# File 'lib/rspec_otel/matchers/emit_span.rb', line 88

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

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/rspec_otel/matchers/emit_span.rb', line 17

def matches?(block)
  capture_before_spans(block)
  matching_span?
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rspec_otel/matchers/emit_span.rb', line 92

def supports_block_expectations?
  true
end

#with_attributes(attributes) ⇒ Object



32
33
34
35
# File 'lib/rspec_otel/matchers/emit_span.rb', line 32

def with_attributes(attributes)
  @filters << ->(span) { attributes_match?(span.attributes, attributes) }
  self
end

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



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

def with_event(name, attributes = {})
  event = OpenTelemetry::SDK::Trace::Event.new(name, attributes)
  @filters << ->(span) { span.events && event_match?(span.events, event) }
  self
end

#with_exception(exception = nil) ⇒ Object



69
70
71
# File 'lib/rspec_otel/matchers/emit_span.rb', line 69

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


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

def with_link(attributes = {})
  @filters << ->(span) { span.links && link_match?(span.links, attributes) }
  self
end

#with_status(code, description) ⇒ Object



64
65
66
67
# File 'lib/rspec_otel/matchers/emit_span.rb', line 64

def with_status(code, description)
  @filters << ->(span) { status_match?(span.status, code, description) }
  self
end

#without_attributes(attributes) ⇒ Object



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

def without_attributes(attributes)
  @filters << ->(span) { !attributes_match?(span.attributes, attributes) }
  self
end

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



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

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

#without_exception(exception = nil) ⇒ Object



73
74
75
# File 'lib/rspec_otel/matchers/emit_span.rb', line 73

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


47
48
49
50
# File 'lib/rspec_otel/matchers/emit_span.rb', line 47

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