Upgrade hope to 4.0.0, which uses empty tuple as unit
[beam_stats.git] / src / beam_stats_consumer_graphite.erl
1 -module(beam_stats_consumer_graphite).
2
3 -include("include/beam_stats.hrl").
4 -include("beam_stats_logging.hrl").
5
6 -behaviour(beam_stats_consumer).
7
8 -export_type(
9 [ option/0
10 ]).
11
12 -export(
13 [ init/1
14 , consume/2
15 , terminate/1
16 ]).
17
18 -type option() ::
19 {consumption_interval , non_neg_integer()}
20 | {host , inet:ip_address() | inet:hostname()}
21 | {port , inet:port_number()}
22 | {timeout , timeout()}
23 .
24
25 -record(state,
26 { sock = none :: hope_option:t(Socket :: port())
27 , host :: inet:ip_address() | inet:hostname()
28 , port :: inet:port_number()
29 , timeout :: timeout()
30 }).
31
32 -type state() ::
33 #state{}.
34
35 -define(DEFAULT_HOST , "localhost").
36 -define(DEFAULT_PORT , 2003).
37 -define(DEFAULT_TIMEOUT , 5000).
38
39 -spec init([option()]) ->
40 {non_neg_integer(), state()}.
41 init(Options) ->
42 Get = fun (Key, Default) -> hope_kv_list:get(Options, Key, Default) end,
43 ConsumptionInterval = Get(consumption_interval, 60000),
44 State = #state
45 { sock = none
46 , host = Get(host , ?DEFAULT_HOST)
47 , port = Get(port , ?DEFAULT_PORT)
48 , timeout = Get(timeout , ?DEFAULT_TIMEOUT)
49 },
50 {ConsumptionInterval, State}.
51
52 -spec consume(beam_stats_consumer:queue(), state()) ->
53 state().
54 consume(Q, #state{}=State1) ->
55 Payload = beam_stats_queue_to_iolists(Q),
56 State2 = try_to_connect_if_no_socket(State1),
57 try_to_send(State2, Payload).
58
59 -spec terminate(state()) ->
60 {}.
61 terminate(#state{sock=SockOpt}) ->
62 hope_option:iter(SockOpt, fun gen_tcp:close/1).
63
64 %% ============================================================================
65
66 -spec try_to_send(state(), iolist()) ->
67 state().
68 try_to_send(#state{sock=none}=State, _) ->
69 ?log_error("Sending failed. No socket in state."),
70 % TODO: Maybe schedule retry?
71 State;
72 try_to_send(#state{sock={some, Sock}}=State, Payload) ->
73 case gen_tcp:send(Sock, Payload)
74 of ok ->
75 State
76 ; {error, _}=Error ->
77 ?log_error("gen_tcp:send(~p, Payload) -> ~p", [Sock, Error]),
78 % TODO: Maybe schedule retry?
79 ok = gen_tcp:close(Sock),
80 State#state{sock=none}
81 end.
82
83 -spec try_to_connect_if_no_socket(state()) ->
84 state().
85 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
86 State;
87 try_to_connect_if_no_socket(
88 #state
89 { sock = none
90 , host = Host
91 , port = Port
92 , timeout = Timeout
93 }=State
94 ) ->
95 Options = [binary, {active, false}],
96 case gen_tcp:connect(Host, Port, Options, Timeout)
97 of {ok, Sock} ->
98 State#state{sock = {some, Sock}}
99 ; {error, _}=Error ->
100 ?log_error(
101 "gen_tcp:connect(~p, ~p, ~p, ~p) -> ~p",
102 [Host, Port, Options, Timeout, Error]
103 ),
104 State#state{sock = none}
105 end.
106
107 -spec beam_stats_queue_to_iolists(beam_stats_consumer:queue()) ->
108 [iolist()].
109 beam_stats_queue_to_iolists(Q) ->
110 [beam_stats_to_iolist(B) || B <- queue:to_list(Q)].
111
112 -spec beam_stats_to_iolist(beam_stats:t()) ->
113 [iolist()].
114 beam_stats_to_iolist(#beam_stats{}=BeamStats) ->
115 Msgs = beam_stats_msg_graphite:of_beam_stats(BeamStats),
116 lists:map(fun beam_stats_msg_graphite:to_iolist/1, Msgs).
This page took 0.054156 seconds and 4 git commands to generate.