From 09d5ecdec8ef299c687eaf2c81db4130a9c6d0d4 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Wed, 7 Aug 2019 17:37:53 +0300 Subject: [PATCH] MSPF-499: Enable healthcheck logging (#22) * Bump rbkmoney/build_utils@b9b18f3e * Bump test dep rbkmoney/machinegun@5b85e3c7 * Bump to rbkmoney/erlang-health@d8fb5f2c --- apps/shortener/src/shortener.erl | 27 +++++++++++----- apps/shortener/src/shortener_slug.erl | 9 ++++-- .../test/shortener_general_SUITE.erl | 28 ++++++++++++++-- build_utils | 2 +- docker-compose.sh | 2 +- rebar.config | 9 ++++-- rebar.lock | 32 ++++++++++++------- test/machinegun/config.yaml | 3 ++ 8 files changed, 83 insertions(+), 29 deletions(-) diff --git a/apps/shortener/src/shortener.erl b/apps/shortener/src/shortener.erl index 66c83cf..12ae70b 100644 --- a/apps/shortener/src/shortener.erl +++ b/apps/shortener/src/shortener.erl @@ -38,15 +38,26 @@ start_link() -> -spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. init([]) -> - HealthCheckers = genlib_app:env(?MODULE, health_checkers, []), + HealthRoutes = get_health_routes(genlib_app:env(?MODULE, health_check, #{})), {ok, { {one_for_all, 0, 1}, % TODO - get_processor_childspecs(genlib_app:env(?MODULE, processor), HealthCheckers) ++ - get_api_childspecs(genlib_app:env(?MODULE, api), HealthCheckers) + get_processor_childspecs(genlib_app:env(?MODULE, processor), HealthRoutes) ++ + get_api_childspecs(genlib_app:env(?MODULE, api), HealthRoutes) }}. -get_processor_childspecs(Opts, HealthCheckers) -> +get_health_routes(Check) -> + [erl_health_handle:get_route(enable_health_logging(Check))]. + +enable_health_logging(Check = #{}) -> + maps:map( + fun (_, V = {_, _, _}) -> + #{runner => V, event_handler => {erl_health_event_handler, []}} + end, + Check + ). + +get_processor_childspecs(Opts, HealthRoutes) -> {ok, IP} = inet:parse_address(maps:get(ip, Opts, "::")), [woody_server:child_spec( ?MODULE, @@ -62,12 +73,12 @@ get_processor_childspecs(Opts, HealthCheckers) -> shortener_slug }} ], - additional_routes => [erl_health_handle:get_route(HealthCheckers)] + additional_routes => HealthRoutes } )]. -get_api_childspecs(Opts, HealthCheckers) -> +get_api_childspecs(Opts, HealthRoutes) -> AuthorizerSpec = shortener_authorizer_jwt:get_child_spec(maps:get(authorizer, Opts)), - HealthRoutes = [{'_', [erl_health_handle:get_route(HealthCheckers)]}], - SwaggerServerSpec = shortener_swagger_server:child_spec(shortener_handler, Opts, HealthRoutes), + HealthRouter = [{'_', HealthRoutes}], + SwaggerServerSpec = shortener_swagger_server:child_spec(shortener_handler, Opts, HealthRouter), [AuthorizerSpec, SwaggerServerSpec]. diff --git a/apps/shortener/src/shortener_slug.erl b/apps/shortener/src/shortener_slug.erl index f63d926..4f267c2 100644 --- a/apps/shortener/src/shortener_slug.erl +++ b/apps/shortener/src/shortener_slug.erl @@ -235,15 +235,18 @@ handle_signal_result(Result, Machine) -> construct_machine_change(Events, #mg_stateproc_Machine{aux_state = AuxState}) -> #mg_stateproc_MachineStateChange{ - events = [marshal(event, E) || E <- Events], + events = [construct_content(marshal(event, E)) || E <- Events], aux_state = construct_aux_state(AuxState) }. construct_aux_state(undefined) -> - {nl, #mg_msgpack_Nil{}}; + construct_content({nl, #mg_msgpack_Nil{}}); construct_aux_state(AuxState) -> AuxState. +construct_content(Data) -> + #mg_stateproc_Content{data = Data}. + construct_complex_action(Actions) -> lists:foldl(fun apply_action/2, #mg_stateproc_ComplexAction{}, Actions). @@ -253,7 +256,7 @@ apply_action(remove, CA) -> CA#mg_stateproc_ComplexAction{remove = #mg_stateproc_RemoveAction{}}. unmarshal_history(H) -> - [unmarshal(event, E) || #mg_stateproc_Event{event_payload = E} <- H]. + [unmarshal(event, E) || #mg_stateproc_Event{data = E} <- H]. %% diff --git a/apps/shortener/test/shortener_general_SUITE.erl b/apps/shortener/test/shortener_general_SUITE.erl index add2918..6b4ed48 100644 --- a/apps/shortener/test/shortener_general_SUITE.erl +++ b/apps/shortener/test/shortener_general_SUITE.erl @@ -20,6 +20,8 @@ -export([url_expired/1]). -export([always_unique_url/1]). +-export([health_check_passing/1]). + -export([woody_timeout_test/1]). -export([unsupported_cors_method/1]). @@ -39,7 +41,8 @@ all() -> [ {group, general}, {group, cors}, - woody_timeout_test + woody_timeout_test, + health_check_passing ]. -spec groups() -> [{atom(), list(), [test_case_name()]}]. @@ -94,8 +97,7 @@ init_per_group(_Group, C) -> genlib_app:start_application_with(shortener, get_app_config( ?config(port, C), ?config(netloc, C), - get_keysource("keys/local/private.pem", C), - <<"http://machinegun:8022/v1/automaton">> + get_keysource("keys/local/private.pem", C) )), [ {shortener_app, ShortenerApp} @@ -302,6 +304,20 @@ woody_timeout_test(C) -> true = (Time >= 3000000), genlib_app:stop_unload_applications(Apps). +%% +-spec health_check_passing(config()) -> _. + +health_check_passing(C) -> + Apps = genlib_app:start_application_with(shortener, get_app_config( + ?config(port, C), + ?config(netloc, C), + get_keysource("keys/local/private.pem", C) + )), + Path = ?config(api_endpoint, C) ++ "/health", + {ok, 200, _, Payload} = hackney:request(get, Path, [], <<>>, [with_body]), + #{<<"service">> := <<"shortener">>} = jsx:decode(Payload, [return_maps]), + genlib_app:stop_unload_applications(Apps). + %% set_api_auth_token(Name, Permissions, C) -> UserID = genlib:to_binary(Name), @@ -363,6 +379,9 @@ format_ts(Ts) -> %% +get_app_config(Port, Netloc, PemFile) -> + get_app_config(Port, Netloc, PemFile, <<"http://machinegun:8022/v1/automaton">>). + get_app_config(Port, Netloc, PemFile, AutomatonUrl) -> [ {space_size , 8}, @@ -391,6 +410,9 @@ get_app_config(Port, Netloc, PemFile, AutomatonUrl) -> ip => "::", port => 8022 }}, + {health_check, #{ + service => {erl_health, service, [<<"shortener">>]} + }}, {service_clients, #{ automaton => #{ url => AutomatonUrl, diff --git a/build_utils b/build_utils index ea4aa04..b9b18f3 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit ea4aa042f482551d624fd49a570d28488f479e93 +Subproject commit b9b18f3ee375aa5fd105daf57189ac242c40f572 diff --git a/docker-compose.sh b/docker-compose.sh index 2837e2c..04598cd 100755 --- a/docker-compose.sh +++ b/docker-compose.sh @@ -15,7 +15,7 @@ services: condition: service_healthy machinegun: - image: dr.rbkmoney.com/rbkmoney/machinegun:769bd38b57bb3230fded6acd76b2c5a672745e9a + image: dr2.rbkmoney.com/rbkmoney/machinegun:5b85e3c73041e5cbcfcc35c465cf14214163389b command: /opt/machinegun/bin/machinegun foreground volumes: - ./test/machinegun/config.yaml:/opt/machinegun/etc/config.yaml diff --git a/rebar.config b/rebar.config index c1f4953..f09820c 100644 --- a/rebar.config +++ b/rebar.config @@ -74,6 +74,11 @@ {git, "https://github.com/rbkmoney/cowboy_cors.git", {branch, "master"} } + }, + {cowboy_cors, + {git, "https://github.com/rbkmoney/how_are_you.git", + {branch, "master"} + } } ]}. @@ -90,8 +95,8 @@ {runtime_tools , load}, % debugger {tools , load}, % profiler {logger_logstash_formatter, load}, % log formatter - sasl, - shortener + shortener, + how_are_you ]}, {sys_config, "./config/sys.config"}, {vm_args, "./config/vm.args"}, diff --git a/rebar.lock b/rebar.lock index fb3dfca..e90c6ba 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,5 +1,6 @@ {"1.1.0", [{<<"base64url">>,{pkg,<<"base64url">>,<<"0.0.1">>},1}, + {<<"bear">>,{pkg,<<"bear">>,<<"0.8.7">>},3}, {<<"cache">>,{pkg,<<"cache">>,<<"2.2.0">>},1}, {<<"certifi">>,{pkg,<<"certifi">>,<<"2.3.1">>},1}, {<<"cg_mon">>, @@ -9,7 +10,7 @@ {<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.5.0">>},0}, {<<"cowboy_access_log">>, {git,"git@github.com:rbkmoney/cowboy_access_log.git", - {ref,"bf313a82a5f18924aae1f13681676678ad0609b6"}}, + {ref,"9d755d7fc2209b18932c6609fbb1f95ae9d2404f"}}, 0}, {<<"cowboy_cors">>, {git,"https://github.com/rbkmoney/cowboy_cors.git", @@ -18,11 +19,15 @@ {<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.6.0">>},1}, {<<"erl_health">>, {git,"https://github.com/rbkmoney/erlang-health.git", - {ref,"2575c7b63d82a92de54d2d27e504413675e64811"}}, + {ref,"d8fb5f2c2a14f6180125fec414d394d89015fe6b"}}, 0}, + {<<"folsom">>, + {git,"git@github.com:folsom-project/folsom.git", + {ref,"9309bad9ffadeebbefe97521577c7480c7cfcd8a"}}, + 2}, {<<"genlib">>, {git,"https://github.com/rbkmoney/genlib.git", - {ref,"f805a11f6e73faffb05656c5192fbe199df36f27"}}, + {ref,"901f5d7232e21cddc80c2864bf0c918e862b861a"}}, 0}, {<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1}, {<<"gproc">>,{pkg,<<"gproc">>,<<"0.8.0">>},1}, @@ -30,7 +35,11 @@ {git,"https://github.com/ninenines/gun.git", {ref,"e7dd9f227e46979d8073e71c683395a809b78cb4"}}, 1}, - {<<"hackney">>,{pkg,<<"hackney">>,<<"1.15.0">>},0}, + {<<"hackney">>,{pkg,<<"hackney">>,<<"1.13.0">>},0}, + {<<"how_are_you">>, + {git,"https://github.com/rbkmoney/how_are_you.git", + {ref,"2bb46054e16aaba9357747cc72b7c42e1897a56d"}}, + 1}, {<<"idna">>,{pkg,<<"idna">>,<<"5.1.2">>},1}, {<<"jesse">>, {git,"https://github.com/rbkmoney/jesse.git", @@ -41,23 +50,23 @@ {<<"lager">>,{pkg,<<"lager">>,<<"3.6.4">>},0}, {<<"logger_logstash_formatter">>, {git,"git@github.com:rbkmoney/logger_logstash_formatter.git", - {ref,"54c371215e3d73b2a868bc6375e523f95e826fe3"}}, + {ref,"b53af86014ba5748d8704cdb23867a590e8a7ae0"}}, 0}, {<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},1}, {<<"mg_proto">>, {git,"git@github.com:rbkmoney/machinegun_proto.git", - {ref,"5c07c579014f9900357f7a72f9d10a03008b9da1"}}, + {ref,"ebae56fe2b3e79e4eb34afc8cb55c9012ae989f8"}}, 0}, {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.0.2">>},1}, {<<"parse_trans">>, - {git,"https://github.com/uwiger/parse_trans.git", - {ref,"76abb347c3c1d00fb0ccf9e4b43e22b3d2288484"}}, + {git,"https://github.com/rbkmoney/parse_trans.git", + {ref,"5ee45f5bfa6c04329bea3281977b774f04c89f11"}}, 0}, {<<"ranch">>,{pkg,<<"ranch">>,<<"1.6.2">>},1}, {<<"rfc3339">>,{pkg,<<"rfc3339">>,<<"0.2.2">>},0}, {<<"scoper">>, {git,"git@github.com:rbkmoney/scoper.git", - {ref,"e03318fd1feea0e2bb0ba5c634bb38b18aa81efa"}}, + {ref,"95643f40dd628c77f33f12be96cf1c39dccc9683"}}, 0}, {<<"snowflake">>, {git,"https://github.com/rbkmoney/snowflake.git", @@ -71,7 +80,7 @@ {<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.3.1">>},2}, {<<"woody">>, {git,"git@github.com:rbkmoney/woody_erlang.git", - {ref,"5ee89dd0b2d52ff955a4107a8d9dc0f8fdd365a0"}}, + {ref,"649a8aba300d5ce3ada2aacf4c55e44011169dce"}}, 0}, {<<"woody_user_identity">>, {git,"git@github.com:rbkmoney/woody_erlang_user_identity.git", @@ -80,13 +89,14 @@ [ {pkg_hash,[ {<<"base64url">>, <<"36A90125F5948E3AFD7BE97662A1504B934DD5DAC78451CA6E9ABF85A10286BE">>}, + {<<"bear">>, <<"16264309AE5D005D03718A5C82641FCC259C9E8F09ADEB6FD79CA4271168656F">>}, {<<"cache">>, <<"3C11DBF4CD8FCD5787C95A5FB2A04038E3729CFCA0386016EEA8C953AB48A5AB">>}, {<<"certifi">>, <<"D0F424232390BF47D82DA8478022301C561CF6445B5B5FB6A84D49A9E76D2639">>}, {<<"cowboy">>, <<"4EF3AE066EE10FE01EA3272EDC8F024347A0D3EB95F6FBB9AED556DACBFC1337">>}, {<<"cowlib">>, <<"8AA629F81A0FC189F261DC98A42243FA842625FEEA3C7EC56C48F4CCDB55490F">>}, {<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>}, {<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>}, - {<<"hackney">>, <<"287A5D2304D516F63E56C469511C42B016423BCB167E61B611F6BAD47E3CA60E">>}, + {<<"hackney">>, <<"24EDC8CD2B28E1C652593833862435C80661834F6C9344E84B6A2255E7AEEF03">>}, {<<"idna">>, <<"E21CB58A09F0228A9E0B95EAA1217F1BCFC31A1AAA6E1FDF2F53A33F7DBD9494">>}, {<<"jose">>, <<"9DC5A14AB62DB4E41677FCC97993752562FB57AD0B8BA062589682EDD3ACB91F">>}, {<<"jsx">>, <<"7ACC7D785B5ABE8A6E9ADBDE926A24E481F29956DD8B4DF49E3E4E7BCC92A018">>}, diff --git a/test/machinegun/config.yaml b/test/machinegun/config.yaml index fe6a632..fa44f8a 100644 --- a/test/machinegun/config.yaml +++ b/test/machinegun/config.yaml @@ -1,3 +1,6 @@ +service_name: machinegun +snowflake_machine_id: 42 + namespaces: url-shortener: processor: