Permalink
Browse files

Added untag_metric/2 API call, updated unit tests accordingly, small …

…improvements
  • Loading branch information...
1 parent 1b5178a commit edcb9b9e4962e77d92d96a7ff58dd23ed6cfaf5c @AlexandreBeaulne AlexandreBeaulne committed Jan 26, 2013
Showing with 40 additions and 18 deletions.
  1. +5 −1 README.md
  2. +18 −14 src/folsom_ets.erl
  3. +4 −0 src/folsom_metrics.erl
  4. +13 −3 test/folsom_erlang_checks.erl
View
@@ -148,7 +148,11 @@ tag metrics:
> folsom_metrics:tag_metric(Name, Tag).
-and query a list of tuples `[{Name, Value}]` of all metrics with a given tag:
+and untag metrics:
+
+ > folsom_metrics:untag_metric(Name, Tag).
+
+Users can query a list of tuples `[{Name, Value}]` of all metrics with a given tag:
> folsom_metrics:get_metrics_value(Tag).
View
@@ -30,6 +30,7 @@
add_handler/4,
add_handler/5,
tag_handler/2,
+ untag_handler/2,
delete_handler/1,
handler_exists/1,
notify/1,
@@ -77,6 +78,14 @@ tag_handler(Name, Tag) ->
{error, Name, nonexistent_metric}
end.
+untag_handler(Name, Tag) ->
+ case handler_exists(Name) of
+ true ->
+ rm_tag(Name, Tag);
+ false ->
+ {error, Name, nonexistent_metric}
+ end.
+
delete_handler(Name) ->
{_, Info} = get_info(Name),
ok = delete_metric(Name, proplists:get_value(type, Info)).
@@ -160,10 +169,11 @@ get_history_values(Name, Count) ->
folsom_metrics_history:get_events(Name, Count).
get_group_values(Tag) ->
- [{Name, get_values(Name)} || Name <- get_handlers(), has_tag(Name, Tag)].
+ folsom_ets:get_group_values(Tag, '_').
get_group_values(Tag, Type) ->
- [{Name, get_values(Name)} || Name <- get_handlers(), has_tag(Name, Tag), has_type(Name, Type)].
+ Metrics = ets:match(?FOLSOM_TABLE, {'$1', {metric, '$2', Type, '_'}}),
+ [{Name, get_values(Name)} || [Name, Tags] <- Metrics, sets:is_element(Tag, Tags)].
%%%===================================================================
%%% Internal functions
@@ -246,20 +256,14 @@ maybe_add_handler(_, Name, _, _, _, true) ->
{error, Name, metric_already_exists}.
add_tag(Name, Tag) ->
- OldMetric = ets:lookup_element(?FOLSOM_TABLE, Name, 2),
- NewMetric = OldMetric#metric{tags=sets:add_element(Tag, get_tags(Name))},
- true = ets:update_element(?FOLSOM_TABLE, Name, {2, NewMetric}),
+ M = #metric{tags=Tags} = ets:lookup_element(?FOLSOM_TABLE, Name, 2),
+ true = ets:update_element(?FOLSOM_TABLE, Name, {2, M#metric{tags=sets:add_element(Tag, Tags)}}),
ok.
-get_tags(Name) ->
- Metric = ets:lookup_element(?FOLSOM_TABLE, Name, 2),
- Metric#metric.tags.
-
-has_tag(Name, Tag) ->
- sets:is_element(Tag, get_tags(Name)).
-
-has_type(Name, Type) ->
- {Name, [{type, Type}]} =:= get_info(Name).
+rm_tag(Name, Tag) ->
+ M = #metric{tags=Tags} = ets:lookup_element(?FOLSOM_TABLE, Name, 2),
+ true = ets:update_element(?FOLSOM_TABLE, Name, {2, M#metric{tags=sets:del_element(Tag, Tags)}}),
+ ok.
delete_metric(Name, history) ->
History = folsom_metrics_history:get_value(Name),
View
@@ -42,6 +42,7 @@
new_spiral/1,
delete_metric/1,
tag_metric/2,
+ untag_metric/2,
notify/1,
notify/2,
notify/3,
@@ -118,6 +119,9 @@ new_spiral(Name) ->
tag_metric(Name, Tag) ->
folsom_ets:tag_handler(Name, Tag).
+untag_metric(Name, Tag) ->
+ folsom_ets:untag_handler(Name, Tag).
+
delete_metric(Name) ->
folsom_ets:delete_handler(Name).
@@ -241,7 +241,8 @@ check_metrics() ->
[{count, 100}, {one, 100}] = folsom_metrics:get_metric_value(spiral).
check_group_metrics() ->
- Metrics = folsom_metrics:get_metrics_value("mygroup"),
+ Group = "mygroup",
+ Metrics = folsom_metrics:get_metrics_value(Group),
5 = length(Metrics),
{counter, 0} = lists:keyfind(counter,1,Metrics),
{counter2, 0} = lists:keyfind(counter2,1,Metrics),
@@ -263,9 +264,18 @@ check_group_metrics() ->
{spiral, [{count, 100}, {one, 100}]} = lists:keyfind(spiral,1,Metrics),
- Counters = folsom_metrics:get_metrics_value("mygroup",counter),
+ Counters = folsom_metrics:get_metrics_value(Group,counter),
{counter, 0} = lists:keyfind(counter,1,Counters),
- {counter2, 0} = lists:keyfind(counter2,1,Counters).
+ {counter2, 0} = lists:keyfind(counter2,1,Counters),
+
+ ok = folsom_metrics:untag_metric(counter2, Group),
+ ok = folsom_metrics:untag_metric(<<"gauge">>, Group),
+ ok = folsom_metrics:untag_metric(meter, Group),
+ ok = folsom_metrics:untag_metric(spiral, Group),
+ ?debugFmt("~n~nuntagged metrics: ~p, ~p, ~p and ~p in group ~p~n", [counter2,<<"gauge">>,meter,spiral,Group]),
+ RemainingMetrics = folsom_metrics:get_metrics_value(Group),
+ 1 = length(RemainingMetrics),
+ {counter, 0} = lists:keyfind(counter,1,Metrics).
delete_metrics() ->
17 = length(ets:tab2list(?FOLSOM_TABLE)),

0 comments on commit edcb9b9

Please sign in to comment.