1 |
|
-module(mongoose_instrument_registry). |
2 |
|
|
3 |
|
-export([start/0, attach/3, detach/2, lookup/2]). |
4 |
|
|
5 |
|
-spec start() -> ok. |
6 |
|
start() -> |
7 |
104 |
ets:new(?MODULE, [named_table, public, {read_concurrency, true}]), |
8 |
104 |
ok. |
9 |
|
|
10 |
|
-spec attach(mongoose_instrument:event_name(), mongoose_instrument:labels(), |
11 |
|
mongoose_instrument:handlers()) -> ok | {error, already_attached}. |
12 |
|
attach(Event, Labels, Val) -> |
13 |
:-( |
case ets:insert_new(?MODULE, {{Event, Labels}, Val}) of |
14 |
:-( |
true -> ok; |
15 |
:-( |
false -> {error, already_attached} |
16 |
|
end. |
17 |
|
|
18 |
|
-spec detach(mongoose_instrument:event_name(), mongoose_instrument:labels()) -> ok. |
19 |
|
detach(Event, Labels) -> |
20 |
:-( |
ets:delete(?MODULE, {Event, Labels}), |
21 |
:-( |
ok. |
22 |
|
|
23 |
|
-spec lookup(mongoose_instrument:event_name(), mongoose_instrument:labels()) -> |
24 |
|
{ok, mongoose_instrument:handlers()} | {error, not_found}. |
25 |
|
lookup(Event, Labels) -> |
26 |
:-( |
case ets:lookup(?MODULE, {Event, Labels}) of |
27 |
:-( |
[] -> {error, not_found}; |
28 |
:-( |
[{_, Val}] -> {ok, Val} |
29 |
|
end. |