Skip to content

Commit e035d5a

Browse files
committed
add metrics:update_or_create/3 .
1 parent e0dd8a0 commit e035d5a

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

src/metrics.erl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
%% API
1414
-export([new/2]).
1515
-export([update/1, update/2]).
16+
-export([update_or_create/3]).
1617
-export([backend/0, backend/1]).
1718

1819
-export([start_link/0]).
@@ -61,6 +62,12 @@ update(Name) ->
6162
update(Name, Probe) ->
6263
metrics_mod:update(Name, Probe).
6364

65+
%% @doc update a metric and create it if it doesn't exists
66+
-spec update_or_create(list(), probe(), metric()) -> ok | any().
67+
update_or_create(Name, Probe, Type) ->
68+
metrics_mod:update_or_create(Name, Probe, Type).
69+
70+
6471
%% @doc retrieve the current backend name
6572
-spec backend() -> atom().
6673
backend() -> gen_server:call(?MODULE, get_backend).
@@ -163,9 +170,11 @@ build_metrics_mod(Mod, Config) when is_atom(Mod), is_map(Config) ->
163170
[?Q("(Type, Name) -> _@Mod@:new(Type, Name, _@Config@)")]),
164171
Update = erl_syntax:function(merl:term('update'),
165172
[?Q("(Name, Probe) -> _@Mod@:update(Name, Probe, _@Config@)")]),
173+
UpdateOrCreate = erl_syntax:function(merl:term('update_or_create'),
174+
[?Q("(Name, Probe, Type) -> _@Mod@:update_or_create(Name, Probe, Type, _@Config@)")]),
166175
Module = ?Q("-module('metrics_mod')."),
167-
Exported = ?Q("-export(['new'/2, 'update'/2])."),
168-
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update]],
176+
Exported = ?Q("-export(['new'/2, 'update'/2, 'update_or_create'/3])."),
177+
Functions = [ ?Q("'@_F'() -> [].") || F <- [New, Update, UpdateOrCreate]],
169178
Forms = lists:flatten([Module, Exported, Functions]),
170179
merl:compile_and_load(Forms, [verbose]),
171180
ok.

src/metrics_exometer.erl

Lines changed: 6 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]).
14+
-export([new/3, update/3, update_or_create/4]).
1515

1616

1717
-spec new(atom(), any(), map()) -> ok | {error, metric_exists | unsupported_type}.
@@ -30,3 +30,8 @@ new(_, _, _) ->
3030

3131
update(Name, {c, I}, _Config) when is_integer(I) -> exometer:update(Name, I);
3232
update(Name, Val, _Config) -> exometer:update(Name, Val).
33+
34+
update_or_create(Name, {c, I}, Type, _Config) when is_integer(I) ->
35+
exometer:update_or_create(Name, I, Type, []);
36+
update_or_create(Name, Val, Type, _Config) ->
37+
exometer:update_or_create(Name, Val, Type, []).

src/metrics_folsom.erl

Lines changed: 11 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]).
14+
-export([new/3, update/3, update_or_create/4]).
1515

1616
-spec new(atom(), any(), map()) -> ok | {error, term()}.
1717
new(counter, Name, _Config) ->
@@ -33,3 +33,13 @@ update(Name, {c, I}, _Config) when I < 0 ->
3333
folsom_metrics:notify(Name, {dec, abs(I)});
3434
update(Name, Val, _Config) ->
3535
folsom_metrics:notify(Name, Val).
36+
37+
update_or_create(Name, Probe, Type, Config) ->
38+
case update(Name, Probe, Config) of
39+
ok -> ok;
40+
{error, Name, nonexistent_metric} ->
41+
new(Type, Name, Config),
42+
update(Name, Probe, Config);
43+
Error ->
44+
Error
45+
end.

src/metrics_grapherl.erl

Lines changed: 3 additions & 3 deletions
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]).
22+
-export([init/0, new/3, update/3, update_or_create/4]).
2323

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

@@ -33,9 +33,9 @@ new(counter, _Name, _Config) -> ok;
3333
new(gauge, _Name, _Config) -> ok;
3434
new(_, _, _) -> {error, unsupported_type}.
3535

36-
update(Name, Probe, Config) ->
37-
spawn(?MODULE, send_metrics, [Name, Probe, Config]).
36+
update(Name, Probe, Config) -> spawn(?MODULE, send_metrics, [Name, Probe, Config]).
3837

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

4040
parse_address({_, _, _, _}=Addr) -> Addr;
4141
parse_address({_, _, _, _, _, _, _, _}= Addr) -> Addr;

src/metrics_noop.erl

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

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

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

test/metrics_tests.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ folsom_test_() ->
3434
fun folsom_counter_test_/1,
3535
fun folsom_counter_test_inc_/1,
3636
fun folsom_counter_test_mul_/1,
37-
fun folsom_gauge_test_/1
37+
fun folsom_gauge_test_/1,
38+
fun folsom_update_or_create_/1
3839
]
3940
}
4041
}.
@@ -50,7 +51,8 @@ exometer_test_() ->
5051
fun exometer_counter_test_/1,
5152
fun exometer_counter_test_inc_/1,
5253
fun exometer_counter_test_mul_/1,
53-
fun exometer_gauge_test_/1
54+
fun exometer_gauge_test_/1,
55+
fun exometer_update_or_create_/1
5456
]
5557
}
5658
}.
@@ -82,6 +84,11 @@ folsom_gauge_test_(_) ->
8284
metrics:update("g", 1),
8385
?_assertEqual(1, folsom_metrics:get_metric_value("g")).
8486

87+
folsom_update_or_create_(_) ->
88+
ok = metrics:backend(metrics_folsom),
89+
metrics:update_or_create("new_counter", {c, 1}, counter),
90+
?_assertEqual(1, folsom_metrics:get_metric_value("new_counter")).
91+
8592
exometer_counter_test_(_) ->
8693
ok = metrics:backend(metrics_exometer),
8794
ok = metrics:new(counter, "c1"),
@@ -109,4 +116,7 @@ exometer_gauge_test_(_) ->
109116
metrics:update("g1", 1),
110117
?_assertMatch({ok, [{value, 1}, _]}, exometer:get_value("g1")).
111118

112-
119+
exometer_update_or_create_(_) ->
120+
ok = metrics:backend(metrics_exometer),
121+
metrics:update_or_create("new_exo_counter", {c, 1}, counter),
122+
?_assertMatch({ok, [{value, 1}, _]}, exometer:get_value("new_exo_counter")).

0 commit comments

Comments
 (0)