Skip to content

Commit

Permalink
Added functions match_remove/1,3,4 to all metrics
Browse files Browse the repository at this point in the history
match_remove can be used to selectively remove values for multiple
labels. The match head and match condition use the same rules as the
match speficiation parts of the same name.
  • Loading branch information
RoadRunnr committed May 19, 2021
1 parent b38f87d commit 53ba8ab
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 10 deletions.
29 changes: 28 additions & 1 deletion src/metrics/prometheus_boolean.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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},
Expand Down
29 changes: 28 additions & 1 deletion src/metrics/prometheus_counter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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),
Expand Down
28 changes: 27 additions & 1 deletion src/metrics/prometheus_gauge.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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),
Expand Down
32 changes: 30 additions & 2 deletions src/metrics/prometheus_histogram.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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), '_'),
Expand Down
29 changes: 28 additions & 1 deletion src/metrics/prometheus_quantile_summary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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),
Expand Down
28 changes: 27 additions & 1 deletion src/metrics/prometheus_summary.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'.<br/>
%% 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, []).
Expand Down Expand Up @@ -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),
Expand Down
37 changes: 36 additions & 1 deletion test/eunit/metric/prometheus_boolean_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(_) ->
Expand Down Expand Up @@ -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]},
Expand Down
Loading

0 comments on commit 53ba8ab

Please sign in to comment.