From 9ae19918a990177e5fc76f7640121485f4c1cdb2 Mon Sep 17 00:00:00 2001 From: ndiezel0 Date: Mon, 21 Feb 2022 11:57:24 +0300 Subject: [PATCH] TD-187: Add Prometheus (#5) --- config/sys.config | 5 ++++- rebar.config | 10 ++++++++++ src/bouncer.erl | 10 +++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/config/sys.config b/config/sys.config index 2e0f7b3..916b548 100644 --- a/config/sys.config +++ b/config/sys.config @@ -99,6 +99,9 @@ {snowflake, [ {max_backward_clock_moving, 1000} % 1 second % {machine_id, hostname_hash} - ]} + ]}, + {prometheus, [ + {collectors, [default]} + ]} ]. diff --git a/rebar.config b/rebar.config index 32fbc06..aeec88e 100644 --- a/rebar.config +++ b/rebar.config @@ -93,6 +93,14 @@ {profiles, [ {prod, [ + {deps, [ + %% NOTE + %% Because of a dependency conflict, prometheus libs are only included in production build for now + %% https://github.com/project-fifo/rebar3_lint/issues/42 + %% https://github.com/valitydev/hellgate/pull/2/commits/884724c1799703cee4d1033850fe32c17f986d9e + {prometheus, "4.8.1"}, + {prometheus_cowboy, "0.1.8"} + ]}, %% Relx configuration {relx, [ {release, {bouncer, "0.1.0"}, [ @@ -106,6 +114,8 @@ % logger formatter {logger_logstash_formatter, load}, how_are_you, + prometheus, + prometheus_cowboy, bouncer ]}, {sys_config, "./config/sys.config"}, diff --git a/src/bouncer.erl b/src/bouncer.erl index 359e860..569f795 100644 --- a/src/bouncer.erl +++ b/src/bouncer.erl @@ -32,6 +32,10 @@ init([]) -> EventHandlers = genlib_app:env(?MODULE, woody_event_handlers, [woody_event_handler_default]), Healthcheck = enable_health_logging(genlib_app:env(?MODULE, health_check, #{})), {OpaClient, OpaClientSpec} = bouncer_opa_client:init(get_opa_opts()), + AdditionalRoutes = [ + erl_health_handle:get_route(Healthcheck), + get_prometheus_route() + ], WoodySpec = woody_server:child_spec( ?MODULE, #{ @@ -44,7 +48,7 @@ init([]) -> handlers => get_handler_specs(ServiceOpts, AuditPulse, OpaClient) ++ get_stub_handler_specs(ServiceOpts), - additional_routes => [erl_health_handle:get_route(Healthcheck)] + additional_routes => AdditionalRoutes } ), {ok, { @@ -126,3 +130,7 @@ enable_health_logging(Check) -> fun(_, Runner) -> #{runner => Runner, event_handler => EvHandler} end, Check ). + +-spec get_prometheus_route() -> {iodata(), module(), _Opts :: any()}. +get_prometheus_route() -> + {"/metrics/[:registry]", prometheus_cowboy2_handler, []}.