Skip to content

Commit

Permalink
chore(metrics): extend hb_metrics to abstract prometheus metrics
Browse files Browse the repository at this point in the history
Create hb_metrics process that abstracts metrics subsystem implementation
and exposes the API for registering and interacting with metrics.
  • Loading branch information
oltarasenko committed Dec 5, 2024
1 parent 8649c32 commit 173838a
Show file tree
Hide file tree
Showing 7 changed files with 774 additions and 148 deletions.
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
<img src="https://arweave.net/dOpRkKrNNQ4HHebxZlPCo0BWfrjwJ-CEBQs2EPgrwbg" />

Soon.

<!-- toc -->

- [Development](#development)

<!-- tocstop -->

## Development

Environment Setup TBD

This repo uses `npm` and `node` to install and execute top level, repo-wide, tooling. So first, you should run `npm i`
at the root to install those dependencies. This will set up git hooks that will help ensure you're following some of the
guidelines outlined in this document:

- All Erlang files are formatted using [`erlfmt`](https://github.com/WhatsApp/erlfmt)
- All Commit Messages follow our [Conventional Commits Config](https://www.conventionalcommits.org/en/v1.0.0/)
- All Markdown files should contain a Table of Contents

Once you've install the top level tooling, each of these conventions are enforced, via git a commit hook, automatically.
48 changes: 48 additions & 0 deletions config/sys.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[
{hb, [
{metrics, [
#{
type => histogram,
name => "ao_cache_write_time",
description => "Histogram of write operation duration",
buckets => [5, 10, 100, 500, 1000]
},
#{
type => counter,
name => "ao_cache_read",
description => "Counts ao cache read operation"
},
#{
type => counter,
name => "ao_cache_write",
description => "Counts ao cache write operation"
},
#{
type => counter,
name => "ao_cache_list",
description => "Counts ao cache list operation"
},

#{
type => counter,
name => "ao_cache_resolve",
description => "Counts ao cache resolve operation"
},

#{
type => counter,
name => "ao_cache_type",
description => "Counts ao cache type operation"
},

#{
type => counter,
name => "ao_make_link",
description => "Counts ao_cache ao_make_link operation"
}



]}
]}
].
6 changes: 4 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
]}.

{shell, [
{apps, [hb]}
{apps, [hb]},
{config, "./config/sys.config"}
]}.

{eunit, [
{apps, [hb]}
{apps, [hb]},
{config, "./config/sys.config"}
]}.

{eunit_opts, [verbose]}.
Expand Down
100 changes: 50 additions & 50 deletions src/dev_scheduler_registry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,80 @@
%%% only SU processes are supported.

start() ->
pg:start(pg),
ok.
pg:start(pg),
ok.

get_wallet() ->
% TODO: We might want to use a different wallet per SU later.
hb:wallet().
% TODO: We might want to use a different wallet per SU later.
hb:wallet().

find(ProcID) -> find(ProcID, false).
find(ProcID, GenIfNotHosted) when is_binary(ProcID) ->
find(binary_to_list(ProcID), GenIfNotHosted);
find(binary_to_list(ProcID), GenIfNotHosted);
find(ProcID, GenIfNotHosted) ->
case pg:get_local_members({su, ProcID}) of
[] ->
maybe_new_proc(ProcID, GenIfNotHosted);
[Pid] ->
case is_process_alive(Pid) of
true -> Pid;
false ->
maybe_new_proc(ProcID, GenIfNotHosted)
end
end.
case pg:get_local_members({su, ProcID}) of
[] ->
maybe_new_proc(ProcID, GenIfNotHosted);
[Pid] ->
case is_process_alive(Pid) of
true -> Pid;
false -> maybe_new_proc(ProcID, GenIfNotHosted)
end
end.

get_processes() ->
[ ProcID || {su, ProcID} <- pg:which_groups() ].
[ProcID || {su, ProcID} <- pg:which_groups()].

maybe_new_proc(_ProcID, false) -> not_found;
maybe_new_proc(_ProcID, false) ->
not_found;
maybe_new_proc(ProcID, GenIfNotHosted) when is_binary(ProcID) ->
maybe_new_proc(binary_to_list(ProcID), GenIfNotHosted);
maybe_new_proc(ProcID, _) ->
maybe_new_proc(binary_to_list(ProcID), GenIfNotHosted);
maybe_new_proc(ProcID, _) ->
?event({starting_su_for, ProcID}),
Pid = dev_scheduler_server:start(ProcID, get_wallet()),
try
pg:join({su, ProcID}, Pid),
Pid
catch
error:badarg ->
{error, registration_failed}
end.
Pid = dev_scheduler_server:start(ProcID, get_wallet()),
try
pg:join({su, ProcID}, Pid),
Pid
catch
error:badarg ->
{error, registration_failed}
end.

%%% Tests

setup() ->
application:ensure_all_started(hb),
start().
application:ensure_all_started(hb),
start().

-define(TEST_PROC_ID1, binary_to_list(<<0:256>>)).
-define(TEST_PROC_ID2, binary_to_list(<<1:256>>)).

find_non_existent_process_test() ->
setup(),
?assertEqual(not_found, ?MODULE:find(?TEST_PROC_ID1)).
setup(),
?assertEqual(not_found, ?MODULE:find(?TEST_PROC_ID1)).

create_and_find_process_test() ->
setup(),
Pid1 = ?MODULE:find(?TEST_PROC_ID1, true),
?assert(is_pid(Pid1)),
?assertEqual(Pid1, ?MODULE:find(?TEST_PROC_ID1)).
setup(),
Pid1 = ?MODULE:find(?TEST_PROC_ID1, true),
?assert(is_pid(Pid1)),
?assertEqual(Pid1, ?MODULE:find(?TEST_PROC_ID1)).

create_multiple_processes_test() ->
setup(),
Pid1 = ?MODULE:find(?TEST_PROC_ID1, true),
Pid2 = ?MODULE:find(?TEST_PROC_ID2, true),
?assert(is_pid(Pid1)),
?assert(is_pid(Pid2)),
?assertNotEqual(Pid1, Pid2),
?assertEqual(Pid1, ?MODULE:find(?TEST_PROC_ID1)),
?assertEqual(Pid2, ?MODULE:find(?TEST_PROC_ID2)).
setup(),
Pid1 = ?MODULE:find(?TEST_PROC_ID1, true),
Pid2 = ?MODULE:find(?TEST_PROC_ID2, true),
?assert(is_pid(Pid1)),
?assert(is_pid(Pid2)),
?assertNotEqual(Pid1, Pid2),
?assertEqual(Pid1, ?MODULE:find(?TEST_PROC_ID1)),
?assertEqual(Pid2, ?MODULE:find(?TEST_PROC_ID2)).

get_all_processes_test() ->
setup(),
?MODULE:find(?TEST_PROC_ID1, true),
?MODULE:find(?TEST_PROC_ID2, true),
Processes = ?MODULE:get_processes(),
?assertEqual(2, length(Processes)),
setup(),
?MODULE:find(?TEST_PROC_ID1, true),
?MODULE:find(?TEST_PROC_ID2, true),
Processes = ?MODULE:get_processes(),
?assertEqual(2, length(Processes)),
?event({processes, Processes}),
?assert(lists:member(?TEST_PROC_ID1, Processes)),
?assert(lists:member(?TEST_PROC_ID2, Processes)).
?assert(lists:member(?TEST_PROC_ID1, Processes)),
?assert(lists:member(?TEST_PROC_ID2, Processes)).
Loading

0 comments on commit 173838a

Please sign in to comment.