Skip to content

Commit

Permalink
set once the metric engine
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Apr 2, 2018
1 parent 8172a72 commit 9604828
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/hackney.hrl
Expand Up @@ -45,3 +45,6 @@
path,
ctype = nil
}).


-define(CONFIG, hackney_config).
2 changes: 0 additions & 2 deletions src/hackney_app.erl
Expand Up @@ -20,8 +20,6 @@
%% ===================================================================

start(_StartType, _StartArgs) ->
Metrics = metrics:init(hackney_util:mod_metrics()),
application:set_env(hackney, metrics, Metrics),
hackney_sup:start_link().

stop(_State) ->
Expand Down
4 changes: 2 additions & 2 deletions src/hackney_connect.erl
Expand Up @@ -66,7 +66,7 @@ create_connection(Transport, Host, Port, Options, Dynamic)
MaxBody = proplists:get_value(max_body, Options),

%% get mod metrics
{ok, Engine} = application:get_env(hackney, metrics),
Engine = hackney_metrics:get_engine(),

%% initial state
InitialState = #client{mod_metrics=Engine,
Expand Down Expand Up @@ -279,7 +279,7 @@ check_mod_metrics(#client{mod_metrics=Mod}=State)
when Mod /= nil, Mod /= undefined ->
State;
check_mod_metrics(State) ->
State#client{mod_metrics=metrics:init(hackney_util:mod_metrics())}.
State#client{mod_metrics=hackney_metrics:get_engine()}.

This comment has been minimized.

Copy link
@redink

redink Apr 3, 2018

I think this place is one remediation for can't get engine in L69.

If can't get engine in L69, why could get engine here ? So, I think it should be better for hackney_metrics:init(), first then get.

This comment has been minimized.

Copy link
@benoitc

benoitc Apr 3, 2018

Author Owner

Engine is setup when hackney is started and the configuration maintained via the main supervisor. If you can't get the engine then it means there is another issue.

Th current changes cover the following:

  • config can't be reset when the app env is reset
  • config is upgraded if the main supervisor is upgraded

What is not covered is the upgrade of the metric module itself which will require a full restart of the app. But this change is not expected. Next major version of hackney will go to opencensus.

This comment has been minimized.

Copy link
@redink

redink Apr 3, 2018

It sounds strange. Anyway, it almost impossible for can't get engine after using ets table to keep the engine when init it in main supervisor process.
Thanks.


ssl_opts(Host, Options) ->
case proplists:get_value(ssl_options, Options) of
Expand Down
3 changes: 1 addition & 2 deletions src/hackney_manager.erl
Expand Up @@ -602,8 +602,7 @@ untrack_owner(Pid, Ref, Pids) ->

init_metrics() ->
%% get metrics module
Engine = metrics:init(hackney_util:mod_metrics()),

Engine = hackney_metrics:get_engine(),
%% initialise metrics
_ = metrics:new(Engine, counter, [hackney, nb_requests]),
_ = metrics:new(Engine, counter, [hackney, total_requests]),
Expand Down
27 changes: 27 additions & 0 deletions src/hackney_metrics.erl
@@ -0,0 +1,27 @@
%%% -*- erlang -*-
%%%
%%% This file is part of hackney released under the Apache 2 license.
%%% See the NOTICE for more information.
%%%
%%% Copyright (c) 2012-2018 Benoît Chesneau <benoitc@e-engura.org>
%%%

-module(hackney_metrics).
-author("benoitc").

%% API
-export([
init/0,
get_engine/0
]).


-include("hackney.hrl").


init() ->
Metrics = metrics:init(hackney_util:mod_metrics()),
ets:insert(hackney_config, {mod_metrics, Metrics}).

get_engine() ->
ets:lookup_element(?CONFIG, mod_metrics, 2).
2 changes: 1 addition & 1 deletion src/hackney_pool.erl
Expand Up @@ -554,7 +554,7 @@ monitor_client(Dest, Ref, State) ->

init_metrics(PoolName) ->
%% get metrics module
Engine = metrics:init(hackney_util:mod_metrics()),
Engine = hackney_metrics:get_engine(),

%% initialise metrics
_ = metrics:new(Engine, histogram, [hackney_pool, PoolName, take_rate]),
Expand Down
10 changes: 8 additions & 2 deletions src/hackney_sup.erl
Expand Up @@ -3,7 +3,7 @@
%%% This file is part of hackney released under the Apache 2 license.
%%% See the NOTICE for more information.
%%%
%%% Copyright (c) 2012-2014 Benoît Chesneau <benoitc@e-engura.org>
%%% Copyright (c) 2012-2018 Benoît Chesneau <benoitc@e-engura.org>
%%%

-module(hackney_sup).
Expand All @@ -16,6 +16,8 @@
%% Supervisor callbacks
-export([init/1]).

-include("hackney.hrl").

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

Expand All @@ -28,7 +30,6 @@ start_link() ->
%% start the pool handler
PoolHandler = hackney_app:get_app_env(pool_handler, hackney_pool),
ok = PoolHandler:start(),

%% finish to start the application
{ok, Pid}.

Expand All @@ -37,5 +38,10 @@ start_link() ->
%% ===================================================================

init([]) ->
%% initialize the config table
_ = ets:new(?CONFIG, [set, named_table, public]),
%% initialize the metric engine
hackney_metrics:init(),
Manager = ?CHILD(hackney_manager, worker),
{ok, { {one_for_one, 10000, 1}, [Manager]}}.

0 comments on commit 9604828

Please sign in to comment.