feat: prefix Graphite paths with current schema version
[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_binary(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 ok = hope_option:iter(SockOpt, fun gen_tcp:close/1),
63 {}.
64
65 %% ============================================================================
66
67 -spec try_to_send(state(), binary()) ->
68 state().
69 try_to_send(#state{sock=none}=State, _) ->
70 ?log_error("Sending failed. No socket in state."),
71 % TODO: Maybe schedule retry?
72 State;
73 try_to_send(#state{sock={some, Sock}}=State, Payload) ->
74 case gen_tcp:send(Sock, Payload)
75 of ok ->
76 State
77 ; {error, _}=Error ->
78 ?log_error("gen_tcp:send(~p, ~p) -> ~p", [Sock, Payload, Error]),
79 % TODO: Maybe schedule retry?
80 ok = gen_tcp:close(Sock),
81 State#state{sock=none}
82 end.
83
84 -spec try_to_connect_if_no_socket(state()) ->
85 state().
86 try_to_connect_if_no_socket(#state{sock={some, _}}=State) ->
87 State;
88 try_to_connect_if_no_socket(
89 #state
90 { sock = none
91 , host = Host
92 , port = Port
93 , timeout = Timeout
94 }=State
95 ) ->
96 Options = [binary, {active, false}],
97 case gen_tcp:connect(Host, Port, Options, Timeout)
98 of {ok, Sock} ->
99 State#state{sock = {some, Sock}}
100 ; {error, _}=Error ->
101 ?log_error(
102 "gen_tcp:connect(~p, ~p, ~p, ~p) -> ~p",
103 [Host, Port, Options, Timeout, Error]
104 ),
105 State#state{sock = none}
106 end.
107
108 -spec beam_stats_queue_to_binary(beam_stats_consumer:queue()) ->
109 binary().
110 beam_stats_queue_to_binary(Q) ->
111 Bins = [beam_stats_to_bins(B) || B <- queue:to_list(Q)],
112 iolist_to_binary(Bins).
113
114 -spec beam_stats_to_bins(beam_stats:t()) ->
115 [binary()].
116 beam_stats_to_bins(#beam_stats{}=BeamStats) ->
117 Msgs = beam_stats_msg_graphite:of_beam_stats(BeamStats),
118 lists:map(fun beam_stats_msg_graphite:to_bin/1, Msgs).
This page took 0.052703 seconds and 4 git commands to generate.