diff --git a/src/metrics.erl b/src/metrics.erl index 8bd8df9..50d2cea 100644 --- a/src/metrics.erl +++ b/src/metrics.erl @@ -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]). @@ -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(). @@ -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. diff --git a/src/metrics_exometer.erl b/src/metrics_exometer.erl index e33b134..86e99d2 100644 --- a/src/metrics_exometer.erl +++ b/src/metrics_exometer.erl @@ -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}. @@ -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). diff --git a/src/metrics_folsom.erl b/src/metrics_folsom.erl index 5d6ff6b..ffac909 100644 --- a/src/metrics_folsom.erl +++ b/src/metrics_folsom.erl @@ -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) -> @@ -43,3 +43,6 @@ update_or_create(Name, Probe, Type, Config) -> Error -> Error end. + +delete(Name, _Config) -> + folsom_metrics:delete_metric(Name). diff --git a/src/metrics_grapherl.erl b/src/metrics_grapherl.erl index 0073872..0acc812 100644 --- a/src/metrics_grapherl.erl +++ b/src/metrics_grapherl.erl @@ -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]). @@ -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) -> diff --git a/src/metrics_noop.erl b/src/metrics_noop.erl index 25e973e..5a280d3 100644 --- a/src/metrics_noop.erl +++ b/src/metrics_noop.erl @@ -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. diff --git a/test/metrics_tests.erl b/test/metrics_tests.erl index d372e4c..8077e36 100644 --- a/test/metrics_tests.erl +++ b/test/metrics_tests.erl @@ -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 ] } }. @@ -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 ] } }. @@ -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"), @@ -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)).