Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics:delete/1 #3

Merged
merged 1 commit into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-export([update/1, update/2]).
-export([update_or_create/3]).
-export([backend/0, backend/1]).
-export([delete/1]).

-export([start_link/0]).

Expand Down Expand Up @@ -67,6 +68,11 @@ update(Name, Probe) ->
update_or_create(Name, Probe, Type) ->
metrics_mod:update_or_create(Name, Probe, Type).

%% @doc delete a metric
-spec delete(list()) -> ok | any().
delete(Name) ->
metrics_mod:delete(Name).


%% @doc retrieve the current backend name
-spec backend() -> atom().
Expand Down Expand Up @@ -172,9 +178,11 @@ build_metrics_mod(Mod, Config) when is_atom(Mod), is_map(Config) ->
[?Q("(Name, Probe) -> _@Mod@:update(Name, Probe, _@Config@)")]),
UpdateOrCreate = erl_syntax:function(merl:term('update_or_create'),
[?Q("(Name, Probe, Type) -> _@Mod@:update_or_create(Name, Probe, Type, _@Config@)")]),
Delete = erl_syntax:function(merl:term('delete'),
[?Q("(Name) -> _@Mod@:delete(Name, _@Config@)")]),
Module = ?Q("-module('metrics_mod')."),
Exported = ?Q("-export(['new'/2, 'update'/2, 'update_or_create'/3])."),
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update, UpdateOrCreate]],
Exported = ?Q("-export(['new'/2, 'update'/2, 'update_or_create'/3, 'delete'/1])."),
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update, UpdateOrCreate, Delete]],
Forms = lists:flatten([Module, Exported, Functions]),
merl:compile_and_load(Forms, [verbose]),
ok.
5 changes: 4 additions & 1 deletion src/metrics_exometer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-author("Benoit Chesneau").

%% API
-export([new/3, update/3, update_or_create/4]).
-export([new/3, update/3, update_or_create/4, delete/2]).


-spec new(atom(), any(), map()) -> ok | {error, metric_exists | unsupported_type}.
Expand All @@ -35,3 +35,6 @@ update_or_create(Name, {c, I}, Type, _Config) when is_integer(I) ->
exometer:update_or_create(Name, I, Type, []);
update_or_create(Name, Val, Type, _Config) ->
exometer:update_or_create(Name, Val, Type, []).

delete(Name, _Config) ->
exometer:delete(Name).
5 changes: 4 additions & 1 deletion src/metrics_folsom.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-author("Benoit Chesneau").

%% API
-export([new/3, update/3, update_or_create/4]).
-export([new/3, update/3, update_or_create/4, delete/2]).

-spec new(atom(), any(), map()) -> ok | {error, term()}.
new(counter, Name, _Config) ->
Expand Down Expand Up @@ -43,3 +43,6 @@ update_or_create(Name, Probe, Type, Config) ->
Error ->
Error
end.

delete(Name, _Config) ->
folsom_metrics:delete_metric(Name).
4 changes: 3 additions & 1 deletion src/metrics_grapherl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-author("Benoit Chesneau").

%% API
-export([init/0, new/3, update/3, update_or_create/4]).
-export([init/0, new/3, update/3, update_or_create/4, delete/2]).

-export([send_metrics/3]).

Expand All @@ -37,6 +37,8 @@ update(Name, Probe, Config) -> spawn(?MODULE, send_metrics, [Name, Probe, Config

update_or_create(Name, Probe, _Type, Config) -> update(Name, Probe, Config).

delete(_Name, _Config) -> ok.

parse_address({_, _, _, _}=Addr) -> Addr;
parse_address({_, _, _, _, _, _, _, _}= Addr) -> Addr;
parse_address(S) when is_binary(S) ->
Expand Down
3 changes: 2 additions & 1 deletion src/metrics_noop.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
-author("Benoit Chesneau").

%% API
-export([new/3, update/3, update_or_create/4]).
-export([new/3, update/3, update_or_create/4, delete/2]).

new(_Name, _Type, _Config) -> ok.
update(_Name, _Probe, _Config) -> ok.
update_or_create(_Name, _Probe, _Type, _Config) -> ok.
delete(_Name, _Config) -> ok.
20 changes: 18 additions & 2 deletions test/metrics_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ folsom_test_() ->
fun folsom_counter_test_inc_/1,
fun folsom_counter_test_mul_/1,
fun folsom_gauge_test_/1,
fun folsom_update_or_create_/1
fun folsom_update_or_create_/1,
fun folsom_delete_/1
]
}
}.
Expand All @@ -52,7 +53,8 @@ exometer_test_() ->
fun exometer_counter_test_inc_/1,
fun exometer_counter_test_mul_/1,
fun exometer_gauge_test_/1,
fun exometer_update_or_create_/1
fun exometer_update_or_create_/1,
fun exometer_delete_/1
]
}
}.
Expand Down Expand Up @@ -89,6 +91,13 @@ folsom_update_or_create_(_) ->
metrics:update_or_create("new_counter", {c, 1}, counter),
?_assertEqual(1, folsom_metrics:get_metric_value("new_counter")).

folsom_delete_(_) ->
ok = metrics:backend(metrics_folsom),
ok = metrics:new(gauge, "g"),
ok = metrics:new(counter, "c"),
ok = metrics:delete("g"),
?_assertEqual(["c"], folsom_metrics:get_metrics()).

exometer_counter_test_(_) ->
ok = metrics:backend(metrics_exometer),
ok = metrics:new(counter, "c1"),
Expand Down Expand Up @@ -120,3 +129,10 @@ exometer_update_or_create_(_) ->
ok = metrics:backend(metrics_exometer),
metrics:update_or_create("new_exo_counter", {c, 1}, counter),
?_assertMatch({ok, [{value, 1}, _]}, exometer:get_value("new_exo_counter")).

exometer_delete_(_) ->
ok = metrics:backend(metrics_exometer),
ok = metrics:new(gauge, "g"),
ok = metrics:new(counter, "c"),
ok = metrics:delete("g"),
?_assertEqual(undefined, exometer:info("g", status)).