diff --git a/config/sys.config b/config/sys.config index 01d01d2..8f9ac29 100644 --- a/config/sys.config +++ b/config/sys.config @@ -1,10 +1,35 @@ [ {dmt_client, [ - {transport_opts, #{ + {ip, "::"}, + {port, 8022}, + %% 1 sec above cowboy's request_timeout + {shutdown_timeout, 7000}, + {protocol_opts, #{ + % Bump keepalive timeout up to a minute + request_timeout => 6000, + % Should be greater than any other timeouts + idle_timeout => infinity + }}, + {server_transport_opts, #{ + % Keeping the default value + max_connections => 8096 + }}, + {client_transport_opts, #{ recv_timeout => 60000, connect_timeout => 1000 }}, - {woody_event_handlers, [ + {server_woody_event_handlers, [ + {scoper_woody_event_handler, #{ + event_handler_opts => #{ + formatter_opts => #{ + max_length => 1000, + max_depth => 10, + max_pritable_string_length => 40 + } + } + }} + ]}, + {client_woody_event_handlers, [ {scoper_woody_event_handler, #{ event_handler_opts => #{ formatter_opts => #{ @@ -23,6 +48,10 @@ {service_urls, #{ 'Repository' => <<"dominant:8022/v1/domain/repository">>, 'RepositoryClient' => <<"dominant:8022/v1/domain/repository_client">> + }}, + {health_check, #{ + dmt_client => {dmt_client, health_check, []}, + service => {erl_health, service, [<<"dmt_client">>]} }} ]} ]. diff --git a/rebar.config b/rebar.config index d34d426..4e7724a 100644 --- a/rebar.config +++ b/rebar.config @@ -27,10 +27,11 @@ %% Common project dependencies. {deps, [ - {genlib, {git, "https://github.com/rbkmoney/genlib.git", {branch, "master"}}}, - {woody , {git, "git@github.com:rbkmoney/woody_erlang.git", {branch, "master"}}}, - {dmt_core, {git, "git@github.com:rbkmoney/dmt_core.git", {branch, "master"}}}, - {damsel, {git, "git@github.com:rbkmoney/damsel.git", {branch, "release/erlang/master"}}} + {genlib, {git, "https://github.com/rbkmoney/genlib.git", {branch, "master"}}}, + {woody , {git, "git@github.com:rbkmoney/woody_erlang.git", {branch, "master"}}}, + {dmt_core, {git, "git@github.com:rbkmoney/dmt_core.git", {branch, "master"}}}, + {erl_health, {git, "https://github.com/rbkmoney/erlang-health.git", {branch, "master"}}}, + {damsel, {git, "git@github.com:rbkmoney/damsel.git", {branch, "release/erlang/master"}}} ]}. %% XRef checks diff --git a/rebar.lock b/rebar.lock index 01c4de7..8032ea1 100644 --- a/rebar.lock +++ b/rebar.lock @@ -3,9 +3,9 @@ {<<"cache">>,{pkg,<<"cache">>,<<"2.2.0">>},1}, {<<"certifi">>,{pkg,<<"certifi">>,<<"2.5.1">>},2}, {<<"cg_mon">>, - {git,"git@github.com:rbkmoney/cg_mon.git", + {git,"https://github.com/rbkmoney/cg_mon.git", {ref,"5a87a37694e42b6592d3b4164ae54e0e87e24e18"}}, - 2}, + 1}, {<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.5.0">>},1}, {<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.6.0">>},2}, {<<"damsel">>, @@ -16,6 +16,10 @@ {git,"git@github.com:rbkmoney/dmt_core.git", {ref,"8ac78cb1c94abdcdda6675dd7519893626567573"}}, 0}, + {<<"erl_health">>, + {git,"https://github.com/rbkmoney/erlang-health.git", + {ref,"3a80bb43a93eb0e7a940d19912d9e35c2cbe91f9"}}, + 0}, {<<"folsom">>, {git,"git@github.com:folsom-project/folsom.git", {ref,"9309bad9ffadeebbefe97521577c7480c7cfcd8a"}}, @@ -31,6 +35,7 @@ {ref,"2bb46054e16aaba9357747cc72b7c42e1897a56d"}}, 1}, {<<"idna">>,{pkg,<<"idna">>,<<"6.0.0">>},2}, + {<<"jsx">>,{pkg,<<"jsx">>,<<"2.8.0">>},1}, {<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},2}, {<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},2}, {<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.3.0">>},3}, @@ -60,6 +65,7 @@ {<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>}, {<<"hackney">>, <<"07E33C794F8F8964EE86CEBEC1A8ED88DB5070E52E904B8F12209773C1036085">>}, {<<"idna">>, <<"689C46CBCDF3524C44D5F3DDE8001F364CD7608A99556D8FBD8239A5798D4C10">>}, + {<<"jsx">>, <<"749BEC6D205C694AE1786D62CEA6CC45A390437E24835FD16D12D74F07097727">>}, {<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>}, {<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>}, {<<"parse_trans">>, <<"09765507A3C7590A784615CFD421D101AEC25098D50B89D7AA1D66646BC571C1">>}, diff --git a/src/dmt_client.app.src b/src/dmt_client.app.src index 61abb81..98fc597 100644 --- a/src/dmt_client.app.src +++ b/src/dmt_client.app.src @@ -6,7 +6,8 @@ kernel, stdlib, woody, - dmt_core + dmt_core, + erl_health ]}, {mod, {dmt_client, []}}, {env, []} diff --git a/src/dmt_client.erl b/src/dmt_client.erl index fbb7451..c9eab66 100644 --- a/src/dmt_client.erl +++ b/src/dmt_client.erl @@ -16,6 +16,9 @@ -export([pull_range/2]). -export([pull_range/3]). +%% Health check API +-export([health_check/0]). + %% Supervisor callbacks -export([init/1]). @@ -117,13 +120,60 @@ pull_range(Version, Limit) -> pull_range(Version, Limit, Opts) -> dmt_client_backend:pull_range(Version, Limit, Opts). +%% Health check API + +-spec health_check() -> + erl_health:result(). + +health_check() -> + try + _ = dmt_client_cache:get_last_version(), + {passing, #{}} + catch + _Class:_Error -> + {critical, #{last_version => not_found}} + end. + %%% Supervisor callbacks -spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. init([]) -> Cache = #{id => dmt_client_cache, start => {dmt_client_cache, start_link, []}, restart => permanent}, - {ok, {#{strategy => one_for_one, intensity => 10, period => 60}, [Cache]}}. + {ok, {#{ + strategy => one_for_one, intensity => 10, period => 60}, + [ + Cache, + get_health_spec() + ] + }}. + +get_health_spec() -> + {ok, Ip} = inet:parse_address(genlib_app:env(?MODULE, ip, "::")), + HealthRoutes = construct_health_routes(genlib_app:env(?MODULE, health_check, #{})), + woody_server:child_spec( + ?MODULE, + #{ + ip => Ip, + port => genlib_app:env(?MODULE, port, 8022), + transport_opts => genlib_app:env(?MODULE, server_transport_opts, #{}), + protocol_opts => genlib_app:env(?MODULE, protocol_opts, #{}), + event_handler => get_event_handlers(), + handlers => [], + additional_routes => HealthRoutes, + shutdown_timeout => genlib_app:env(?MODULE, shutdown_timeout, 0) + } + ). + +get_event_handlers() -> + genlib_app:env(dmt_client, server_woody_event_handlers, []). + +construct_health_routes(Check) -> + [erl_health_handle:get_route(enable_health_logging(Check))]. + +enable_health_logging(Check) -> + EvHandler = {erl_health_event_handler, []}, + maps:map(fun (_, V = {_, _, _}) -> #{runner => V, event_handler => EvHandler} end, Check). %%% Application callbacks diff --git a/src/dmt_client_api.erl b/src/dmt_client_api.erl index ebdd723..b69dac5 100644 --- a/src/dmt_client_api.erl +++ b/src/dmt_client_api.erl @@ -61,7 +61,7 @@ get_service_module('RepositoryClient') -> dmsl_domain_config_thrift. get_event_handlers() -> - genlib_app:env(dmt_client, woody_event_handlers, []). + genlib_app:env(dmt_client, client_woody_event_handlers, []). -spec ensure_transport_opts(dmt_client:transport_opts()) -> woody_client_thrift_http_transport:transport_options(). @@ -70,4 +70,4 @@ ensure_transport_opts(Opts) when is_map(Opts) -> Opts; ensure_transport_opts(undefined) -> Default = #{recv_timeout => 60000, connect_timeout => 1000}, - genlib_app:env(dmt_client, transport_opts, Default). + genlib_app:env(dmt_client, client_transport_opts, Default).