From 6768b7ba996f6fa81a6c194f581405cd7db4fa5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D1=80=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D1=80=D0=BE=D0=B2?= Date: Sun, 26 May 2019 01:18:15 +0200 Subject: [PATCH] Minor mtp_metric optimizations --- src/mtp_metric.erl | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/mtp_metric.erl b/src/mtp_metric.erl index 2019045..c2a5717 100644 --- a/src/mtp_metric.erl +++ b/src/mtp_metric.erl @@ -45,11 +45,30 @@ rt(Name, Fun, Extra) -> notify(Type, Name, Value, Extra) -> - case application:get_env(?APP, metric_backend) of - {ok, Mod} -> - Mod:notify(Type, Name, Value, Extra); - _ -> - false + case get_backend() of + undefined -> + false; + Mod -> + Mod:notify(Type, Name, Value, Extra) + end. + +get_backend() -> + %% Cache resutl of application:get_env in process dict because it's on the hot path + case erlang:get(metric_backend) of + undefined -> + case application:get_env(?APP, metric_backend) of + {ok, Mod} when Mod =/= false; + Mod =/= undefined -> + erlang:put(metric_backend, Mod), + Mod; + _ -> + erlang:put(metric_backend, false), + undefined + end; + false -> + undefined; + Mod -> + Mod end. -spec passive_metrics() -> [{metric_type(), metric_name(), metric_doc(), @@ -80,8 +99,7 @@ passive_metrics() -> [{gauge, [?APP, connections, count], "Count of ranch connections", [{#{listener => H}, proplists:get_value(all_connections, P)} - || {H, P} <- ranch:info(), - proplists:get_value(protocol, P) == mtp_handler]}] ]. + || {H, P} <- mtproto_proxy_app:mtp_listeners()]}] ]. -spec active_metrics() -> [{metric_type(), metric_name(), metric_doc(), Opts}] when