Module: InstrumentAllTheThings::Testing::RSpecMatchers

Defined in:
lib/instrument_all_the_things/testing/rspec_matchers.rb

Instance Method Summary collapse

Instance Method Details

#counter_value(counter_name, with_tags: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 73

def counter_value(counter_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:count][counter_name]
  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end
  stats.inject(0) { |l, n| l + n[:args][0] }
end

#distribution_values(distribution_name, with_tags: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 13

def distribution_values(distribution_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:distribution][distribution_name]

  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end

  stats&.map { |i| i[:args] }&.map(&:first) || []
end

#emitted_spans(filtered_by: nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 87

def emitted_spans(filtered_by: nil)
  sleep 0.01
  traces = InstrumentAllTheThings::Testing::TraceTracker.tracker.traces.map(&:dup)
  if filtered_by
    filtered_by.transform_keys!(&:to_s)
    traces.select! { |t| filtered_by < t }
  end

  traces
end

#flush_tracesObject



83
84
85
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 83

def flush_traces
  Datadog::Tracing.send(:tracer)&.writer&.worker&.flush_data
end

#gauge_value(counter_name, with_tags: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 62

def gauge_value(counter_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:gauge][counter_name]

  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end
  stats.last&.fetch(:args)&.first
end

#histogram_value(counter_name) ⇒ Object



8
9
10
11
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 8

def histogram_value(counter_name)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:histogram][counter_name]
  stats.inject(0) { |l, n| l + n[:args][0] }
end

#histogram_values(histogram_name, with_tags: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 25

def histogram_values(histogram_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:histogram][histogram_name]

  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end

  stats&.map { |i| i[:args] }&.map(&:first) || []
end

#set_value(counter_name, with_tags: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 49

def set_value(counter_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:set][counter_name]

  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end

  data = stats&.map { |i| i[:args] }&.map(&:first)
  data ? data.uniq.length : 0
end

#timing_values(timing_name, with_tags: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/instrument_all_the_things/testing/rspec_matchers.rb', line 37

def timing_values(timing_name, with_tags: nil)
  stats = InstrumentAllTheThings.stat_reporter.emitted_values[:timing][timing_name]

  if with_tags && !stats.empty?
    stats = stats.select do |s|
      with_tags.all? { |t| s[:tags].include?(t) }
    end
  end

  stats&.map { |i| i[:args] }&.map(&:first) || []
end