Skip to content

Commit

Permalink
update coding style and add review workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
RoadRunnr committed Apr 3, 2024
1 parent 13c525a commit 0a51577
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 127 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/review.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Review

on:
pull_request_target:
types:
- opened
- synchronize
branches:
- master

jobs:
code-style-review:
runs-on: ubuntu-22.04
env:
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
-
name: work around permission issue
run: |
git config --global --add safe.directory /__w/prometheus_diameter_collector/prometheus_diameter_collector
-
name: Check out repository
uses: actions/checkout@v4
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
-
name: install dependencies
run: |
sudo apt update
sudo apt install rebar3 emacs erlang-mode
-
name: format
run: rebar3 fmt
-
name: automated review
uses: googleapis/code-suggester@v4
with:
command: review
pull_number: ${{ github.event.pull_request.number }}
git_dir: '.'
-
name: check
run: git diff --quiet --exit-code
18 changes: 11 additions & 7 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
%%-*-Erlang-*-

%% == Erlang Compiler ==

{erl_opts, [debug_info]}.
Expand All @@ -9,16 +11,18 @@
%% == Xref ==

{xref_checks, [undefined_function_calls, undefined_functions,
locals_not_used, deprecated_function_calls,
deprecated_funcqtions]}.
locals_not_used, deprecated_function_calls,
deprecated_funcqtions]}.

%% == Plugins ==

{plugins, [
% @TODO: Folow https://github.com/markusn/coveralls-erl/pull/36 and use `coveralls` after release
{coveralls, {git, "https://github.com/RoadRunnr/coveralls-erl.git", {branch, "feature/git-info"}}},
rebar3_hex]
}.
{plugins,
[
%% @TODO: Folow https://github.com/markusn/coveralls-erl/pull/36 and use `coveralls` after release
{coveralls, {git, "https://github.com/RoadRunnr/coveralls-erl.git", {branch, "feature/git-info"}}},
rebar3_hex,
rebar3_fmt
]}.

%% == Cover ==
{cover_enabled, true}.
Expand Down
6 changes: 3 additions & 3 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
case {os:getenv("GITHUB_ACTIONS"), os:getenv("GITHUB_TOKEN")} of
{"true", Token} when is_list(Token) ->
CONFIG1 = [{coveralls_repo_token, Token},
{coveralls_service_job_id, os:getenv("GITHUB_RUN_ID")},
{coveralls_commit_sha, os:getenv("GITHUB_SHA")},
{coveralls_flag_name, os:getenv("COVERALLS_FLAG_NAME")} | CONFIG],
{coveralls_service_job_id, os:getenv("GITHUB_RUN_ID")},
{coveralls_commit_sha, os:getenv("GITHUB_SHA")},
{coveralls_flag_name, os:getenv("COVERALLS_FLAG_NAME")} | CONFIG],
case os:getenv("GITHUB_EVENT_NAME") =:= "pull_request"
andalso string:tokens(os:getenv("GITHUB_REF"), "/") of
[_, "pull", PRNO, _] ->
Expand Down
160 changes: 80 additions & 80 deletions src/prometheus_diameter_collector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
-include_lib("prometheus/include/prometheus.hrl").

-export([
deregister_cleanup/1,
collect_mf/2, collect_metrics/2,
gather/0,
gather_statistics/5
]).
deregister_cleanup/1,
collect_mf/2, collect_metrics/2,
gather/0,
gather_statistics/5
]).

-import(prometheus_model_helpers, [create_mf/5,
gauge_metric/2]).
gauge_metric/2]).

-define(METRIC_NAME_PREFIX, "diameter_").

-define(METRICS, [{applications, gauge, "Number of installed DIAMETER applications."},
{connections, gauge, "Number of connections to peers."},
{messages, gauge, "Number of requests."},
{errors, gauge, "Number of errors."}]).
{connections, gauge, "Number of connections to peers."},
{messages, gauge, "Number of requests."},
{errors, gauge, "Number of errors."}]).

%%====================================================================
%% Collector API
Expand All @@ -47,15 +47,15 @@ collect_mf(_Registry, Callback) ->

mf(Callback, {Name, Type, Help}, Stats) ->
Callback(create_mf(?METRIC_NAME(Name), Help, Type, ?MODULE,
{Type, fun(S) -> maps:get(Name, S, undefined) end, Stats})),
{Type, fun(S) -> maps:get(Name, S, undefined) end, Stats})),
ok.

collect_metrics(_, {Type, Fun, Stats}) ->
case Fun(Stats) of
M when is_map(M) ->
[metric(Type, Labels, Value) || {Labels, Value} <- maps:to_list(M)];
_ ->
undefined
M when is_map(M) ->
[metric(Type, Labels, Value) || {Labels, Value} <- maps:to_list(M)];
_ ->
undefined
end.

