Skip to content

Commit

Permalink
Merge pull request #7 from mondemand/max_metrics
Browse files Browse the repository at this point in the history
Moved max_metrics constant to mondemand_config
  • Loading branch information
djnym authored Jun 11, 2016
2 parents c6ba766 + 2eb93c0 commit bcf844d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 6.2.1 (whalen)
* moved max_metrics constant to mondemand_config so we can tweak at runtime

Version 6.2.0 (molinaro)
* added a function to convert from millis since epoch to erlang:now/0
format
Expand Down
1 change: 0 additions & 1 deletion include/mondemand.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

% related to stats messages
-define (MD_STATS_EVENT, <<"MonDemand::StatsMsg">>).
-define (MD_MAX_METRICS, 1024).

% internal records for MonDemand::StatsMsg
-record (md_stats_msg, { send_time,
Expand Down
2 changes: 1 addition & 1 deletion src/mondemand.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ application, mondemand,
[
{ description, "Erlang Mondemand Bindings." },
{ vsn, "6.2.0" },
{ vsn, "6.2.1" },
{ modules, [] },
{ registered, [mondemand,mondemand_sup]},
{ applications, [kernel,stdlib,syntax_tools,lwes]},
Expand Down
14 changes: 13 additions & 1 deletion src/mondemand_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
default_stats/0,
lwes_config/0,
minutes_to_keep/0,
max_metrics/0,
parse_config/1
]).

-define (DEFAULT_MAX_SAMPLE_SIZE, 10).
-define (DEFAULT_STATS, [min, max, sum, count]).
-define (DEFAULT_MINUTES_TO_KEEP, 10).
-define (MOCHI_SENDER_HOST, mondemand_sender_host_global).
-define (MOCHI_MAX_METRICS, mondemand_max_metrics_global).
-define (DEFAULT_MAX_METRICS, 512).

% this function is meant to be called before the supervisor and
% pulls all those configs which are mostly static.
Expand All @@ -39,7 +42,13 @@ init () ->
undefined -> net_adm:localhost();
{ok, H} -> H
end,
mondemand_global:put (?MOCHI_SENDER_HOST, Host).
mondemand_global:put (?MOCHI_SENDER_HOST, Host),
MaxMetrics =
case application:get_env (mondemand, max_metrics) of
undefined -> ?DEFAULT_MAX_METRICS;
{ok, M} -> M
end,
mondemand_global:put (?MOCHI_MAX_METRICS, MaxMetrics).

host () ->
mondemand_global:get (?MOCHI_SENDER_HOST).
Expand Down Expand Up @@ -237,6 +246,9 @@ minutes_to_keep () ->
{ok, I} when is_integer (I) -> I
end.

max_metrics () ->
mondemand_global:get (?MOCHI_MAX_METRICS).

%%--------------------------------------------------------------------
%%% Test functions
%%--------------------------------------------------------------------
Expand Down
34 changes: 21 additions & 13 deletions src/mondemand_statsmsg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,17 @@ from_lwes (#lwes_event { attrs = Data}) ->
}.

to_lwes (L) when is_list (L) ->
lists:flatten (lists:map (fun to_lwes/1, L) );
MaxMetrics = mondemand_config:max_metrics (),
lists:flatten (lists:map (fun (X) -> to_lwes (X, MaxMetrics) end, L) );

% split events into chunks of MD_MAX_METRICS
to_lwes (MondemandStatsMsg = #md_stats_msg { num_metrics = NumMetrics })
when NumMetrics > ?MD_MAX_METRICS ->
lists:reverse (to_lwes( MondemandStatsMsg, []));
to_lwes (MondemandStatsMsg = #md_stats_msg {}) ->
MaxMetrics = mondemand_config:max_metrics (),
to_lwes (MondemandStatsMsg, MaxMetrics).

% split events into chunks of max_metrics
to_lwes (MondemandStatsMsg = #md_stats_msg { num_metrics = NumMetrics }, MaxMetrics)
when NumMetrics > MaxMetrics ->
lists:reverse (to_lwes( MondemandStatsMsg, MaxMetrics, []));

to_lwes (#md_stats_msg { send_time = SendTimeIn,
collect_time = CollectTimeIn,
Expand All @@ -232,7 +237,7 @@ to_lwes (#md_stats_msg { send_time = SendTimeIn,
context = Context,
num_metrics = NumMetrics,
metrics = Metrics
}) ->
}, _) ->
NowMillis = mondemand_util:millis_since_epoch(),
SendTime =
case SendTimeIn of
Expand Down Expand Up @@ -262,18 +267,19 @@ to_lwes (#md_stats_msg { send_time = SendTimeIn,
to_lwes (MondemandStatsMsg = #md_stats_msg {
num_metrics = NumMetrics,
metrics = Metrics
}, EventList)
when NumMetrics > ?MD_MAX_METRICS ->
{ HeadMetrics, TailMetrics } = lists:split (?MD_MAX_METRICS, Metrics),
}, MaxMetrics, EventList)
when NumMetrics > MaxMetrics ->
{ HeadMetrics, TailMetrics } = lists:split (MaxMetrics, Metrics),
to_lwes (MondemandStatsMsg#md_stats_msg {
num_metrics = length (TailMetrics),
metrics = TailMetrics },
MaxMetrics,
[ to_lwes (MondemandStatsMsg#md_stats_msg {
num_metrics = ?MD_MAX_METRICS,
num_metrics = MaxMetrics,
metrics = HeadMetrics})
| EventList ]);

to_lwes (MondemandStatsMsg = #md_stats_msg {}, EventList) ->
to_lwes (MondemandStatsMsg = #md_stats_msg {}, _, EventList) ->
[ to_lwes (MondemandStatsMsg) | EventList ].

metric_to_lwes (MetricIndex,
Expand Down Expand Up @@ -2035,8 +2041,10 @@ statsmsg_test_ () ->
},
{ "many metrics",
fun() ->
mondemand_config:init(),
C = [{<<"foo">>,<<"bar">>}],
MetricsCount = ?MD_MAX_METRICS + 1,
MaxMetrics = mondemand_config:max_metrics (),
MetricsCount = MaxMetrics + 1,
E2K0Name = list_to_binary(string:concat("baz", integer_to_list(MetricsCount))),
MIn = lists:map(fun(X) ->
{
Expand All @@ -2056,7 +2064,7 @@ statsmsg_test_ () ->
% test that to_lwes of long statsmsg returns several events that are correct
[E1, E2] = to_lwes(S),
% test that the metric counts and first metric in events match expected values
?assert (lists:member({?LWES_U_INT_16, <<"num">>, ?MD_MAX_METRICS}, E1#lwes_event.attrs)),
?assert (lists:member({?LWES_U_INT_16, <<"num">>, MaxMetrics}, E1#lwes_event.attrs)),
?assert (lists:member({?LWES_STRING, <<"k0">>, <<"baz1">>}, E1#lwes_event.attrs)),
?assert (lists:member({?LWES_STRING, <<"t0">>, <<"gauge">>}, E1#lwes_event.attrs)),
?assert (lists:member({?LWES_INT_64, <<"v0">>, 1}, E1#lwes_event.attrs)),
Expand Down

0 comments on commit bcf844d

Please sign in to comment.