• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

emqx / emqx / 8613439193

09 Apr 2024 09:25AM UTC coverage: 62.491% (-0.1%) from 62.636%
8613439193

push

github

web-flow
Merge pull request #12854 from id/0409-update-codeowners

chore: update codeowners

34606 of 55378 relevant lines covered (62.49%)

6551.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/apps/emqx_opentelemetry/src/emqx_otel_cpu_sup.erl
1
%%--------------------------------------------------------------------
2
%% Copyright (c) 2024 EMQ Technologies Co., Ltd. All Rights Reserved.
3
%%
4
%% Licensed under the Apache License, Version 2.0 (the "License");
5
%% you may not use this file except in compliance with the License.
6
%% You may obtain a copy of the License at
7
%%
8
%%     http://www.apache.org/licenses/LICENSE-2.0
9
%%
10
%% Unless required by applicable law or agreed to in writing, software
11
%% distributed under the License is distributed on an "AS IS" BASIS,
12
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
%% See the License for the specific language governing permissions and
14
%% limitations under the License.
15
%%--------------------------------------------------------------------
16

17
-module(emqx_otel_cpu_sup).
18

19
-behaviour(gen_server).
20

21
-include_lib("emqx/include/logger.hrl").
22

23
%% gen_server APIs
24
-export([start_link/1]).
25

26
-export([
27
    start_otel_cpu_sup/1,
28
    stop_otel_cpu_sup/0,
29
    stats/1
30
]).
31

32
%% gen_server callbacks
33
-export([
34
    init/1,
35
    handle_continue/2,
36
    handle_call/3,
37
    handle_cast/2,
38
    handle_info/2,
39
    terminate/2,
40
    code_change/3
41
]).
42

43
-define(REFRESH, refresh).
44
-define(OTEL_CPU_USAGE_WORKER, ?MODULE).
45
-define(SUPERVISOR, emqx_otel_sup).
46

47
%%--------------------------------------------------------------------
48
%% API
49
%%--------------------------------------------------------------------
50

51
start_otel_cpu_sup(Conf) ->
52
    Spec = emqx_otel_sup:worker_spec(?MODULE, Conf),
×
53
    assert_started(supervisor:start_child(?SUPERVISOR, Spec)).
×
54

55
stop_otel_cpu_sup() ->
56
    case erlang:whereis(?SUPERVISOR) of
×
57
        undefined ->
58
            ok;
×
59
        Pid ->
60
            case supervisor:terminate_child(Pid, ?MODULE) of
×
61
                ok -> supervisor:delete_child(Pid, ?MODULE);
×
62
                {error, not_found} -> ok;
×
63
                Error -> Error
×
64
            end
65
    end.
66

67
stats(Name) ->
68
    gen_server:call(?OTEL_CPU_USAGE_WORKER, {?FUNCTION_NAME, Name}, infinity).
×
69

70
%%--------------------------------------------------------------------
71
%% gen_server callbacks
72
%% simply handle cpu_sup:util/0,1 called in one process
73
%%--------------------------------------------------------------------
74

75
start_link(Conf) ->
76
    gen_server:start_link({local, ?OTEL_CPU_USAGE_WORKER}, ?MODULE, Conf, []).
×
77

78
init(Conf) ->
79
    {ok, _InitState = #{}, {continue, {setup, Conf}}}.
×
80

81
%% Interval in milliseconds
82
handle_continue({setup, #{metrics := #{enable := true, interval := Interval}}}, State) ->
83
    %% start os_mon temporarily
84
    {ok, _} = application:ensure_all_started(os_mon),
×
85
    %% The returned value of the first call to cpu_sup:util/0 or cpu_sup:util/1 by a
86
    %% process will on most systems be the CPU utilization since system boot,
87
    %% but this is not guaranteed and the value should therefore be regarded as garbage.
88
    %% This also applies to the first call after a restart of cpu_sup.
89
    _Val = cpu_sup:util(),
×
90
    TRef = start_refresh_timer(Interval),
×
91
    {noreply, State#{interval => Interval, refresh_time_ref => TRef}}.
×
92

93
handle_call({stats, Name}, _From, State) ->
94
    {reply, get_stats(Name, State), State};
×
95
handle_call(stop, _From, State) ->
96
    cancel_outdated_timer(State),
×
97
    {stop, normal, State};
×
98
handle_call(Req, _From, State) ->
99
    ?SLOG(error, #{msg => "unexpected_call", call => Req}),
×
100
    {reply, ignored, State}.
×
101

102
handle_cast(Msg, State) ->
103
    ?SLOG(error, #{msg => "unexpected_cast", cast => Msg}),
×
104
    {noreply, State}.
×
105

106
handle_info({timeout, _Timer, ?REFRESH}, State) ->
107
    {noreply, refresh(State)}.
×
108

109
terminate(_Reason, _State) ->
110
    ok.
×
111

112
code_change(_OldVsn, State, _Extra) ->
113
    {ok, State}.
×
114

115
%%--------------------------------------------------------------------
116
%% Internal functions
117
%%--------------------------------------------------------------------
118

119
refresh(#{interval := Interval} = State) ->
120
    NState =
×
121
        case cpu_sup:util([]) of
122
            {all, Use, Idle, _} ->
123
                U = floor(Use * 100) / 100,
×
124
                I = ceil(Idle * 100) / 100,
×
125
                State#{'cpu.use' => U, 'cpu.idle' => I};
×
126
            _ ->
127
                State#{'cpu.use' => 0, 'cpu.idle' => 0}
×
128
        end,
129
    TRef = start_refresh_timer(Interval),
×
130
    NState#{refresh_time_ref => TRef}.
×
131

132
get_stats(Name, State) ->
133
    maps:get(Name, State, 0).
×
134

135
cancel_outdated_timer(#{refresh_time_ref := TRef}) ->
136
    emqx_utils:cancel_timer(TRef),
×
137
    ok.
×
138

139
start_refresh_timer(Interval) ->
140
    start_timer(Interval, ?REFRESH).
×
141

142
start_timer(Interval, Msg) ->
143
    emqx_utils:start_timer(Interval, Msg).
×
144

145
assert_started({ok, _Pid}) -> ok;
×
146
assert_started({ok, _Pid, _Info}) -> ok;
×
147
assert_started({error, {already_started, _Pid}}) -> ok;
×
148
assert_started({error, Reason}) -> {error, Reason}.
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc