Skip to content

Commit

Permalink
write sasl reports to couch
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Feb 6, 2012
1 parent 055139c commit 308ee43
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
7 changes: 3 additions & 4 deletions rebar.config
@@ -1,10 +1,9 @@
{erl_opts, [debug_info]}. %, fail_on_warning]}.

{require_otp_vsn, "R14"}.

{deps_dir, ["deps"]}.

{deps, [
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {branch, "master"}}},
{jsx, ".*", {git, "https://github.com/spawnfest/jsx.git", {branch, "beamspirit"}}}
{couchbeam, ".*", {git, "https://github.com/benoitc/couchbeam.git", {branch, "master"}}},
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {branch, "master"}}},
{jsx, ".*", {git, "https://github.com/spawnfest/jsx.git", {branch, "beamspirit"}}}
]}.
35 changes: 33 additions & 2 deletions src/bigwig_error_handler.erl
Expand Up @@ -12,7 +12,9 @@
%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]).

-record(state, {}).
-record(state, {
cdb %% couchbeam handle, where we write sasl reports
}).

%%%----------------------------------------------------------------------------

Expand All @@ -21,10 +23,31 @@ add_sup_handler() ->

%%%----------------------------------------------------------------------------

init([]) -> {ok, #state{}}.
init([]) ->
Host = "localhost",
Port = 5984,
Prefix = "",
Options = [],
S = couchbeam:server_connection(Host, Port, Prefix, Options),
{ok, Db} = couchbeam:open_or_create_db(S, "bigwig-sasl", []),
{ok, #state{cdb=Db}}.

handle_event(Event, State) ->
%%io:format("EVENT(to ~p) ~p\n", [length(State#state.listeners), Event]),
Json = jsx:term_to_json(Event),
DocId = make_id(),
Doc = {[
{<<"_id">>, DocId},
{<<"report">>, Json}
]},
{ok, Doc1} = couchbeam:save_doc(State#state.cdb, Doc),
{Id,Rev} = couchbeam_doc:get_idrev(Doc1),
{ok, _} = couchbeam:put_attachment( State#state.cdb,
Id,
"report.erlang",
term_to_binary(Event),
[{rev,Rev},
{content_type,<<"application/x-erlang-binary">>}] ),
Msg = {?MODULE, Event},
bigwig_pubsubhub:notify(Msg),
{ok, State}.
Expand All @@ -38,3 +61,11 @@ terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%

make_id() -> iolist_to_binary(io_lib:format("~B", [unixtimehp()])).

unixtimehp() ->
{Meg,S,Mic} = erlang:now(),
(Meg*1000000 + S)*1000000 + Mic.

4 changes: 4 additions & 0 deletions src/bigwig_sup.erl
Expand Up @@ -23,6 +23,10 @@ start_link() ->
%% ===================================================================

init([]) ->
application:start(sasl),
application:start(ibrowse),
application:start(couchbeam),

Pubsub = ?CHILD(bigwig_pubsubhub, worker),
StatsSender = ?CHILD(bigwig_stats_sender, worker),
Http = ?CHILD(bigwig_http, worker),
Expand Down

0 comments on commit 308ee43

Please sign in to comment.