diff --git a/src/metrics/prometheus_boolean.erl b/src/metrics/prometheus_boolean.erl index c88e6299..c8c6dd12 100644 --- a/src/metrics/prometheus_boolean.erl +++ b/src/metrics/prometheus_boolean.erl @@ -42,6 +42,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -208,6 +211,27 @@ remove(Name, LabelValues) -> remove(Registry, Name, LabelValues) -> prometheus_metric:remove_labels(?TABLE, Registry, Name, LabelValues). +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the boolean matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if boolean with name `Name' +%% can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% Returns the number of removed entries. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -295,7 +319,10 @@ collect_metrics(Name, {CLabels, Labels, Registry}) -> %%==================================================================== deregister_select(Registry, Name) -> - [{{{Registry, Name, '_'}, '_'}, [], [true]}]. + delete_match_spec(Registry, Name, '_', []). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond) -> + [{{{Registry, Name, LMatchHead}, '_'}, LMatchCond, [true]}]. set_(Registry, Name, LabelValues, Value) -> case ets:update_element(?TABLE, {Registry, Name, LabelValues}, diff --git a/src/metrics/prometheus_counter.erl b/src/metrics/prometheus_counter.erl index 73072e54..f367bc01 100644 --- a/src/metrics/prometheus_counter.erl +++ b/src/metrics/prometheus_counter.erl @@ -65,6 +65,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -229,6 +232,27 @@ remove(Registry, Name, LabelValues) -> _ -> true end. +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the counter matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if boolean with name `Name' +%% can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% Returns the number of removed entries. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -325,7 +349,10 @@ collect_metrics(Name, {CLabels, Labels, Registry}) -> %%==================================================================== deregister_select(Registry, Name) -> - [{{{Registry, Name, '_', '_'}, '_', '_'}, [], [true]}]. + delete_match_spec(Registry, Name, '_', []). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond) -> + [{{{Registry, Name, LMatchHead, '_'}, '_', '_'}, LMatchCond, [true]}]. insert_metric(Registry, Name, LabelValues, Value, ConflictCB) -> prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LabelValues), diff --git a/src/metrics/prometheus_gauge.erl b/src/metrics/prometheus_gauge.erl index 1ada1aad..3185a082 100644 --- a/src/metrics/prometheus_gauge.erl +++ b/src/metrics/prometheus_gauge.erl @@ -68,6 +68,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -370,6 +373,26 @@ remove(Name, LabelValues) -> remove(Registry, Name, LabelValues) -> prometheus_metric:remove_labels(?TABLE, Registry, Name, LabelValues). +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the gauge matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if boolean with name `Name' +%% can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -469,7 +492,10 @@ maybe_insert_metric_for_inc(Registry, Name, LabelValues, Value) -> end. deregister_select(Registry, Name) -> - [{{{Registry, Name, '_'}, '_', '_'}, [], [true]}]. + delete_match_spec(Registry, Name, '_', []). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond) -> + [{{{Registry, Name, LMatchHead}, '_', '_'}, LMatchCond, [true]}]. insert_metric(Registry, Name, LabelValues, Value, ConflictCB) -> prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LabelValues), diff --git a/src/metrics/prometheus_histogram.erl b/src/metrics/prometheus_histogram.erl index 692f693a..e36604c8 100644 --- a/src/metrics/prometheus_histogram.erl +++ b/src/metrics/prometheus_histogram.erl @@ -49,6 +49,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -299,6 +302,28 @@ remove(Registry, Name, LabelValues) -> _ -> true end. +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the histogram matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if histogram with name +%% `Name' can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% Returns the number of removed entries. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + MF = prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + Buckets = prometheus_metric:mf_data(MF), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond, Buckets)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -529,9 +554,12 @@ load_all_values(Registry, Name, Bounds) -> ets:match(?TABLE, list_to_tuple(QuerySpec)). deregister_select(Registry, Name, Buckets) -> + delete_match_spec(Registry, Name, '_', [], Buckets). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond, Buckets) -> BoundCounters = lists:duplicate(length(Buckets), '_'), - MetricSpec = [{Registry, Name, '_', '_'}, '_', '_', '_'] ++ BoundCounters, - [{list_to_tuple(MetricSpec), [], [true]}]. + MetricSpec = [{Registry, Name, LMatchHead, '_'}, '_', '_', '_'] ++ BoundCounters, + [{list_to_tuple(MetricSpec), LMatchCond, [true]}]. delete_metrics(Registry, Buckets) -> BoundCounters = lists:duplicate(length(Buckets), '_'), diff --git a/src/metrics/prometheus_quantile_summary.erl b/src/metrics/prometheus_quantile_summary.erl index ca438af0..e6ccf56f 100644 --- a/src/metrics/prometheus_quantile_summary.erl +++ b/src/metrics/prometheus_quantile_summary.erl @@ -49,6 +49,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -233,6 +236,27 @@ remove(Registry, Name, LabelValues) -> _ -> true end. +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the summary series matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if boolean with name `Name' +%% can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% Returns the number of removed entries. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -365,7 +389,10 @@ collect_metrics(Name, {CLabels, Labels, Registry, DU, Configuration}) -> %%==================================================================== deregister_select(Registry, Name) -> - [{{{Registry, Name, '_', '_'}, '_', '_', '_', '_'}, [], [true]}]. + delete_match_spec(Registry, Name, '_', []). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond) -> + [{{{Registry, Name, LMatchHead, '_'}, '_', '_', '_', '_'}, LMatchCond, [true]}]. validate_summary_spec(Spec) -> Labels = prometheus_metric_spec:labels(Spec), diff --git a/src/metrics/prometheus_summary.erl b/src/metrics/prometheus_summary.erl index d735a01a..6d62e005 100644 --- a/src/metrics/prometheus_summary.erl +++ b/src/metrics/prometheus_summary.erl @@ -41,6 +41,9 @@ remove/1, remove/2, remove/3, + match_remove/1, + match_remove/3, + match_remove/4, reset/1, reset/2, reset/3, @@ -228,6 +231,26 @@ remove(Registry, Name, LabelValues) -> _ -> true end. +%% @equiv match_remove(default, Name, [], []) +match_remove(Name) -> + match_remove(default, Name, [], []). + +%% @equiv match_remove(default, Name, LMatchHead, LMatchCond) +match_remove(Name, LMatchHead, LMatchCond) -> + match_remove(default, Name, LMatchHead, LMatchCond). + +%% @doc Remove the value of the summary matched by `Registry', `Name', +%% `LMatchHead' and `LMatchCond'. +%% +%% Raises `{unknown_metric, Registry, Name}' error if summary with name `Name' +%% can't be found in `Registry'.
+%% Raises `{invalid_metric_arity, Present, Expected}' error if labels count +%% mismatch. +%% @end +match_remove(Registry, Name, LMatchHead, LMatchCond) -> + prometheus_metric:check_mf_exists(?TABLE, Registry, Name, LMatchHead), + ets:select_delete(?TABLE, delete_match_spec(Registry, Name, LMatchHead, LMatchCond)). + %% @equiv reset(default, Name, []) reset(Name) -> reset(default, Name, []). @@ -353,7 +376,10 @@ collect_metrics(Name, {CLabels, Labels, Registry, DU}) -> %%==================================================================== deregister_select(Registry, Name) -> - [{{{Registry, Name, '_', '_'}, '_', '_', '_'}, [], [true]}]. + delete_match_spec(Registry, Name, '_', []). + +delete_match_spec(Registry, Name, LMatchHead, LMatchCond) -> + [{{{Registry, Name, LMatchHead, '_'}, '_', '_', '_'}, LMatchCond, [true]}]. validate_summary_spec(Spec) -> Labels = prometheus_metric_spec:labels(Spec), diff --git a/test/eunit/metric/prometheus_boolean_tests.erl b/test/eunit/metric/prometheus_boolean_tests.erl index ad1973d0..f549ab57 100644 --- a/test/eunit/metric/prometheus_boolean_tests.erl +++ b/test/eunit/metric/prometheus_boolean_tests.erl @@ -14,6 +14,7 @@ prometheus_format_test_() -> fun test_toggle/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_collector1/1, fun test_collector2/1, @@ -66,7 +67,11 @@ test_errors(_) -> ?_assertError({unknown_metric, default, unknown_metric}, prometheus_boolean:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, - prometheus_boolean:remove(with_label, [repo, db])) + prometheus_boolean:remove(with_label, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_boolean:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_boolean:match_remove(with_label, [repo, db], [])) ]. test_set(_) -> @@ -145,6 +150,36 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_boolean:new([{name, fuse_state}, + {labels, [pool]}, + {help, ""}]), + prometheus_boolean:new([{name, simple_boolean}, {help, ""}]), + + prometheus_boolean:set(fuse_state, [mongodb], true), + prometheus_boolean:set(simple_boolean, true), + + BRValue1 = prometheus_boolean:value(fuse_state, [mongodb]), + BRValue2 = prometheus_boolean:value(simple_boolean), + + RResult1 = prometheus_boolean:match_remove(fuse_state, ['$1'], [{'=:=', '$1', mongodb}]), + RResult2 = prometheus_boolean:match_remove(simple_boolean), + + ARValue1 = prometheus_boolean:value(fuse_state, [mongodb]), + ARValue2 = prometheus_boolean:value(simple_boolean), + + RResult3 = prometheus_boolean:match_remove(fuse_state, ['$1'], [{'=:=', '$1', mongodb}]), + RResult4 = prometheus_boolean:match_remove(simple_boolean), + + [?_assertEqual(true, BRValue1), + ?_assertEqual(true, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_boolean:new([{name, fuse_state}, {labels, [name]}, diff --git a/test/eunit/metric/prometheus_counter_tests.erl b/test/eunit/metric/prometheus_counter_tests.erl index 3ba59013..a4ded369 100644 --- a/test/eunit/metric/prometheus_counter_tests.erl +++ b/test/eunit/metric/prometheus_counter_tests.erl @@ -13,6 +13,7 @@ prometheus_format_test_() -> fun test_inc/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_values/1, fun test_collector1/1, @@ -62,7 +63,11 @@ test_errors(_) -> ?_assertError({unknown_metric, default, unknown_metric}, prometheus_counter:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, - prometheus_counter:remove(db_query_duration, [repo, db])) + prometheus_counter:remove(db_query_duration, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_counter:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_counter:match_remove(db_query_duration, [repo, db], [])) ]. test_inc(_) -> @@ -123,6 +128,36 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_counter:new([{name, http_requests_total}, + {labels, [method]}, + {help, "Http request count"}]), + prometheus_counter:new([{name, simple_counter}, {help, ""}]), + + prometheus_counter:inc(http_requests_total, [get]), + prometheus_counter:inc(simple_counter), + + BRValue1 = prometheus_counter:value(http_requests_total, [get]), + BRValue2 = prometheus_counter:value(simple_counter), + + RResult1 = prometheus_counter:match_remove(http_requests_total, ['$1'], [{'=:=', '$1', get}]), + RResult2 = prometheus_counter:match_remove(simple_counter), + + ARValue1 = prometheus_counter:value(http_requests_total, [get]), + ARValue2 = prometheus_counter:value(simple_counter), + + RResult3 = prometheus_counter:match_remove(http_requests_total, ['$1'], [{'=:=', '$1', get}]), + RResult4 = prometheus_counter:match_remove(simple_counter), + + [?_assertEqual(1, BRValue1), + ?_assertEqual(1, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_counter:new([{name, http_requests_total}, {labels, [method]}, diff --git a/test/eunit/metric/prometheus_gauge_tests.erl b/test/eunit/metric/prometheus_gauge_tests.erl index 9febafaa..117f5347 100644 --- a/test/eunit/metric/prometheus_gauge_tests.erl +++ b/test/eunit/metric/prometheus_gauge_tests.erl @@ -19,6 +19,7 @@ prometheus_format_test_() -> fun test_set_duration_milliseconds/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_values/1, fun test_collector1/1, @@ -90,7 +91,11 @@ test_errors(_) -> ?_assertError({unknown_metric, default, unknown_metric}, prometheus_gauge:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, - prometheus_gauge:remove(with_label, [repo, db])) + prometheus_gauge:remove(with_label, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_gauge:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_gauge:match_remove(with_label, [repo, db], [])) ]. test_set(_) -> @@ -265,6 +270,36 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_gauge:new([{name, pool_size}, + {labels, [pool]}, + {help, "pool size"}]), + prometheus_gauge:new([{name, simple_gauge}, {help, ""}]), + + prometheus_gauge:inc(pool_size, [mongodb]), + prometheus_gauge:inc(simple_gauge), + + BRValue1 = prometheus_gauge:value(pool_size, [mongodb]), + BRValue2 = prometheus_gauge:value(simple_gauge), + + RResult1 = prometheus_gauge:match_remove(pool_size, ['$1'], [{'=:=', '$1', mongodb}]), + RResult2 = prometheus_gauge:match_remove(simple_gauge), + + ARValue1 = prometheus_gauge:value(pool_size, [mongodb]), + ARValue2 = prometheus_gauge:value(simple_gauge), + + RResult3 = prometheus_gauge:match_remove(pool_size, ['$1'], [{'=:=', '$1', mongodb}]), + RResult4 = prometheus_gauge:match_remove(simple_gauge), + + [?_assertEqual(1, BRValue1), + ?_assertEqual(1, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_gauge:new([{name, pool_size}, {labels, [client]}, diff --git a/test/eunit/metric/prometheus_histogram_tests.erl b/test/eunit/metric/prometheus_histogram_tests.erl index 61774284..8ee1bd0d 100644 --- a/test/eunit/metric/prometheus_histogram_tests.erl +++ b/test/eunit/metric/prometheus_histogram_tests.erl @@ -16,6 +16,7 @@ prometheus_format_test_() -> fun test_observe_duration_milliseconds/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_values/1, fun test_collector1/1, @@ -80,6 +81,10 @@ test_errors(_) -> prometheus_histogram:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, prometheus_histogram:remove(db_query_duration, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_histogram:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_histogram:match_remove(db_query_duration, [repo, db], [])), %% histogram specific errors ?_assertError({no_buckets, []}, @@ -287,6 +292,41 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_histogram:new([{name, histogram}, + {buckets, [5, 10]}, + {labels, [pool]}, + {help, ""}]), + prometheus_histogram:new([{name, simple_histogram}, + {buckets, [5, 10]}, + {help, ""}]), + + prometheus_histogram:observe(histogram, [mongodb], 1), + prometheus_histogram:observe(simple_histogram, 1), + prometheus_histogram:observe(histogram, [mongodb], 6), + prometheus_histogram:observe(simple_histogram, 6), + + BRValue1 = prometheus_histogram:value(histogram, [mongodb]), + BRValue2 = prometheus_histogram:value(simple_histogram), + + RResult1 = prometheus_histogram:match_remove(histogram, ['$1'], [{'=:=', '$1', mongodb}]), + RResult2 = prometheus_histogram:match_remove(simple_histogram), + + ARValue1 = prometheus_histogram:value(histogram, [mongodb]), + ARValue2 = prometheus_histogram:value(simple_histogram), + + RResult3 = prometheus_histogram:match_remove(histogram, ['$1'], [{'=:=', '$1', mongodb}]), + RResult4 = prometheus_histogram:match_remove(simple_histogram), + + [?_assertEqual({[1, 1, 0], 7}, BRValue1), + ?_assertEqual({[1, 1, 0], 7}, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_histogram:new([{name, duration_histogram}, {labels, [label]}, diff --git a/test/eunit/metric/prometheus_quantile_summary_tests.erl b/test/eunit/metric/prometheus_quantile_summary_tests.erl index 20d50f39..e057fb62 100644 --- a/test/eunit/metric/prometheus_quantile_summary_tests.erl +++ b/test/eunit/metric/prometheus_quantile_summary_tests.erl @@ -17,6 +17,7 @@ prometheus_format_test_() -> fun test_observe_duration_milliseconds/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_values/1, fun test_collector1/1, @@ -71,6 +72,10 @@ test_errors(_) -> prometheus_quantile_summary:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, prometheus_quantile_summary:remove(db_query_duration, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_quantile_summary:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_quantile_summary:match_remove(db_query_duration, [repo, db], [])), %% summary specific errors ?_assertError({invalid_value, "qwe", "observe accepts only numbers"}, prometheus_quantile_summary:observe(orders_summary, "qwe")), @@ -218,6 +223,34 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_quantile_summary:new([{name, summary}, {labels, [pool]}, {help, ""}]), + prometheus_quantile_summary:new([{name, simple_summary}, {help, ""}]), + + prometheus_quantile_summary:observe(summary, [mongodb], 1), + prometheus_quantile_summary:observe(simple_summary, 1), + + BRValue1 = prometheus_quantile_summary:value(summary, [mongodb]), + BRValue2 = prometheus_quantile_summary:value(simple_summary), + + RResult1 = prometheus_quantile_summary:match_remove(summary, ['$1'], [{'=:=', '$1', mongodb}]), + RResult2 = prometheus_quantile_summary:match_remove(simple_summary), + + ARValue1 = prometheus_quantile_summary:value(summary, [mongodb]), + ARValue2 = prometheus_quantile_summary:value(simple_summary), + + RResult3 = prometheus_quantile_summary:match_remove(summary, ['$1'], [{'=:=', '$1', mongodb}]), + RResult4 = prometheus_quantile_summary:match_remove(simple_summary), + + [?_assertMatch({1, 1, _}, BRValue1), + ?_assertMatch({1, 1, _}, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_quantile_summary:new([{name, orders_summary}, {labels, [department]}, diff --git a/test/eunit/metric/prometheus_summary_tests.erl b/test/eunit/metric/prometheus_summary_tests.erl index 11588880..447b56db 100644 --- a/test/eunit/metric/prometheus_summary_tests.erl +++ b/test/eunit/metric/prometheus_summary_tests.erl @@ -15,6 +15,7 @@ prometheus_format_test_() -> fun test_observe_duration_milliseconds/1, fun test_deregister/1, fun test_remove/1, + fun test_match_remove/1, fun test_default_value/1, fun test_values/1, fun test_collector1/1, @@ -69,6 +70,10 @@ test_errors(_) -> prometheus_summary:remove(unknown_metric)), ?_assertError({invalid_metric_arity, 2, 1}, prometheus_summary:remove(db_query_duration, [repo, db])), + ?_assertError({unknown_metric, default, unknown_metric}, + prometheus_summary:match_remove(unknown_metric)), + ?_assertError({invalid_metric_arity, 2, 1}, + prometheus_summary:match_remove(db_query_duration, [repo, db], [])), %% summary specific errors ?_assertError({invalid_value, "qwe", "observe accepts only numbers"}, prometheus_summary:observe(orders_summary, "qwe")), @@ -188,6 +193,34 @@ test_remove(_) -> ?_assertEqual(false, RResult3), ?_assertEqual(false, RResult4)]. +test_match_remove(_) -> + prometheus_summary:new([{name, summary}, {labels, [pool]}, {help, ""}]), + prometheus_summary:new([{name, simple_summary}, {help, ""}]), + + prometheus_summary:observe(summary, [mongodb], 1), + prometheus_summary:observe(simple_summary, 1), + + BRValue1 = prometheus_summary:value(summary, [mongodb]), + BRValue2 = prometheus_summary:value(simple_summary), + + RResult1 = prometheus_summary:match_remove(summary, ['$1'], [{'=:=', '$1', mongodb}]), + RResult2 = prometheus_summary:match_remove(simple_summary), + + ARValue1 = prometheus_summary:value(summary, [mongodb]), + ARValue2 = prometheus_summary:value(simple_summary), + + RResult3 = prometheus_summary:match_remove(summary, ['$1'], [{'=:=', '$1', mongodb}]), + RResult4 = prometheus_summary:match_remove(simple_summary), + + [?_assertEqual({1, 1}, BRValue1), + ?_assertEqual({1, 1}, BRValue2), + ?_assertEqual(1, RResult1), + ?_assertEqual(1, RResult2), + ?_assertEqual(undefined, ARValue1), + ?_assertEqual(undefined, ARValue2), + ?_assertEqual(0, RResult3), + ?_assertEqual(0, RResult4)]. + test_default_value(_) -> prometheus_summary:new([{name, orders_summary}, {labels, [department]},