forked from TheRealReal/hackney_telemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackney_telemetry_sup.erl
59 lines (46 loc) · 1.96 KB
/
hackney_telemetry_sup.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
%%%-----------------------------------------------------------------------------
%%% @doc hackney_telemetry application supervisor.
%%% @end
%%%-----------------------------------------------------------------------------
-module(hackney_telemetry_sup).
-behaviour(supervisor).
% supervisor callbacks
-export([init/1]).
% public api
-export([start_link/0, start_worker/1, stop_worker/1]).
-define(SERVER, ?MODULE).
-include("hackney_telemetry.hrl").
start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 0, period => 1},
ChildSpecs =
[
hackney_telemetry_worker:child_spec([{metric, [hackney, nb_requests]}]),
hackney_telemetry_worker:child_spec([{metric, [hackney, total_requests]}]),
hackney_telemetry_worker:child_spec([{metric, [hackney, finished_requests]}])
],
{ok, {SupFlags, ChildSpecs}}.
%%%=============================================================================
%%% @doc Starts a worker for the given metric under this supervisor
%%% @end
%%%=============================================================================
-spec start_worker(hackney_metric()) -> ok.
start_worker(Metric) ->
ChildSpec = hackney_telemetry_worker:child_spec([{metric, Metric}]),
supervisor:start_child(?MODULE, ChildSpec),
ok.
%%%=============================================================================
%%% @doc Stops the worker associated with the given metric name, if it exists.
%%% @end
%%%=============================================================================
-spec stop_worker(hackney_metric()) -> ok | undefined.
stop_worker(Metric) ->
WorkerId = {hackney_telemetry_worker, Metric},
{global, WorkerName} = hackney_telemetry_worker:worker_name(Metric),
case global:whereis_name(WorkerName) of
undefined -> undefined;
_Pid ->
ok = supervisor:terminate_child(?SERVER, WorkerId),
ok = supervisor:delete_child(?SERVER, WorkerId),
ok
end.