Skip to content

Commit 9b811a1

Browse files
committed
Increase quantile summary test coverage
1 parent bff620d commit 9b811a1

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

src/metrics/prometheus_quantile_summary.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,10 @@ create_summary(Name, Help, Data) ->
494494

495495
validate_error(Error) when is_number(Error), 0.0 < Error, Error < 100.0 ->
496496
ok;
497-
validate_error(_Error) ->
498-
erlang:error({invalid_error, "Error should be a percentage point in (0,100)"}).
497+
validate_error(Error) ->
498+
erlang:error({invalid_error, Error, "Error should be a percentage point in (0,100)"}).
499499

500500
validate_bound(Bound) when is_integer(Bound), 0 < Bound ->
501501
ok;
502-
validate_bound(_Bound) ->
503-
erlang:error({invalid_bound, "Bound should be a positive integer"}).
502+
validate_bound(Bound) ->
503+
erlang:error({invalid_bound, Bound, "Bound should be a positive integer"}).

test/eunit/metric/prometheus_quantile_summary_tests.erl

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ prometheus_format_test_() ->
1919
fun test_deregister/1,
2020
fun test_remove/1,
2121
fun test_default_value/1,
22+
fun test_values_when_empty/1,
23+
fun test_values_when_multiple_in_parallel/1,
24+
fun test_values_when_non_existing/1,
2225
fun test_values/1,
2326
fun test_collector1/1,
2427
fun test_collector2/1,
@@ -31,15 +34,7 @@ test_merge_logic_when_fetching_value(_) ->
3134
prometheus_quantile_summary:declare(
3235
[{name, Name}, {labels, []}, {help, ""}, {error, 0.01}, {bound, 2184}]
3336
),
34-
% Observe many values
35-
Fun = fun() ->
36-
[
37-
prometheus_quantile_summary:observe(Name, N)
38-
|| N <- lists:seq(1, 100)
39-
]
40-
end,
41-
Monitors = [spawn_monitor(Fun) || _ <- lists:seq(1, 1000)],
42-
collect_monitors(Monitors),
37+
parallel_observe_sequence_of_values(Name),
4338
Value = prometheus_quantile_summary:value(Name),
4439
[
4540
?_assertMatch(
@@ -85,6 +80,23 @@ test_errors(_) ->
8580
{invalid_metric_help, 12, "metric help is not a string"},
8681
prometheus_quantile_summary:new([{name, "qwe"}, {help, 12}])
8782
),
83+
?_assertError(
84+
{invalid_bound, 3.141592, "Bound should be a positive integer"},
85+
prometheus_quantile_summary:new([
86+
{name, "qwe"},
87+
{bound, 3.141592},
88+
{help, ""}
89+
])
90+
),
91+
?_assertError(
92+
{invalid_error, 101, "Error should be a percentage point in (0,100)"},
93+
prometheus_quantile_summary:new([
94+
{name, "qwe"},
95+
{error, 101},
96+
{labels, ["qua", "quantile"]},
97+
{help, ""}
98+
])
99+
),
88100
%% mf/arity errors
89101
?_assertError(
90102
{unknown_metric, default, unknown_metric},
@@ -400,6 +412,46 @@ test_default_value(_) ->
400412
?_assertMatch({0, 0, []}, SomethingValue)
401413
].
402414

415+
test_values_when_empty(_) ->
416+
prometheus_quantile_summary:new([
417+
{name, orders_summary},
418+
{labels, [department]},
419+
{help, "Track orders count/total sum"}
420+
]),
421+
[
422+
?_assertMatch(
423+
[],
424+
lists:sort(prometheus_quantile_summary:values(default, orders_summary))
425+
)
426+
].
427+
428+
test_values_when_multiple_in_parallel(_) ->
429+
prometheus_quantile_summary:new([
430+
{name, orders_summary},
431+
{labels, []},
432+
{help, "Track orders count/total sum"}
433+
]),
434+
parallel_observe_sequence_of_values(orders_summary),
435+
[
436+
?_assertMatch(
437+
[
438+
{[],100000,5050000,
439+
[{0.5,49.90296094906653},
440+
{0.9,89.13032933635913},
441+
{0.95,94.64203039019942}]}
442+
],
443+
lists:sort(prometheus_quantile_summary:values(default, orders_summary))
444+
)
445+
].
446+
447+
test_values_when_non_existing(_) ->
448+
[
449+
?_assertMatch(
450+
[],
451+
lists:sort(prometheus_quantile_summary:values(default, orders_summary))
452+
)
453+
].
454+
403455
test_values(_) ->
404456
prometheus_quantile_summary:new([
405457
{name, orders_summary},
@@ -408,7 +460,6 @@ test_values(_) ->
408460
]),
409461
prometheus_quantile_summary:observe(orders_summary, [electronics], 765.5),
410462
prometheus_quantile_summary:observe(orders_summary, [groceries], 112.3),
411-
412463
[
413464
?_assertMatch(
414465
[
@@ -533,6 +584,17 @@ test_collector3(_) ->
533584
)
534585
].
535586

587+
parallel_observe_sequence_of_values(Name) ->
588+
% Observe many values
589+
Fun = fun() ->
590+
[
591+
prometheus_quantile_summary:observe(Name, N)
592+
|| N <- lists:seq(1, 100)
593+
]
594+
end,
595+
Monitors = [spawn_monitor(Fun) || _ <- lists:seq(1, 1000)],
596+
collect_monitors(Monitors).
597+
536598
collect_monitors([]) ->
537599
ok;
538600
collect_monitors([{Pid, Ref} | Monitors]) ->

0 commit comments

Comments
 (0)