Class: RspecOtel::Matchers::EmitMetric

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

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ EmitMetric

Returns a new instance of EmitMetric.



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

def initialize(name)
  @name = name
  @kind = nil
  @filters = []
  @before_count = 0
  @pre_snapshot = {}
  @closest_metric = nil
  @closest_filter_count = 0
  @emitted_outside_block = false
end

Instance Method Details

#failure_messageObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec_otel/matchers/emit_metric.rb', line 55

def failure_message
  closest = @closest_metric || new_snapshots.first
  if closest.nil?
    "expected metric #{printable_name} to have been emitted, but no metrics were emitted at all"
  elsif @emitted_outside_block
    "expected metric #{printable_name} to have been emitted within the block, but it was already emitted before"
  else
    "expected metric #{printable_name} to have been emitted, but it couldn't be found. " \
      "Found a close matching metric named `#{closest.name}`#{MetricDetails.new(closest)}"
  end
end

#failure_message_when_negatedObject



67
68
69
# File 'lib/rspec_otel/matchers/emit_metric.rb', line 67

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

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(block)
  execute_block(block) if block.respond_to?(:call)
  matching_metric?
end

#of_type(kind) ⇒ Object



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

def of_type(kind)
  @kind = kind
  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rspec_otel/matchers/emit_metric.rb', line 71

def supports_block_expectations?
  true
end

#with_attributes(attributes) ⇒ Object



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

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

#with_count(count) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rspec_otel/matchers/emit_metric.rb', line 46

def with_count(count)
  @filters << lambda { |dp|
    raise ArgumentError, 'with_count is only supported for histogram data points' if dp.respond_to?(:value)

    dp.count == count
  }
  self
end

#with_value(value) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rspec_otel/matchers/emit_metric.rb', line 37

def with_value(value)
  @filters << lambda { |dp|
    raise ArgumentError, 'with_value is not supported for histogram data points' unless dp.respond_to?(:value)

    dp.value == value
  }
  self
end

#without_attributes(attributes) ⇒ Object



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

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