diff --git a/apps/shortener/src/shortener.app.src b/apps/shortener/src/shortener.app.src index a61a022..3dc226e 100644 --- a/apps/shortener/src/shortener.app.src +++ b/apps/shortener/src/shortener.app.src @@ -17,7 +17,8 @@ swag_server, mg_proto, woody, - woody_user_identity + woody_user_identity, + erl_health ]}, {env, []}, {modules, []}, diff --git a/apps/shortener/src/shortener.erl b/apps/shortener/src/shortener.erl index 884239f..5515c88 100644 --- a/apps/shortener/src/shortener.erl +++ b/apps/shortener/src/shortener.erl @@ -38,14 +38,15 @@ start_link() -> -spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. init([]) -> + HealthCheckers = genlib_app:env(?MODULE, health_checkers, []), {ok, { {one_for_all, 0, 1}, % TODO - get_processor_childspecs(genlib_app:env(?MODULE, processor)) ++ - get_api_childspecs(genlib_app:env(?MODULE, api)) + get_processor_childspecs(genlib_app:env(?MODULE, processor), HealthCheckers) ++ + get_api_childspecs(genlib_app:env(?MODULE, api), HealthCheckers) }}. -get_processor_childspecs(Opts) -> +get_processor_childspecs(Opts, HealthCheckers) -> {ok, IP} = inet:parse_address(maps:get(ip, Opts, "::")), [woody_server:child_spec( ?MODULE, @@ -59,11 +60,13 @@ get_processor_childspecs(Opts) -> {mg_proto_state_processing_thrift, 'Processor'}, shortener_slug }} - ] + ], + additional_routes => [erl_health_handle:get_route(HealthCheckers)] } )]. -get_api_childspecs(Opts) -> +get_api_childspecs(Opts, HealthCheckers) -> AuthorizerSpec = shortener_authorizer_jwt:get_child_spec(maps:get(authorizer, Opts)), - SwaggerServerSpec = shortener_swagger_server:child_spec(shortener_handler, Opts), + HealthRoutes = [{'_', [erl_health_handle:get_route(HealthCheckers)]}], + SwaggerServerSpec = shortener_swagger_server:child_spec(shortener_handler, Opts, HealthRoutes), [AuthorizerSpec, SwaggerServerSpec]. diff --git a/apps/shortener/src/shortener_swagger_server.erl b/apps/shortener/src/shortener_swagger_server.erl index 9d9e43b..00726b2 100644 --- a/apps/shortener/src/shortener_swagger_server.erl +++ b/apps/shortener/src/shortener_swagger_server.erl @@ -1,17 +1,17 @@ -module(shortener_swagger_server). --export([child_spec/2]). +-export([child_spec/3]). -define(APP, shortener). -define(DEFAULT_ACCEPTORS_POOLSIZE, 10). -define(DEFAULT_IP_ADDR, "::"). -define(DEFAULT_PORT, 8080). --spec child_spec(module(), map()) -> supervisor:child_spec(). +-spec child_spec(module(), map(), cowboy_router:routes()) -> supervisor:child_spec(). -child_spec(LogicHandler, Opts) -> +child_spec(LogicHandler, Opts, AdditionalRoutes) -> {Transport, TransportOpts} = get_socket_transport(Opts), - CowboyOpts = get_cowboy_config(LogicHandler, Opts), + CowboyOpts = get_cowboy_config(LogicHandler, AdditionalRoutes, Opts), ranch:child_spec( ?MODULE, Transport, @@ -26,10 +26,11 @@ get_socket_transport(Opts) -> Acceptors = maps:get(acceptors, Opts, ?DEFAULT_ACCEPTORS_POOLSIZE), {ranch_tcp, [{ip, IP}, {port, Port}, {num_acceptors, Acceptors}]}. -get_cowboy_config(LogicHandler, Opts) -> +get_cowboy_config(LogicHandler, AdditionalRoutes, Opts) -> ShortUrlTemplate = maps:get(short_url_template, Opts), ShortUrlPath = maps:get(path, ShortUrlTemplate), Routes = squash_routes( + AdditionalRoutes ++ swag_server_router:get_paths(LogicHandler) ++ [{'_', [{genlib:to_list(ShortUrlPath) ++ ":shortenedUrlID", shortener_handler, #{}}]}] ), diff --git a/config/sys.config b/config/sys.config index fe7ed02..b5f4df6 100644 --- a/config/sys.config +++ b/config/sys.config @@ -57,7 +57,12 @@ }}, {service_clients, #{ automaton => #{url => <<"http://machinegun:8022/v1/automaton">>} - }} + }}, + {health_checkers, [ + {erl_health, disk , ["/", 99] }, + {erl_health, cg_memory, [99] }, + {erl_health, service , [<<"shortener">>]} + ]} ]} ]. diff --git a/rebar.config b/rebar.config index 561b1bd..8868a50 100644 --- a/rebar.config +++ b/rebar.config @@ -70,6 +70,11 @@ {git, "git@github.com:rbkmoney/machinegun_proto.git", {branch, "master"} } + }, + {erl_health, + {git, "https://github.com/rbkmoney/erlang-health.git", + {branch, master} + } } ]}. diff --git a/rebar.lock b/rebar.lock index f499e7a..bb7dd79 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,6 +1,10 @@ {"1.1.0", [{<<"base64url">>,{pkg,<<"base64url">>,<<"0.0.1">>},1}, {<<"certifi">>,{pkg,<<"certifi">>,<<"0.7.0">>},1}, + {<<"cg_mon">>, + {git,"https://github.com/rbkmoney/cg_mon.git", + {ref,"5a87a37694e42b6592d3b4164ae54e0e87e24e18"}}, + 1}, {<<"cowboy">>,{pkg,<<"cowboy">>,<<"1.0.4">>},0}, {<<"cowboy_access_log">>, {git,"git@github.com:rbkmoney/cowboy_access_log.git", @@ -11,6 +15,10 @@ {ref,"392f5804b63fff2bd0fda67671d5b2fbe0badd37"}}, 0}, {<<"cowlib">>,{pkg,<<"cowlib">>,<<"1.0.2">>},1}, + {<<"erl_health">>, + {git,"https://github.com/rbkmoney/erlang-health.git", + {ref,"0398b5c6cf276732cb5d2170f247f04207cb9ebb"}}, + 0}, {<<"genlib">>, {git,"https://github.com/rbkmoney/genlib.git", {ref,"7fc1ca1a57dbe2b8b837951095e314c32afd6c9a"}}, @@ -57,7 +65,7 @@ 1}, {<<"woody">>, {git,"git@github.com:rbkmoney/woody_erlang.git", - {ref,"ad1e91050c36d8de15f1c7d8dd8a2c682d2d158c"}}, + {ref,"d9362a5f8128c031300958da09c237ca27076cc5"}}, 0}, {<<"woody_user_identity">>, {git,"git@github.com:rbkmoney/woody_erlang_user_identity.git",