Skip to content

Commit 5cf713c

Browse files
authored
Merge pull request #3 from botsunit/add_delete
Add metrics:delete/1
2 parents a9294ae + acb2181 commit 5cf713c

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

src/metrics.erl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-export([update/1, update/2]).
1616
-export([update_or_create/3]).
1717
-export([backend/0, backend/1]).
18+
-export([delete/1]).
1819

1920
-export([start_link/0]).
2021

@@ -67,6 +68,11 @@ update(Name, Probe) ->
6768
update_or_create(Name, Probe, Type) ->
6869
metrics_mod:update_or_create(Name, Probe, Type).
6970

71+
%% @doc delete a metric
72+
-spec delete(list()) -> okany().
73+
delete(Name) ->
74+
metrics_mod:delete(Name).
75+
7076

7177
%% @doc retrieve the current backend name
7278
-spec backend() -> atom().
@@ -172,9 +178,11 @@ build_metrics_mod(Mod, Config) when is_atom(Mod), is_map(Config) ->
172178
[?Q("(Name, Probe) -> _@Mod@:update(Name, Probe, _@Config@)")]),
173179
UpdateOrCreate = erl_syntax:function(merl:term('update_or_create'),
174180
[?Q("(Name, Probe, Type) -> _@Mod@:update_or_create(Name, Probe, Type, _@Config@)")]),
181+
Delete = erl_syntax:function(merl:term('delete'),
182+
[?Q("(Name) -> _@Mod@:delete(Name, _@Config@)")]),
175183
Module = ?Q("-module('metrics_mod')."),
176-
Exported = ?Q("-export(['new'/2, 'update'/2, 'update_or_create'/3])."),
177-
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update, UpdateOrCreate]],
184+
Exported = ?Q("-export(['new'/2, 'update'/2, 'update_or_create'/3, 'delete'/1])."),
185+
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update, UpdateOrCreate, Delete]],
178186
Forms = lists:flatten([Module, Exported, Functions]),
179187
merl:compile_and_load(Forms, [verbose]),
180188
ok.

src/metrics_exometer.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-author("Benoit Chesneau").
1212

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

1616

1717
-spec new(atom(), any(), map()) -> ok | {error, metric_exists | unsupported_type}.
@@ -35,3 +35,6 @@ update_or_create(Name, {c, I}, Type, _Config) when is_integer(I) ->
3535
exometer:update_or_create(Name, I, Type, []);
3636
update_or_create(Name, Val, Type, _Config) ->
3737
exometer:update_or_create(Name, Val, Type, []).
38+
39+
delete(Name, _Config) ->
40+
exometer:delete(Name).

src/metrics_folsom.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-author("Benoit Chesneau").
1212

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

1616
-spec new(atom(), any(), map()) -> ok | {error, term()}.
1717
new(counter, Name, _Config) ->
@@ -43,3 +43,6 @@ update_or_create(Name, Probe, Type, Config) ->
4343
Error ->
4444
Error
4545
end.
46+
47+
delete(Name, _Config) ->
48+
folsom_metrics:delete_metric(Name).

src/metrics_grapherl.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
-author("Benoit Chesneau").
2020

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

2424
-export([send_metrics/3]).
2525

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

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

40+
delete(_Name, _Config) -> ok.
41+
4042
parse_address({_, _, _, _}=Addr) -> Addr;
4143
parse_address({_, _, _, _, _, _, _, _}= Addr) -> Addr;
4244
parse_address(S) when is_binary(S) ->

src/metrics_noop.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
-author("Benoit Chesneau").
1313

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

1717
new(_Name, _Type, _Config) -> ok.
1818
update(_Name, _Probe, _Config) -> ok.
1919
update_or_create(_Name, _Probe, _Type, _Config) -> ok.
20+
delete(_Name, _Config) -> ok.

test/metrics_tests.erl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ folsom_test_() ->
3535
fun folsom_counter_test_inc_/1,
3636
fun folsom_counter_test_mul_/1,
3737
fun folsom_gauge_test_/1,
38-
fun folsom_update_or_create_/1
38+
fun folsom_update_or_create_/1,
39+
fun folsom_delete_/1
3940
]
4041
}
4142
}.
@@ -52,7 +53,8 @@ exometer_test_() ->
5253
fun exometer_counter_test_inc_/1,
5354
fun exometer_counter_test_mul_/1,
5455
fun exometer_gauge_test_/1,
55-
fun exometer_update_or_create_/1
56+
fun exometer_update_or_create_/1,
57+
fun exometer_delete_/1
5658
]
5759
}
5860
}.
@@ -89,6 +91,13 @@ folsom_update_or_create_(_) ->
8991
metrics:update_or_create("new_counter", {c, 1}, counter),
9092
?_assertEqual(1, folsom_metrics:get_metric_value("new_counter")).
9193

94+
folsom_delete_(_) ->
95+
ok = metrics:backend(metrics_folsom),
96+
ok = metrics:new(gauge, "g"),
97+
ok = metrics:new(counter, "c"),
98+
ok = metrics:delete("g"),
99+
?_assertEqual(["c"], folsom_metrics:get_metrics()).
100+
92101
exometer_counter_test_(_) ->
93102
ok = metrics:backend(metrics_exometer),
94103
ok = metrics:new(counter, "c1"),
@@ -120,3 +129,10 @@ exometer_update_or_create_(_) ->
120129
ok = metrics:backend(metrics_exometer),
121130
metrics:update_or_create("new_exo_counter", {c, 1}, counter),
122131
?_assertMatch({ok, [{value, 1}, _]}, exometer:get_value("new_exo_counter")).
132+
133+
exometer_delete_(_) ->
134+
ok = metrics:backend(metrics_exometer),
135+
ok = metrics:new(gauge, "g"),
136+
ok = metrics:new(counter, "c"),
137+
ok = metrics:delete("g"),
138+
?_assertEqual(undefined, exometer:info("g", status)).

0 commit comments

Comments
 (0)