-
Notifications
You must be signed in to change notification settings - Fork 77
/
bigwig_http_stats_stream.erl
44 lines (32 loc) · 1.28 KB
/
bigwig_http_stats_stream.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
%% Websocket connection that sends periodic stats about the VM
%%
-module(bigwig_http_stats_stream).
-behaviour(cowboy_http_handler).
-behaviour(cowboy_http_websocket_handler).
-export([init/3, handle/2, terminate/2]).
-export([websocket_init/3, websocket_handle/3, websocket_terminate/3,
websocket_info/3]).
init({tcp, http}, _Req, _Opts) ->
{upgrade, protocol, cowboy_http_websocket}.
handle(_Req, _State) ->
exit(websockets_only).
terminate(_Req, _State) ->
exit(websockets_only).
websocket_init(_TransportName, Req, _Opts) ->
bigwig_pubsubhub:register_client(self()),
{ok, Req, undefined_state}.
%% TODO handle stuff like {bigwig, {appmon, ... }} and send that too
websocket_info({bigwig, {bigwig_stats_sender, Stats}}, Req, State) ->
Reply = jsx:term_to_json([{stats, Stats}]),
{reply, {text, Reply}, Req, State};
websocket_info({bigwig, {bigwig_appmon, Stats}}, Req, State) ->
Reply = jsx:term_to_json([{appmon, Stats}]),
{reply, {text, Reply}, Req, State};
websocket_info({bigwig, _}, Req, State) ->
{ok, Req, State};
websocket_info(Info, Req, State) ->
io:format("Unhandled msg to ~p ~p\n", [?MODULE, Info]),
{ok, Req, State}.
websocket_terminate(_Reason, _Req, _State) -> ok.
websocket_handle(_Msg, Req, State) ->
{ok, Req, State}.