1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(pgmp_telemetry). |
17 |
|
|
18 |
|
|
19 |
|
-export([callback_mode/0]). |
20 |
|
-export([init/1]). |
21 |
|
-export([start_link/0]). |
22 |
|
-export([start_link/1]). |
23 |
|
-export([terminate/3]). |
24 |
|
-include_lib("kernel/include/logger.hrl"). |
25 |
|
|
26 |
|
|
27 |
|
start_link() -> |
28 |
10 |
?FUNCTION_NAME(#{}). |
29 |
|
|
30 |
|
|
31 |
|
start_link(Arg) -> |
32 |
10 |
gen_statem:start_link(?MODULE, [Arg], envy_gen:options(?MODULE)). |
33 |
|
|
34 |
|
|
35 |
|
init([_Arg]) -> |
36 |
10 |
try |
37 |
10 |
M = pgmp_config:telemetry(module), |
38 |
:-( |
F = pgmp_config:telemetry(function), |
39 |
|
|
40 |
:-( |
Telemetry = fun M:F/4, |
41 |
|
|
42 |
:-( |
case telemetry:attach_many( |
43 |
|
?MODULE, |
44 |
|
phrase_file:consult(pgmp_config:telemetry(event_names)), |
45 |
|
fun M:F/4, |
46 |
|
pgmp_config:telemetry(config)) of |
47 |
|
|
48 |
|
ok -> |
49 |
:-( |
{ok, ready, #{telemetry => Telemetry}, hibernate}; |
50 |
|
|
51 |
|
{error, Reason} -> |
52 |
:-( |
{stop, Reason} |
53 |
|
end |
54 |
|
|
55 |
|
catch |
56 |
|
error:badarg -> |
57 |
10 |
?LOG_NOTICE(#{?MODULE => "no configuration"}), |
58 |
10 |
ignore |
59 |
|
end. |
60 |
|
|
61 |
|
|
62 |
|
callback_mode() -> |
63 |
:-( |
handle_event_function. |
64 |
|
|
65 |
|
|
66 |
|
terminate(_Reason, _State, _Data) -> |
67 |
:-( |
telemetry:detach(?MODULE). |