Skip to content
Permalink
Browse files
Merge pull request #70 from avasenin/tagged-notify
Add ability to pass tags to notify
  • Loading branch information
joewilliams committed Nov 4, 2013
2 parents a5bb8fc + 091ccfd commit 9745617d5d481b58a99459449a41647ddb30ecf0
Showing 3 changed files with 46 additions and 10 deletions.
@@ -36,14 +36,16 @@
notify/1,
notify/2,
notify/3,
tagged_notify/4,
notify_existing_metric/3,
get_handlers/0,
get_handlers_info/0,
get_info/1,
get_values/1,
get_history_values/2,
get_group_values/1,
get_group_values/2
get_group_values/2,
get_tags/1
]).

-record(metric, {
@@ -111,12 +113,16 @@ notify(Name, Event) ->

%% notify/3, makes sure metric exist, if not creates metric
notify(Name, Event, Type) ->
case handler_exists(Name) of
true ->
notify(Name, Event, Type, true);
false ->
notify(Name, Event, Type, false)
end.
notify(Name, Event, Type, handler_exists(Name)).

tagged_notify(Name, Event, Type, Tags) ->
R = notify(Name, Event, Type),
case get_tags(Name) of
{error, _, _} -> skip;
CurrentTags ->
[add_tag(Name, T) || T <- Tags, not sets:is_element(T, CurrentTags)]
end,
R.

%% assumes metric already exists, bypasses above checks
notify_existing_metric(Name, Event, Type) ->
@@ -131,8 +137,8 @@ get_handlers_info() ->
get_info(Name) ->
case handler_exists(Name) of
true ->
[{_, #metric{type = Type}}] = ets:lookup(?FOLSOM_TABLE, Name),
{Name, [{type, Type}]};
[{_, #metric{type = Type, tags = Tags}}] = ets:lookup(?FOLSOM_TABLE, Name),
{Name, [{type, Type}, {tags, Tags}]};
false ->
{error, Name, nonexistent_metric}
end.
@@ -175,6 +181,15 @@ get_group_values(Tag, Type) ->
Metrics = ets:match(?FOLSOM_TABLE, {'$1', {metric, '$2', Type, '_'}}),
[{Name, get_values(Name)} || [Name, Tags] <- Metrics, sets:is_element(Tag, Tags)].

get_tags(Name) ->
case handler_exists(Name) of
true ->
{_, Info} = get_info(Name),
proplists:get_value(tags, Info);
false ->
{error, Name, nonexistent_metric}
end.

%%%===================================================================
%%% Internal functions
%%%===================================================================
@@ -46,9 +46,11 @@
notify/1,
notify/2,
notify/3,
notify/4,
safely_notify/1,
safely_notify/2,
safely_notify/3,
safely_notify/4,
notify_existing_metric/3,
get_metrics/0,
metric_exists/1,
@@ -60,6 +62,7 @@
get_histogram_statistics/1,
get_histogram_statistics/2,
get_history_values/2,
get_tags/1,
histogram_timed_update/2,
histogram_timed_update/3,
histogram_timed_update/4,
@@ -140,6 +143,9 @@ notify(Name, Event) ->
notify(Name, Event, Type) ->
folsom_ets:notify(Name, Event, Type).

notify(Name, Event, Type, Tags) ->
folsom_ets:tagged_notify(Name, Event, Type, Tags).

safely_notify(Event) ->
catch notify(Event).

@@ -149,6 +155,9 @@ safely_notify(Name, Event) ->
safely_notify(Name, Event, Type) ->
catch notify(Name, Event, Type).

safely_notify(Name, Event, Type, Tags) ->
catch notify(Name, Event, Type, Tags).

notify_existing_metric(Name, Event, Type) ->
folsom_ets:notify_existing_metric(Name, Event, Type).

@@ -185,6 +194,9 @@ get_histogram_statistics(Name1, Name2) ->
get_history_values(Name, Count) ->
folsom_ets:get_history_values(Name, Count).

get_tags(Name) ->
folsom_ets:get_tags(Name).

histogram_timed_update(Name, Fun) ->
{Time, Value} = timer:tc(Fun),
ok = notify({Name, Time}),
@@ -140,6 +140,11 @@ populate_metrics() ->

[ok = folsom_metrics:notify({slide_sorted_a, Value}) || Value <- ?DATA2],

ok = folsom_metrics:notify(tagged_metric, 1, meter, [a, b]),
ok = folsom_metrics:notify(tagged_metric, 1, meter, [c]),

{error, _, unsupported_metric_type} = folsom_metrics:notify(tagged_unknown_metric, 1, unknown_metric, [tag]),

3.141592653589793 = folsom_metrics:histogram_timed_update(timed, math, pi, []),

Begin = folsom_metrics:histogram_timed_begin(timed2),
@@ -192,6 +197,8 @@ check_metrics() ->

2 = folsom_metrics:get_metric_value(<<"gauge">>),

true = sets:is_subset(sets:from_list([a,b,c]), folsom_metrics:get_tags(tagged_metric)),

[11,12,13,14,15,6,7,8,9,10] = folsom_metrics:get_metric_value(noneb),

[11,12,13,14,15] = folsom_metrics:get_metric_value(nonec),
@@ -302,7 +309,7 @@ check_group_metrics() ->
{counter, 0} = lists:keyfind(counter,1,Metrics).

delete_metrics() ->
20 = length(ets:tab2list(?FOLSOM_TABLE)),
21 = length(ets:tab2list(?FOLSOM_TABLE)),

ok = folsom_metrics:delete_metric(counter),
ok = folsom_metrics:delete_metric(counter2),
@@ -320,6 +327,8 @@ delete_metrics() ->
ok = folsom_metrics:delete_metric(noneb),
ok = folsom_metrics:delete_metric(nonec),

ok = folsom_metrics:delete_metric(tagged_metric),

ok = folsom_metrics:delete_metric(slide_sorted_a),

ok = folsom_metrics:delete_metric(timed),

0 comments on commit 9745617

Please sign in to comment.