Skip to content

Commit

Permalink
use the metrics application
Browse files Browse the repository at this point in the history
There is no point to maintain an duplicae code here since we maintain the
metrics application afterall.
  • Loading branch information
benoitc committed Mar 2, 2016
1 parent 970d17c commit 74fe0a3
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 303 deletions.
9 changes: 4 additions & 5 deletions doc/edoc-info
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
%% encoding: UTF-8
{application,hackney}.
{modules,[hackney,hackney_app,hackney_bstr,hackney_connect,hackney_cookie,
hackney_date,hackney_dummy_metrics,hackney_exometer_metrics,
hackney_folsom_metrics,hackney_headers,hackney_http,
hackney_http_connect,hackney_manager,hackney_multipart,hackney_pool,
hackney_pool_handler,hackney_request,hackney_response,
hackney_socks5,hackney_ssl_transport,hackney_stream,hackney_sup,
hackney_date,hackney_headers,hackney_http,hackney_http_connect,
hackney_manager,hackney_multipart,hackney_pool,hackney_pool_handler,
hackney_request,hackney_response,hackney_socks5,
hackney_ssl_transport,hackney_stream,hackney_sup,
hackney_tcp_transport,hackney_trace,hackney_url,hackney_util]}.
2 changes: 1 addition & 1 deletion include/hackney.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
connection = nil,
method = nil,
path,
ctype = nil}).
ctype = nil}).
3 changes: 2 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{idna, "1.1.0"},
{mimerl, "1.0.2"},
{certifi, "0.3.0"},
{metrics, "1.0.0"},
{ssl_verify_hostname, "1.0.5"}
]}.

Expand All @@ -38,4 +39,4 @@
{test, [
{deps, [{cowboy, "1.0.4"}]}
]}
]}.
]}.
4 changes: 4 additions & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Rebar2Deps0 = [{idna, ".*",
{git, "https://github.com/certifi/erlang-certifi",
{tag, "0.3.0"}}},

{metrics, ".*",
{git, "https://github.com/benoitc/metrics",
{tag, "1.0.0"}}},

{ssl_verify_hostname, ".*",
{git, "https://github.com/deadtrickster/ssl_verify_hostname.erl",
{tag, "1.0.5"}}}],
Expand Down
1 change: 1 addition & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[{<<"certifi">>,{pkg,<<"certifi">>,<<"0.3.0">>},0},
{<<"idna">>,{pkg,<<"idna">>,<<"1.1.0">>},0},
{<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.0">>},0},
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.0.2">>},0},
{<<"ssl_verify_hostname">>,{pkg,<<"ssl_verify_hostname">>,<<"1.0.5">>},0}].
2 changes: 1 addition & 1 deletion src/hackney.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
idna,
mimerl,
certifi]},
{included_applications, [ssl_verify_hostname]},
{included_applications, [ssl_verify_hostname, metrics]},
{mod, { hackney_app, []}},
{env, [{timeout, 150000},
{max_connections, 50},
Expand Down
34 changes: 17 additions & 17 deletions src/hackney_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
-define(REFS, hackney_manager_refs).

-record(mstate, {pids=dict:new(),
mod_metrics}).
metrics}).


new_request(#client{request_ref=Ref}=Client)
Expand Down Expand Up @@ -283,14 +283,14 @@ init(_) ->
ets:new(?REFS, [named_table, set, protected]),

%% initialize metrics
Mod = init_metrics(),
Metrics = init_metrics(),

process_flag(trap_exit, true),
%% return {ok, {Pids, Refs}}
%% Pids are the managed pids
%% Refs are the managed requests
{ok, #mstate{pids=dict:new(),
mod_metrics=Mod}}.
metrics=Metrics}}.


handle_call({new_request, Pid, Ref, Client}, _From, #mstate{pids=Pids}=State) ->
Expand Down Expand Up @@ -581,24 +581,24 @@ wait_async_response(Stream) ->

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

%% initialise metrics
Mod:new(counter, [hackney, nb_requests]),
Mod:new(counter, [hackney, total_requests]),
Mod:new(counter, [hackney, finished_requests]),
Mod.
metrics:new(Engine, counter, [hackney, nb_requests]),
metrics:new(Engine, counter, [hackney, total_requests]),
metrics:new(Engine, counter, [hackney, finished_requests]),
Engine.

start_request(#request_info{host=Host}, #mstate{mod_metrics=Mod}) ->
Mod:increment_counter([hackney, Host, nb_requests]),
Mod:increment_counter([hackney, nb_requests]),
Mod:increment_counter([hackney, total_requests]).
start_request(#request_info{host=Host}, #mstate{metrics=Engine}) ->
metrics:increment_counter(Engine, [hackney, Host, nb_requests]),
metrics:increment_counter(Engine, [hackney, nb_requests]),
metrics:increment_counter(Engine, [hackney, total_requests]).


finish_request(#request_info{start_time=Begin, host=Host},
#mstate{mod_metrics=Mod}) ->
#mstate{metrics=Engine}) ->
RequestTime = timer:now_diff(os:timestamp(), Begin)/1000,
Mod:update_histogram([hackney, Host, request_time], RequestTime),
Mod:decrement_counter([hackney, Host, nb_requests]),
Mod:decrement_counter([hackney, nb_requests]),
Mod:increment_counter([hackney, finished_requests]).
metrics:update_histogram(Engine, [hackney, Host, request_time], RequestTime),
metrics:decrement_counter(Engine, [hackney, Host, nb_requests]),
metrics:decrement_counter(Engine, [hackney, nb_requests]),
metrics:increment_counter(Engine, [hackney, finished_requests]).
17 changes: 5 additions & 12 deletions src/hackney_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,11 @@ privdir() ->

mod_metrics() ->
case application:get_env(hackney, mod_metrics) of
{ok, folsom} -> hackney_folsom_metrics;
{ok, exometer} -> hackney_exometer_metrics;
{ok, dummy} -> hackney_dummy_metrics;
{ok, Mod} ->
_ = code:ensure_loaded(Mod),
case erlang:function_exported(Mod, new, 2) of
false ->
{error, badarg};
true ->
Mod
end;
_ -> hackney_dummy_metrics
{ok, folsom} -> metrics_folsom;
{ok, exometer} -> metrics_exometers;
{ok, dummy} -> metrics_dumy;
{ok, Mod} -> Mod;
_ -> metrics_dummy
end.


Expand Down
6 changes: 3 additions & 3 deletions src/http/hackney_response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ wait_headers({header, {Key, Value}=KV, Parser}, Client, Status, Headers) ->
[KV | Headers]);

wait_headers({headers_complete, Parser}, Client, Status, Headers) ->
Mod = Client#client.mod_metrics,
ResponseTime = timer:now_diff(os:timestamp(),
Client#client.start_time)/1000,
Mod:update_histogram([hackney, Client#client.host, response_time],
ResponseTime),
metrics:update_histogram(Client#client.mod_metrics,
[hackney, Client#client.host, response_time],
ResponseTime),
{ok, Status, lists:reverse(Headers), Client#client{parser=Parser}}.

stream_body(Client=#client{response_state=done}) ->
Expand Down
50 changes: 0 additions & 50 deletions src/metrics/hackney_dummy_metrics.erl

This file was deleted.

76 changes: 0 additions & 76 deletions src/metrics/hackney_exometer_metrics.erl

This file was deleted.

87 changes: 0 additions & 87 deletions src/metrics/hackney_folsom_metrics.erl

This file was deleted.

Loading

0 comments on commit 74fe0a3

Please sign in to comment.