From 68a78e0977fa389192eeddd3552f0fea25ac5358 Mon Sep 17 00:00:00 2001 From: Sergey Yelin Date: Tue, 20 Jul 2021 12:23:02 +0300 Subject: [PATCH] Format sources --- Makefile | 8 ++++++-- rebar.config | 17 ++++++++++++----- src/hay_cgroup_handler.erl | 2 +- src/hay_metrics.erl | 14 +++++++------- src/hay_metrics_handler.erl | 2 -- src/hay_metrics_publisher.erl | 2 -- src/hay_net_utils.erl | 2 +- src/hay_statsd_publisher.erl | 29 +++++++++++++++-------------- src/hay_vm_handler.erl | 32 +++++++++++++++++++------------- src/hay_woody_event_handler.erl | 19 +++++++++++-------- src/how_are_you.app.src | 4 +--- 11 files changed, 73 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index ac40ea8..c4e492b 100644 --- a/Makefile +++ b/Makefile @@ -24,5 +24,9 @@ distclean: run: $(REBAR) as run shell -#test: -# $(REBAR) ct + +check_format: + $(REBAR) fmt -c + +format: + $(REBAR) fmt -w diff --git a/rebar.config b/rebar.config index 1eabdaf..9c945cd 100644 --- a/rebar.config +++ b/rebar.config @@ -1,6 +1,5 @@ % Common project erlang options. {erl_opts, [ - % mandatory debug_info, warnings_as_errors, @@ -23,13 +22,12 @@ % bin_opt_info % no_auto_import % warn_missing_spec_all - ]}. % Common project dependencies. {deps, [ - {folsom , {git, "https://github.com/folsom-project/folsom.git" , {branch, "master"}}}, - {cg_mon , {git, "https://github.com/rbkmoney/cg_mon.git" , {branch, "master"}}} + {folsom, {git, "https://github.com/folsom-project/folsom.git", {branch, "master"}}}, + {cg_mon, {git, "https://github.com/rbkmoney/cg_mon.git", {branch, "master"}}} ]}. {xref_checks, [ @@ -53,7 +51,7 @@ ]} ]}. -{shell, [{apps,[how_are_you]}]}. +{shell, [{apps, [how_are_you]}]}. {profiles, [ {test, [ @@ -67,3 +65,12 @@ ]} ]} ]}. + +{plugins, [ + {erlfmt, "0.15.1"} +]}. + +{erlfmt, [ + {print_width, 120}, + {files, ["src/*.{app.src,erl}", "rebar.config"]} +]}. diff --git a/src/hay_cgroup_handler.erl b/src/hay_cgroup_handler.erl index 92d3754..a21b571 100644 --- a/src/hay_cgroup_handler.erl +++ b/src/hay_cgroup_handler.erl @@ -71,5 +71,5 @@ gather_metrics(Metrics, KeyPrefix) -> Data = [{Key, Fun()} || {Key, Fun} <- Metrics], [ hay_metrics:construct(gauge, [KeyPrefix, Key], Value) - || {Key, Value} <- Data, Value =/= undefined + || {Key, Value} <- Data, Value =/= undefined ]. diff --git a/src/hay_metrics.erl b/src/hay_metrics.erl index 2cbf389..2e3e94a 100644 --- a/src/hay_metrics.erl +++ b/src/hay_metrics.erl @@ -11,16 +11,16 @@ -export([fold/2]). -record(metric, { - type :: metric_type(), - key :: metric_key(), - value :: metric_value() + type :: metric_type(), + key :: metric_key(), + value :: metric_value() }). -opaque metric() :: #metric{}. -type metric_type() :: counter | gauge. -type metric_key() :: binary(). -type metric_raw_key() :: - atom() + atom() | integer() | binary() | maybe_improper_list(metric_raw_key(), metric_raw_key()). @@ -45,9 +45,9 @@ -spec construct(metric_type(), metric_raw_key(), metric_value()) -> metric(). construct(Type, Key, Val) -> #metric{ - type = Type, - key = construct_key(Key), - value = Val + type = Type, + key = construct_key(Key), + value = Val }. -spec type(metric()) -> metric_type(). diff --git a/src/hay_metrics_handler.erl b/src/hay_metrics_handler.erl index 17975cd..2fa8dc1 100644 --- a/src/hay_metrics_handler.erl +++ b/src/hay_metrics_handler.erl @@ -81,7 +81,6 @@ handle_info({timeout, TRef, update}, State0 = #state{timer = TRef}) -> #state{handler = Handler, handler_state = HandlerState} = State = restart_timer(State0), ok = push_metrics(Handler:gather_metrics(HandlerState)), {noreply, State, hibernate}; - handle_info(_Msg, State) -> {noreply, State, hibernate}. @@ -112,7 +111,6 @@ push_metrics(Metrics) -> restart_timer(State = #state{timer = undefined}) -> start_timer(State); - restart_timer(State = #state{timer = TimerRef}) -> _ = erlang:cancel_timer(TimerRef), start_timer(State#state{timer = undefined}). diff --git a/src/hay_metrics_publisher.erl b/src/hay_metrics_publisher.erl index b264e96..78a4f66 100644 --- a/src/hay_metrics_publisher.erl +++ b/src/hay_metrics_publisher.erl @@ -87,7 +87,6 @@ handle_info({timeout, TRef, publish}, State0 = #state{timer = TRef}) -> #state{handler = Handler, handler_state = HandlerState} = State = restart_timer(State0), {ok, NewHandlerState} = Handler:publish_metrics(fun hay_metrics:fold/2, HandlerState), {noreply, State#state{handler_state = NewHandlerState}, hibernate}; - handle_info(_Msg, State) -> {noreply, State, hibernate}. @@ -101,7 +100,6 @@ terminate(_Reason, _State) -> restart_timer(State = #state{timer = undefined}) -> start_timer(State); - restart_timer(State = #state{timer = TimerRef}) -> _ = erlang:cancel_timer(TimerRef), start_timer(State#state{timer = undefined}). diff --git a/src/hay_net_utils.erl b/src/hay_net_utils.erl index b78bb0a..087e012 100644 --- a/src/hay_net_utils.erl +++ b/src/hay_net_utils.erl @@ -84,4 +84,4 @@ detect_ip_family(Host) -> inet; {error, _Details} -> inet6 - end. \ No newline at end of file + end. diff --git a/src/hay_statsd_publisher.erl b/src/hay_statsd_publisher.erl index bacc876..ebd602c 100644 --- a/src/hay_statsd_publisher.erl +++ b/src/hay_statsd_publisher.erl @@ -19,14 +19,14 @@ socket_opts => [gen_udp:option()] }. -type socket_opt() :: - gen_udp:option() | - {ip, inet:socket_address()} | - {fd, non_neg_integer()} | - {ifaddr, inet:socket_address()} | - inet:address_family() | - {port, inet:port_number()} | - {netns, file:filename_all()} | - {bind_to_device, binary()}. + gen_udp:option() + | {ip, inet:socket_address()} + | {fd, non_neg_integer()} + | {ifaddr, inet:socket_address()} + | inet:address_family() + | {port, inet:port_number()} + | {netns, file:filename_all()} + | {bind_to_device, binary()}. -export_type([host/0]). -export_type([port_number/0]). @@ -70,12 +70,13 @@ get_interval(#state{interval = Interval}) -> -spec publish_metrics(metric_fold(), state()) -> {ok, state()} | {error, Reason :: term()}. publish_metrics(Fold, State0) -> State = resolve(ensure_socket_exists(State0)), - ok = case Fold(fun process_metrics/2, {State, [], 0}) of - {State, Packet, Size} when Size > 0 -> - ok = send_packet(Packet, State); - {State, [], 0} -> - ok - end, + ok = + case Fold(fun process_metrics/2, {State, [], 0}) of + {State, Packet, Size} when Size > 0 -> + ok = send_packet(Packet, State); + {State, [], 0} -> + ok + end, {ok, State}. %% Internals diff --git a/src/hay_vm_handler.erl b/src/hay_vm_handler.erl index f349c85..ac2515f 100644 --- a/src/hay_vm_handler.erl +++ b/src/hay_vm_handler.erl @@ -61,7 +61,7 @@ gather_vm_memory() -> % ] [ hay_metrics:construct(gauge, [<<"vm">>, <<"memory">>, Key], Val) - || {Key, Val} <- erlang:memory() + || {Key, Val} <- erlang:memory() ]. gather_io_stat() -> @@ -69,10 +69,9 @@ gather_io_stat() -> {Input, Output} = erlang:statistics(io), [ hay_metrics:construct(gauge, [<<"vm">>, <<"io">>, Key], Val) - || {Key, Val} <- [Input, Output] + || {Key, Val} <- [Input, Output] ]. - %% TODO do we need this? %% Probably not cuz it can be monitored through standart tools gather_system_memory() -> @@ -88,26 +87,27 @@ gather_system_memory() -> % ] [ hay_metrics:construct(gauge, [<<"system">>, <<"memory">>, Key], Val) - || {Key, Val} <- memsup:get_system_memory_data() + || {Key, Val} <- memsup:get_system_memory_data() ]. gather_load_stats() -> Names = gather_load_stat_keys(), [ hay_metrics:construct(gauge, [<<"vm">>, <<"load">>, Name], get_statistics_counter(Name)) - || Name <- Names + || Name <- Names ]. gather_vm_info() -> [ hay_metrics:construct(gauge, [<<"vm">>, <<"info">>, Name], erlang:system_info(Name)) - || Name <- get_vm_info_keys() + || Name <- get_vm_info_keys() ]. gather_load_stat_keys() -> [ total_run_queue_lengths, - total_run_queue_lengths_all, %% With dirty schedulers + %% With dirty schedulers + total_run_queue_lengths_all, total_active_tasks, context_switches, reductions, @@ -116,11 +116,16 @@ gather_load_stat_keys() -> ]. get_vm_info_keys() -> [ - atom_count, atom_limit, - ets_count, ets_limit, - port_count, port_limit, - process_count, process_limit, - schedulers, schedulers_online, + atom_count, + atom_limit, + ets_count, + ets_limit, + port_count, + port_limit, + process_count, + process_limit, + schedulers, + schedulers_online, dirty_cpu_schedulers_online, dirty_io_schedulers ]. @@ -144,4 +149,5 @@ gather_scheduler_utilization() -> ] end, [], - NewSwt). \ No newline at end of file + NewSwt + ). diff --git a/src/hay_woody_event_handler.erl b/src/hay_woody_event_handler.erl index f0ba774..fbeed17 100644 --- a/src/hay_woody_event_handler.erl +++ b/src/hay_woody_event_handler.erl @@ -12,15 +12,18 @@ -spec handle_event(Event, RpcId, Meta, Opts) -> ok when Event :: woody_event_handler:event(), RpcId :: woody:rpc_id() | undefined, - Meta :: woody_event_handler:event_meta(), - Opts :: woody:options(). + Meta :: woody_event_handler:event_meta(), + Opts :: woody:options(). handle_event( - 'invoke service handler', _RpcId, + 'invoke service handler', + _RpcId, #{function := Function, service_schema := {Handler, Service}}, - _Opts) -> - Key = [<<"woody">>, <<"rpc">>, <<"server">>, Handler, Service, Function], - how_are_you:metric_push( - how_are_you:metric_construct(counter, Key, 1)), + _Opts +) -> + Key = [<<"woody">>, <<"rpc">>, <<"server">>, Handler, Service, Function], + how_are_you:metric_push( + how_are_you:metric_construct(counter, Key, 1) + ), ok; handle_event(_Event, _RpcId, _Meta, _Opts) -> - ok. \ No newline at end of file + ok. diff --git a/src/how_are_you.app.src b/src/how_are_you.app.src index 18979c7..8910122 100644 --- a/src/how_are_you.app.src +++ b/src/how_are_you.app.src @@ -1,7 +1,5 @@ {application, how_are_you, [ - {description, - "Simple app for collecting and reporting metrics to somewere (statsd for ex.)" - }, + {description, "Simple app for collecting and reporting metrics to somewere (statsd for ex.)"}, {vsn, "0.1"}, {registered, []}, {mod, {how_are_you, []}},