Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit a2b35d5

Browse files
committed
migrate oc_stat_exporter to gen_event
1 parent 2297bb2 commit a2b35d5

12 files changed

+171
-287
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[*]
2+
indent_style = space
3+
4+
[*.erl]
5+
indent_size = 4
6+
7+
[*.yml]
8+
indent_size = 2

src/oc_internal_timer.erl

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
-callback ping() -> ok.
44

5-
-export([start_link/1,
5+
-export([start_link/2,
66
init/1,
77
handle_call/3,
88
handle_cast/2,
@@ -12,27 +12,25 @@
1212
interval :: pos_integer(),
1313
module :: module()}).
1414

15-
start_link(Opts) ->
16-
gen_server:start_link(?MODULE, Opts, []).
15+
start_link(Interval, Module) ->
16+
gen_server:start_link(?MODULE, {Interval, Module}, []).
1717

18-
init(Opts) ->
19-
Interval = proplists:get_value(interval, Opts),
20-
Module = proplists:get_value(module, Opts),
21-
Ref = erlang:send_after(Interval, self(), ping),
18+
init({Interval, Module}) ->
19+
Ref = erlang:send_after(Interval, self(), ping),
2220

23-
{ok, #state{timer = Ref,
24-
interval = Interval,
25-
module = Module}}.
21+
{ok, #state{timer = Ref,
22+
interval = Interval,
23+
module = Module}}.
2624

2725
handle_call(_Msg, _From, State) -> {reply, ok, State}.
2826

2927
handle_cast(_Msg, State) -> {noreply, State}.
3028

3129
handle_info(ping, #state{timer = Ref, interval = Interval, module = Mod}) ->
32-
_ = erlang:cancel_timer(Ref),
33-
ok = Mod:ping(),
34-
NewRef = erlang:send_after(Interval, self(), ping),
30+
_ = erlang:cancel_timer(Ref),
31+
ok = Mod:ping(),
32+
NewRef = erlang:send_after(Interval, self(), ping),
3533

36-
{noreply, #state{timer = NewRef,
37-
interval = Interval,
38-
module = Mod}}.
34+
{noreply, #state{timer = NewRef,
35+
interval = Interval,
36+
module = Mod}}.

src/oc_reporter_pid.erl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
%%%-----------------------------------------------------------------------
1818
-module(oc_reporter_pid).
1919

20-
-behaviour(oc_reporter).
20+
-behaviour(gen_event).
2121

2222
-export([init/1,
23-
report/2]).
23+
handle_call/2,
24+
handle_event/2]).
2425

2526
init(Pid) -> Pid.
2627

27-
report(Spans, Pid) ->
28+
handle_call(_Msg, State) -> {ok, ok, State}.
29+
30+
handle_event(Spans, Pid) ->
2831
[Pid ! {span, Span} || Span <- Spans],
29-
ok.
32+
{ok, Pid}.

src/oc_reporter_stdout.erl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
%%%------------------------------------------------------------------------
2+
%% Copyright 2017, OpenCensus Authors
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+
116
-module(oc_reporter_stdout).
217

318
-behaviour(gen_event).

src/oc_stat.erl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
%%%-----------------------------------------------------------------------
1818
-module(oc_stat).
1919

20+
-behaviour(gen_server).
21+
2022
-export([record/2,
2123
record/3,
2224
export/0]).
2325

26+
-export([add_handler/1,
27+
add_handler/2,
28+
delete_handler/1]).
29+
2430
-export([start_link/0,
2531
init/1,
2632
handle_call/3,
@@ -75,6 +81,29 @@ record(Ctx, Measures) ->
7581
export() ->
7682
[oc_stat_view:export(View) || View <- oc_stat_view:all_subscribed_()].
7783

84+
%%--------------------------------------------------------------------
85+
%% @doc
86+
%% @equiv add_handler(Handler, []).
87+
%% @end
88+
%%--------------------------------------------------------------------
89+
add_handler(Handler) -> add_handler(Handler, []).
90+
91+
%%--------------------------------------------------------------------
92+
%% @doc
93+
%% Add new handler
94+
%% @end
95+
%%--------------------------------------------------------------------
96+
add_handler(Handler, Args) ->
97+
gen_event:add_handler(oc_stat_reporter, Handler, Args).
98+
99+
%%--------------------------------------------------------------------
100+
%% @doc
101+
%% Delete handler
102+
%% @end
103+
%%--------------------------------------------------------------------
104+
delete_handler(Handler) ->
105+
gen_event:delete_handler(oc_stat_reporter, Handler, []).
106+
78107
%% gen_server implementation
79108

80109
%% @private

src/oc_stat_exporter.erl

Lines changed: 0 additions & 160 deletions
This file was deleted.
Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%%%------------------------------------------------------------------------
2-
%% Copyright 2018, OpenCensus Authors
2+
%% Copyright 2017, OpenCensus Authors
33
%% Licensed under the Apache License, Version 2.0 (the "License");
44
%% you may not use this file except in compliance with the License.
55
%% You may obtain a copy of the License at
@@ -11,39 +11,34 @@
1111
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
%% See the License for the specific language governing permissions and
1313
%% limitations under the License.
14-
%%
15-
%% @doc Server with no logic, simply owns the span ets table.
14+
%%%------------------------------------------------------------------------
15+
16+
%% @doc
17+
%% Exporter exports the collected records as view data.
1618
%% @end
17-
%%%-------------------------------------------------------------------------
18-
-module(oc_server).
19+
-module(oc_stat_reporter).
1920

20-
-export([start_link/0]).
21+
-behaviour(oc_internal_timer).
2122

22-
-export([init/1,
23-
handle_call/3,
24-
handle_cast/2]).
23+
-export([start_link/1,
24+
ping/0]).
2525

2626
-include("opencensus.hrl").
27+
-include("oc_logger.hrl").
2728

28-
-record(state, {}).
29-
30-
start_link() ->
31-
maybe_init_ets(),
32-
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
29+
start_link(Handlers) ->
30+
case gen_event:start_link({local, ?MODULE}, []) of
31+
{ok, Pid} ->
32+
[gen_event:add_handler(Pid, Handler, Opts)
33+
|| {Handler, Opts} <- Handlers],
3334

34-
init([]) ->
35-
{ok, #state{}}.
36-
37-
handle_call(_, _From, State) ->
38-
{noreply, State}.
35+
{ok, Pid};
36+
Other -> Other
37+
end.
3938

40-
handle_cast(_, State) ->
41-
{noreply, State}.
39+
%% @private
40+
ping() ->
41+
Measurements = oc_stat:export(),
42+
gen_event:sync_notify(?MODULE, {stats, Measurements}),
4243

43-
maybe_init_ets() ->
44-
case ets:info(?SPAN_TAB, name) of
45-
undefined ->
46-
ets:new(?SPAN_TAB, [named_table, public, {write_concurrency, true}, {keypos, #span.span_id}]);
47-
_ ->
48-
ok
49-
end.
44+
ok.

0 commit comments

Comments
 (0)