Module: StatsD::Instrument::Assertions
- Includes:
- Helpers
- Defined in:
- lib/statsd/instrument/assertions.rb
Instance Method Summary
collapse
-
#assert_no_statsd_calls(metric_name = nil, &block) ⇒ Object
-
#assert_statsd_calls(expected_metrics, &block) ⇒ Object
-
#assert_statsd_distribution(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_gauge(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_histogram(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_increment(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_key_value(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_measure(metric_name, options = {}, &block) ⇒ Object
-
#assert_statsd_set(metric_name, options = {}, &block) ⇒ Object
Methods included from Helpers
#capture_statsd_calls, #with_capture_backend
Instance Method Details
#assert_no_statsd_calls(metric_name = nil, &block) ⇒ Object
6
7
8
9
10
|
# File 'lib/statsd/instrument/assertions.rb', line 6
def assert_no_statsd_calls(metric_name = nil, &block)
metrics = capture_statsd_calls(&block)
metrics.select! { |m| m.name == metric_name } if metric_name
assert(metrics.empty?, "No StatsD calls for metric #{metrics.map(&:name).join(', ')} expected.")
end
|
#assert_statsd_calls(expected_metrics, &block) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/statsd/instrument/assertions.rb', line 41
def assert_statsd_calls(expected_metrics, &block)
raise ArgumentError, "block must be given" unless block_given?
capture_backend = StatsD::Instrument::Backends::CaptureBackend.new
with_capture_backend(capture_backend) do
exception_occurred = nil
begin
block.call
rescue => exception
exception_occurred = exception
raise
ensure
metrics = capture_backend.collected_metrics
matched_expected_metrics = []
expected_metrics.each do |expected_metric|
expected_metric_times = expected_metric.times
expected_metric_times_remaining = expected_metric.times
filtered_metrics = metrics.select { |m| m.type == expected_metric.type && m.name == expected_metric.name }
if filtered_metrics.empty?
flunk_with_exception_info(exception_occurred, "No StatsD calls for metric #{expected_metric.name} " \
"of type #{expected_metric.type} were made.")
end
filtered_metrics.each do |metric|
next unless expected_metric.matches(metric)
assert(within_numeric_range?(metric.sample_rate),
"Unexpected sample rate type for metric #{metric.name}, must be numeric")
if expected_metric_times_remaining == 0
flunk_with_exception_info(exception_occurred, "Unexpected StatsD call; number of times this metric " \
"was expected exceeded: #{expected_metric.inspect}")
end
expected_metric_times_remaining -= 1
metrics.delete(metric)
if expected_metric_times_remaining == 0
matched_expected_metrics << expected_metric
end
end
next if expected_metric_times_remaining == 0
msg = +"Metric expected #{expected_metric_times} times but seen " \
"#{expected_metric_times - expected_metric_times_remaining} " \
"times: #{expected_metric.inspect}."
msg << "\nCaptured metrics with the same key: #{filtered_metrics}" if filtered_metrics.any?
flunk_with_exception_info(exception_occurred, msg)
end
expected_metrics -= matched_expected_metrics
unless expected_metrics.empty?
flunk_with_exception_info(exception_occurred, "Unexpected StatsD calls; the following metric expectations " \
"were not satisfied: #{expected_metrics.inspect}")
end
pass
end
end
end
|
#assert_statsd_distribution(metric_name, options = {}, &block) ⇒ Object
28
29
30
|
# File 'lib/statsd/instrument/assertions.rb', line 28
def assert_statsd_distribution(metric_name, options = {}, &block)
assert_statsd_call(:d, metric_name, options, &block)
end
|
#assert_statsd_gauge(metric_name, options = {}, &block) ⇒ Object
20
21
22
|
# File 'lib/statsd/instrument/assertions.rb', line 20
def assert_statsd_gauge(metric_name, options = {}, &block)
assert_statsd_call(:g, metric_name, options, &block)
end
|
#assert_statsd_histogram(metric_name, options = {}, &block) ⇒ Object
24
25
26
|
# File 'lib/statsd/instrument/assertions.rb', line 24
def assert_statsd_histogram(metric_name, options = {}, &block)
assert_statsd_call(:h, metric_name, options, &block)
end
|
#assert_statsd_increment(metric_name, options = {}, &block) ⇒ Object
12
13
14
|
# File 'lib/statsd/instrument/assertions.rb', line 12
def assert_statsd_increment(metric_name, options = {}, &block)
assert_statsd_call(:c, metric_name, options, &block)
end
|
#assert_statsd_key_value(metric_name, options = {}, &block) ⇒ Object
36
37
38
|
# File 'lib/statsd/instrument/assertions.rb', line 36
def assert_statsd_key_value(metric_name, options = {}, &block)
assert_statsd_call(:kv, metric_name, options, &block)
end
|
#assert_statsd_measure(metric_name, options = {}, &block) ⇒ Object
16
17
18
|
# File 'lib/statsd/instrument/assertions.rb', line 16
def assert_statsd_measure(metric_name, options = {}, &block)
assert_statsd_call(:ms, metric_name, options, &block)
end
|
#assert_statsd_set(metric_name, options = {}, &block) ⇒ Object
32
33
34
|
# File 'lib/statsd/instrument/assertions.rb', line 32
def assert_statsd_set(metric_name, options = {}, &block)
assert_statsd_call(:s, metric_name, options, &block)
end
|