./ct_report/coverage/mod_event_pusher_http.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% File : mod_event_pusher_http.erl
3 %%% Author : Baibossynv Valery <baibossynov.valery@gmail.com>
4 %%% Purpose : Message passing via http
5 %%% Created : 16 Dec 2015 by Baibossynv Valery <baibossynov.valery@gmail.com>
6 %%%----------------------------------------------------------------------
7
8 -module(mod_event_pusher_http).
9 -author("baibossynov.valery@gmail.com").
10
11 -ignore_xref([behaviour_info/1]).
12
13 -behaviour(gen_mod).
14 -behaviour(mod_event_pusher).
15 -behaviour(mongoose_module_metrics).
16
17 -callback should_make_req(Acc :: mongoose_acc:t(),
18 Dir :: in | out,
19 Packet :: exml:element(),
20 From :: jid:jid(),
21 To :: jid:jid(),
22 Opts :: [{atom(), term()}]) -> boolean().
23 -callback prepare_headers(Acc :: mongoose_acc:t(),
24 Dir :: in | out,
25 Domain :: jid:lserver(),
26 Message :: binary(),
27 Sender :: jid:luser(),
28 Receiver :: jid:luser(),
29 Opts :: [{atom(), term()}]) -> [{binary(), binary()}].
30 -callback prepare_body(Acc :: mongoose_acc:t(),
31 Dir :: in | out,
32 Domain :: jid:lserver(),
33 Message :: binary(),
34 Sender :: jid:luser(),
35 Receiver :: jid:luser(),
36 Opts :: [{atom(), term()}]) -> binary().
37
38 -include("mod_event_pusher_events.hrl").
39 -include("jlib.hrl").
40
41 %% API
42 -export([start/2, stop/1, config_spec/0, push_event/2]).
43
44 %% config spec callbacks
45 -export([fix_path/1]).
46
47 -include("mongoose.hrl").
48 -include("jlib.hrl").
49 -include("mongoose_config_spec.hrl").
50
51 -define(SENT_METRIC, [mod_event_pusher_http, sent]).
52 -define(FAILED_METRIC, [mod_event_pusher_http, failed]).
53 -define(RESPONSE_METRIC, [mod_event_pusher_http, response_time]).
54
55 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
56 start(HostType, _Opts) ->
57
:-(
ensure_metrics(HostType),
58
:-(
ok.
59
60 -spec stop(mongooseim:host_type()) -> ok.
61 stop(_HostType) ->
62
:-(
ok.
63
64 -spec config_spec() -> mongoose_config_spec:config_section().
65 config_spec() ->
66 108 #section{items = #{<<"handlers">> => #list{items = handler_config_spec(),
67 validate = unique}},
68 defaults = #{<<"handlers">> => []}}.
69
70 handler_config_spec() ->
71 108 #section{items = #{<<"pool_name">> => #option{type = atom,
72 validate = pool_name},
73 <<"path">> => #option{type = binary,
74 process = fun ?MODULE:fix_path/1},
75 <<"callback_module">> => #option{type = atom,
76 validate = module}
77 },
78 defaults = #{<<"pool_name">> => http_pool,
79 <<"path">> => <<>>,
80 <<"callback_module">> => mod_event_pusher_http_defaults}
81 }.
82
83 push_event(Acc, #chat_event{direction = Dir, from = From, to = To, packet = Packet}) ->
84
:-(
HostType = mongoose_acc:host_type(Acc),
85
:-(
lists:map(fun(Opts) -> push_event(Acc, Dir, From, To, Packet, Opts) end,
86 gen_mod:get_module_opt(HostType, ?MODULE, handlers)),
87
:-(
Acc;
88 push_event(Acc, _Event) ->
89
:-(
Acc.
90
91 push_event(Acc, Dir, From, To, Packet, Opts = #{callback_module := Mod}) ->
92
:-(
Body = exml_query:path(Packet, [{element, <<"body">>}, cdata], <<>>),
93
:-(
case Mod:should_make_req(Acc, Dir, Packet, From, To, Opts) of
94 true ->
95
:-(
make_req(Acc, Dir, From#jid.lserver, From#jid.luser, To#jid.luser, Body, Opts);
96 _ ->
97
:-(
ok
98 end,
99
:-(
Acc.
100
101 %%%===================================================================
102 %%% Internal functions
103 %%%===================================================================
104
105 make_req(Acc, Dir, Domain, Sender, Receiver, Message, Opts) ->
106
:-(
#{pool_name := PoolName, path := Path, callback_module := Mod} = Opts,
107
:-(
HostType = mongoose_acc:host_type(Acc),
108
:-(
Body = Mod:prepare_body(Acc, Dir, Domain, Message, Sender, Receiver, Opts),
109
:-(
Headers = Mod:prepare_headers(Acc, Dir, Domain, Message, Sender, Receiver, Opts),
110
:-(
LogMeta = #{what => event_pusher_http_req,
111 text => <<"mod_event_pusher_http makes an external HTTP call">>,
112 path => Path, body => Body, headers => Headers,
113 sender => Sender, receiver => Receiver, direction => Dir,
114 server => Domain, pool_name => PoolName, acc => Acc},
115
:-(
?LOG_INFO(LogMeta),
116
:-(
T0 = os:timestamp(),
117
:-(
{Res, Elapsed} = case mongoose_http_client:post(HostType, PoolName, Path, Headers, Body) of
118 {ok, _} ->
119
:-(
{ok, timer:now_diff(os:timestamp(), T0)};
120 {error, Reason} ->
121
:-(
{{error, Reason}, 0}
122 end,
123
:-(
record_result(HostType, Res, Elapsed, LogMeta),
124
:-(
ok.
125
126 ensure_metrics(HostType) ->
127
:-(
mongoose_metrics:ensure_metric(HostType, ?SENT_METRIC, spiral),
128
:-(
mongoose_metrics:ensure_metric(HostType, ?FAILED_METRIC, spiral),
129
:-(
mongoose_metrics:ensure_metric(HostType, ?RESPONSE_METRIC, histogram),
130
:-(
ok.
131
132 record_result(HostType, ok, Elapsed, _LogMeta) ->
133
:-(
mongoose_metrics:update(HostType, ?SENT_METRIC, 1),
134
:-(
mongoose_metrics:update(HostType, ?RESPONSE_METRIC, Elapsed),
135
:-(
ok;
136 record_result(HostType, {error, Reason}, _, LogMeta) ->
137
:-(
mongoose_metrics:update(HostType, ?FAILED_METRIC, 1),
138
:-(
?LOG_WARNING(LogMeta#{what => event_pusher_http_req_failed,
139 text => <<"mod_event_pusher_http HTTP call failed">>,
140 reason => Reason
141
:-(
}),
142
:-(
ok.
143
144 %% @doc Strip initial slash (it is added by mongoose_http_client)
145 fix_path(<<"/", R/binary>>) ->
146
:-(
R;
147 fix_path(R) ->
148
:-(
R.
Line Hits Source