Skip to content

Commit ec2ae35

Browse files
committed
Use persistent_term for storing ETS table's TID
This is preliminary step towards implementing #142. This allows using unnamed table and provide a way to swap implementation for more performant one.
1 parent 614bfb9 commit ec2ae35

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/telemetry_handler_table.erl

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,72 @@ insert(HandlerId, EventNames, Function, Config) ->
4242
delete(HandlerId) ->
4343
gen_server:call(?MODULE, {delete, HandlerId}).
4444

45+
impl_get() -> persistent_term:get(telemetry).
46+
4547
-spec list_for_event(telemetry:event_name()) -> [#handler{}].
4648
list_for_event(EventName) ->
47-
try
48-
ets:lookup(?MODULE, EventName)
49-
catch
50-
error:badarg ->
49+
case impl_get() of
50+
{ets, TID} ->
51+
try
52+
ets:lookup(TID, EventName)
53+
catch
54+
error:badarg ->
55+
persistent_term:erase(telemetry),
56+
?LOG_WARNING("Failed to lookup telemetry handlers. "
57+
"Ensure the telemetry application has been started. ", []),
58+
[]
59+
end;
60+
_ ->
5161
?LOG_WARNING("Failed to lookup telemetry handlers. "
5262
"Ensure the telemetry application has been started. ", []),
5363
[]
5464
end.
5565

5666
-spec list_by_prefix(telemetry:event_prefix()) -> [#handler{}].
5767
list_by_prefix(EventPrefix) ->
58-
Pattern = match_pattern_for_prefix(EventPrefix),
59-
ets:match_object(?MODULE, Pattern).
68+
case impl_get() of
69+
{ets, TID} ->
70+
Pattern = match_pattern_for_prefix(EventPrefix),
71+
ets:match_object(TID, Pattern);
72+
_ ->
73+
?LOG_WARNING("Failed to lookup telemetry handlers. "
74+
"Ensure the telemetry application has been started. ", []),
75+
[]
76+
end.
6077

6178
init([]) ->
62-
_ = create_table(),
79+
TID = create_table(),
80+
81+
persistent_term:put(telemetry, {ets, TID}),
82+
6383
{ok, []}.
6484

6585
handle_call({insert, HandlerId, EventNames, Function, Config}, _From, State) ->
66-
case ets:match(?MODULE, #handler{id=HandlerId,
67-
_='_'}) of
68-
[] ->
69-
Objects = [#handler{id=HandlerId,
70-
event_name=EventName,
71-
function=Function,
72-
config=Config} || EventName <- EventNames],
73-
ets:insert(?MODULE, Objects),
74-
{reply, ok, State};
75-
_ ->
76-
{reply, {error, already_exists}, State}
86+
case impl_get() of
87+
{ets, TID} ->
88+
case ets:match(TID, #handler{id=HandlerId,
89+
_='_'}) of
90+
[] ->
91+
Objects = [#handler{id=HandlerId,
92+
event_name=EventName,
93+
function=Function,
94+
config=Config} || EventName <- EventNames],
95+
ets:insert(TID, Objects),
96+
{reply, ok, State};
97+
_ ->
98+
{reply, {error, already_exists}, State}
99+
end
77100
end;
78101
handle_call({delete, HandlerId}, _From, State) ->
79-
case ets:select_delete(?MODULE, [{#handler{id=HandlerId,
80-
_='_'}, [], [true]}]) of
81-
0 ->
82-
{reply, {error, not_found}, State};
83-
_ ->
84-
{reply, ok, State}
102+
case impl_get() of
103+
{ets, TID} ->
104+
case ets:select_delete(TID, [{#handler{id=HandlerId,
105+
_='_'}, [], [true]}]) of
106+
0 ->
107+
{reply, {error, not_found}, State};
108+
_ ->
109+
{reply, ok, State}
110+
end
85111
end.
86112

87113
handle_cast(_Msg, State) ->
@@ -94,6 +120,7 @@ code_change(_, State, _) ->
94120
{ok, State}.
95121

96122
terminate(_Reason, _State) ->
123+
persistent_term:erase(telemetry),
97124
ok.
98125

99126
%%

0 commit comments

Comments
 (0)