Skip to content

Commit

Permalink
merge large trace support
Browse files Browse the repository at this point in the history
  • Loading branch information
djnym committed Sep 19, 2016
2 parents bbb81ed + 1a8c1ff commit ec96381
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 15 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 6.4.0 (molinaro)
* now able to post traces over 65kb
* updated the version of lwes dependency

Version 6.3.0 (molinaro)
* fix median calculation where there is only one sample (since this is
ill defined, it will be set to zero)
Expand Down
12 changes: 12 additions & 0 deletions mondemand_client-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

deps=
for d in deps/* ; do
deps="$deps -pa $d/ebin"
done
exec erl \
$deps \
-pa ebin \
-config mondemand_client_dev.config \
-s reloader \
-s mondemand_app
8 changes: 8 additions & 0 deletions mondemand_client_dev.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{ mondemand, [ { lwes_channel, { "127.0.0.1", 20502 } },
{ http_endpoint,
[ {trace, "http://127.0.0.1:20602/tcp"} ]
}
]
}
].
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
{deps,
[
{ lwes,
"4.2.0",
{git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.2.0"}}
"4.4.0",
{git, "git://github.com/lwes/lwes-erlang.git", {tag, "4.4.0"}}
},
{ parse_trans,
"2.9.2",
Expand Down
4 changes: 2 additions & 2 deletions src/mondemand.app.src
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{ application, mondemand,
[
{ description, "Erlang Mondemand Bindings." },
{ vsn, "6.3.0" },
{ vsn, "6.4.0" },
{ modules, [] },
{ registered, [mondemand,mondemand_sup]},
{ applications, [kernel,stdlib,syntax_tools,lwes]},
{ applications, [kernel,stdlib,syntax_tools,lwes,inets]},
{ env, [
{ config_file,"/etc/mondemand/mondemand.conf"},
{ send_interval, 60 }
Expand Down
54 changes: 45 additions & 9 deletions src/mondemand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
interval,
jitter,
timer,
channels = dict:new()
channels = dict:new(),
http_config
}).

%-=====================================================================-
Expand Down Expand Up @@ -129,6 +130,7 @@ add_sample (ProgId, Key, Context, Value)
when is_integer (Value), is_list (Context) ->
mondemand_statdb:add_sample (ProgId, Key, Context, Value).


all () ->
mondemand_statdb:all().

Expand Down Expand Up @@ -161,6 +163,9 @@ trace_owner_in_list (List) ->
orelse proplists:is_defined (?MD_TRACE_OWNER_KEY_LIST, List)
orelse proplists:is_defined (?MD_TRACE_OWNER_KEY_ATOM, List).

send_trace (ProgId, Message, Context)
when is_tuple (Context) andalso element (1, Context) =:= dict ->
send_trace (ProgId, Message, dict:to_list (Context));
send_trace (ProgId, Message, Context) ->
case Context of
List when is_list (Context) ->
Expand Down Expand Up @@ -335,6 +340,12 @@ init([]) ->
% use milliseconds for interval
Interval = IntervalSecs * 1000,

HttpConfig =
case mondemand_config:get_http_config () of
{error, _} -> undefined;
HC -> HC
end,

case mondemand_config:lwes_config () of
{error, Error} ->
{stop, {error, Error}};
Expand Down Expand Up @@ -363,7 +374,8 @@ init([]) ->
config = Config,
jitter = Jitter,
interval = Interval,
channels = ChannelDict
channels = ChannelDict,
http_config = HttpConfig
}
};
{error, Error} ->
Expand Down Expand Up @@ -402,20 +414,35 @@ handle_call (_Request, _From, State) ->

% send an event
handle_cast ({send, Name, Event},
State = #state { channels = ChannelDict }) ->
State = #state { http_config = HttpConfig }) ->
NewState =
case dict:find (Name, ChannelDict) of
{ok, Channels} ->
NewChannels = lwes:emit (Channels, Event),
State#state { channels = dict:store (Name, NewChannels, ChannelDict)};
case Name of
?MD_TRACE_EVENT ->
Bin = lwes_event:to_binary(Event),
case size(Bin) of
X when X > 65535 ->
post_via_http (Bin, HttpConfig),
State;
_ -> emit_one (Name, Bin, State)
end;
_ ->
error_logger:error_msg ("Unrecognized event ~p : ~p",[Name, dict:to_list(ChannelDict)]),
State
emit_one (Name, Event, State)
end,
{ noreply, NewState };
handle_cast (_Request, State) ->
{noreply, State}.

emit_one (Name, Event, State = #state { channels = ChannelDict }) ->
case dict:find (Name, ChannelDict) of
{ok, Channels} ->
NewChannels = lwes:emit (Channels, Event),
State#state { channels = dict:store (Name, NewChannels, ChannelDict)};
_ ->
error_logger:error_msg ("Unrecognized event ~p : ~p",
[Name, dict:to_list(ChannelDict)]),
State
end.

handle_info (flush, State = #state {timer = undefined, interval = Interval}) ->
{ok, TRef} = timer:send_interval (Interval, ?MODULE, flush),
flush (),
Expand Down Expand Up @@ -470,6 +497,15 @@ open_all (Config) ->
close_all (ChannelDict) ->
[ lwes:close (Channels) || {_, Channels} <- dict:to_list (ChannelDict) ].

post_via_http (Bin, HttpConfig) ->
case HttpConfig of
undefined -> ok;
_ -> Endpoint = proplists:get_value (trace, HttpConfig),
{ok, _ } = httpc:request (post,
{Endpoint, [], "", Bin},
[], [])
end.

%%--------------------------------------------------------------------
%%% Test functions
%%--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/mondemand_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
start() ->
[ ensure_started (App)
|| App
<- [sasl, syntax_tools, lwes, mondemand]
<- [sasl, syntax_tools, lwes, inets, mondemand]
].

%-=====================================================================-
Expand Down
25 changes: 24 additions & 1 deletion src/mondemand_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
lwes_config/0,
minutes_to_keep/0,
max_metrics/0,
parse_config/1
parse_config/1,
get_http_config/0
]).

-define (DEFAULT_MAX_SAMPLE_SIZE, 10).
Expand Down Expand Up @@ -249,6 +250,28 @@ minutes_to_keep () ->
max_metrics () ->
mondemand_global:get (?MOCHI_MAX_METRICS).

get_http_config () ->
case application:get_env (mondemand, http_endpoint) of
{ok, HttpConfig} ->
HttpConfig;
undefined ->
case application:get_env (mondemand, config_file) of
{ok, File} ->
case file:read_file (File) of
{ok, Bin} ->
{match, [TraceEndPoint]} =
re:run (Bin, "MONDEMAND_HTTP_ENDPOINT_TRACE=\"([^\"]+)\"",
[{capture, all_but_first, list}]),
[{trace, TraceEndPoint}];
E ->
E
end;
undefined ->
{error, no_http_configured}
end
end.


%%--------------------------------------------------------------------
%%% Test functions
%%--------------------------------------------------------------------
Expand Down

0 comments on commit ec96381

Please sign in to comment.