metric(gauge, Labels, Value) ->
Expand All @@ -76,76 +76,76 @@ gather() ->
Services = diameter:services(),
lists:foldl(
fun(SvcName, Stats) ->
Info = diameter:service_info(SvcName, [applications, peers]),
gather_service(SvcName, Info, Stats)
Info = diameter:service_info(SvcName, [applications, peers]),
gather_service(SvcName, Info, Stats)
end, #{}, Services).

gather_service(SvcName, Info, Stats) ->
lists:foldl(
fun({applications, Apps}, S0) ->
add([applications, [{svc, SvcName}]], length(Apps), S0);
({peers, Peers}, S0) ->
Apps = proplists:get_value(applications, Info, []),
lists:foldl(
fun({PeerName, Peer}, S1) ->
gather_peer(SvcName, PeerName, Peer, Apps, S1)
end, S0, Peers)
add([applications, [{svc, SvcName}]], length(Apps), S0);
({peers, Peers}, S0) ->
Apps = proplists:get_value(applications, Info, []),
lists:foldl(
fun({PeerName, Peer}, S1) ->
gather_peer(SvcName, PeerName, Peer, Apps, S1)
end, S0, Peers)
end, Stats, Info).

gather_peer(SvcName, Peer, Info, Apps, Stats) ->
lists:foldl(
fun({connections, C}, S0) ->
lists:foldl(
fun(X, S1) ->
gather_connection(SvcName, Peer, X, S1)
end, S0, C);
({statistics, S}, S0) ->
gather_statistics(SvcName, Peer, S, Apps, S0)
lists:foldl(
fun(X, S1) ->
gather_connection(SvcName, Peer, X, S1)
end, S0, C);
({statistics, S}, S0) ->
gather_statistics(SvcName, Peer, S, Apps, S0)
end, Stats, Info).

gather_connection(SvcName, Peer, Values, Stats) ->
{_, _, State} = proplists:get_value(watchdog, Values, {undefine, undefined, unknown}),
Type = case proplists:get_value(type, Values, unknown) of
accept -> responder;
connect -> initiator;
Other -> Other
end,
accept -> responder;
connect -> initiator;
Other -> Other
end,
Port = proplists:get_value(port, Values, []),
Connection = case proplists:get_value(module, Port, unknown) of
diameter_tcp -> tcp;
diameter_sctp -> sctp;
OtherTP -> OtherTP
end,
diameter_tcp -> tcp;
diameter_sctp -> sctp;
OtherTP -> OtherTP
end,
add([connections, [{svc, SvcName}, {peer, Peer}, {type, Type},
{state, State}, {protocol, Connection}]], 1, Stats).
{state, State}, {protocol, Connection}]], 1, Stats).

gather_statistics(SvcName, Peer, S, Apps, Stats) ->
lists:foldl(
fun({{{_, _, 1} = Msg, Direction}, Cnt}, S1) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)}]], Cnt, S1);
({{Msg, Direction, {'Result-Code', RC}}, Cnt}, S1) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)},
{rc, RC}]], Cnt, S1);
({{_, _, error}, Cnt}, S1) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)}]], Cnt, S1);
({{Msg, Direction, {'Result-Code', RC}}, Cnt}, S1) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)},
{rc, RC}]], Cnt, S1);
({{_, _, error}, Cnt}, S1) ->
add([errors,[{svc, SvcName}, {peer, Peer},
{error, unknown}]], Cnt, S1);
({{Msg, Direction, Result}, Cnt}, S1) when is_atom(Result) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)},
{rc, Result}]], Cnt, S1);
({Error, Cnt}, S1) when is_atom(Error) ->
add([errors,[{svc, SvcName}, {peer, Peer},
{error, Error}]], Cnt, S1);
(_, S1) ->
S1
({{Msg, Direction, Result}, Cnt}, S1) when is_atom(Result) ->
add([messages, [{svc, SvcName}, {peer, Peer},
{direction, Direction},
{type, msg_type(Msg)},
{msg, msg_name(Msg, Apps)},
{rc, Result}]], Cnt, S1);
({Error, Cnt}, S1) when is_atom(Error) ->
add([errors,[{svc, SvcName}, {peer, Peer},
{error, Error}]], Cnt, S1);
(_, S1) ->
S1
end, Stats, S).

msg_type({_, 0}) -> answer;
Expand All @@ -155,33 +155,33 @@ msg_type({_, _, 1}) -> request.

try_dict(Dict, {_, CmdCode, Rbit} = Cmd) ->
case code:is_loaded(Dict) of
{file, _} ->
try Dict:msg_name(CmdCode, Rbit =:= 1) of
'' -> Cmd;
Name when is_atom(Name) -> Name;
_ -> Cmd
catch
_:_ ->
Cmd
end;
_ ->
Cmd
{file, _} ->
try Dict:msg_name(CmdCode, Rbit =:= 1) of
'' -> Cmd;
Name when is_atom(Name) -> Name;
_ -> Cmd
catch
_:_ ->
Cmd
end;
_ ->
Cmd
end.

msg_name({0, _, _} = Cmd, _Apps) ->
case try_dict(diameter_gen_base_rfc6733, Cmd) of
Name when is_atom(Name) ->
Name;
_ ->
try_dict(diameter_gen_base_rfc3588, Cmd)
Name when is_atom(Name) ->
Name;
_ ->
try_dict(diameter_gen_base_rfc3588, Cmd)
end;
msg_name({ApplId, _, _} = Cmd, Apps) ->
case lists:filter(
fun(E) -> ApplId =:= proplists:get_value(id, E, -1) end, Apps) of
[App|_] ->
try_dict(proplists:get_value(dictionary, App, undefined), Cmd);
_ ->
Cmd
fun(E) -> ApplId =:= proplists:get_value(id, E, -1) end, Apps) of
[App|_] ->
try_dict(proplists:get_value(dictionary, App, undefined), Cmd);
_ ->
Cmd
end;
msg_name(_, _Apps) ->
unknown.
Loading

0 comments on commit 0a51577

Please sign in to comment.