-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
-module(erldns_metrics). | ||
|
||
-behavior(gen_server). | ||
|
||
-export([start_link/0]). | ||
|
||
-define(DEFAULT_PORT, 8082). | ||
|
||
% Gen server hooks | ||
-export([init/1, | ||
handle_call/3, | ||
handle_cast/2, | ||
handle_info/2, | ||
terminate/2, | ||
code_change/3 | ||
]). | ||
|
||
-record(state, {}). | ||
|
||
start_link() -> | ||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). | ||
|
||
init([]) -> | ||
lager:debug("Starting ~p", [?MODULE]), | ||
|
||
Dispatch = cowboy_router:compile( | ||
[ | ||
{'_', | ||
[ | ||
{"/", erldns_metrics_root_handler, []} | ||
] | ||
} | ||
] | ||
), | ||
|
||
{ok, _} = cowboy:start_http(http, 10, [{port, port()}], [{env, [{dispatch, Dispatch}]}]), | ||
|
||
{ok, #state{}}. | ||
|
||
handle_call(_Message, _From, State) -> | ||
{reply, ok, State}. | ||
handle_cast(_, State) -> | ||
{noreply, State}. | ||
handle_info(_, State) -> | ||
{noreply, State}. | ||
terminate(_, _) -> | ||
ok. | ||
code_change(_PreviousVersion, State, _Extra) -> | ||
{ok, State}. | ||
|
||
port() -> | ||
proplists:get_value(port, metrics_env(), ?DEFAULT_PORT). | ||
|
||
metrics_env() -> | ||
case application:get_env(erldns, metrics) of | ||
{ok, MetricsEnv} -> MetricsEnv; | ||
_ -> [] | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-module(erldns_metrics_root_handler). | ||
|
||
-export([init/3]). | ||
-export([content_types_provided/2]). | ||
-export([to_html/2, to_json/2, to_text/2]). | ||
|
||
-export([filter_stats/1]). | ||
|
||
init(_Transport, _Req, []) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
|
||
content_types_provided(Req, State) -> | ||
{[ | ||
{<<"text/html">>, to_html}, | ||
{<<"text/plain">>, to_text}, | ||
{<<"application/json">>, to_json} | ||
], Req, State}. | ||
|
||
to_html(Req, State) -> | ||
{<<"erldns metrics">>, Req, State}. | ||
|
||
to_text(Req, State) -> | ||
{<<"erldns metrics">>, Req, State}. | ||
|
||
to_json(Req, State) -> | ||
Body = jsx:encode([{<<"erldns">>, | ||
[ | ||
{<<"metrics">>, erldns_app:metrics()}, | ||
{<<"stats">>, filter_stats(erldns_app:stats())} | ||
] | ||
}]), | ||
{Body, Req, State}. | ||
|
||
% Functions to clean up the stats so they can be returned as JSON. | ||
filter_stats(Stats) -> | ||
filter_stats(Stats, []). | ||
|
||
filter_stats([], FilteredStats) -> | ||
FilteredStats; | ||
filter_stats([{Name, Stats}|Rest], FilteredStats) -> | ||
filter_stats(Rest, FilteredStats ++ [{Name, filter_stat_set(Stats)}]). | ||
|
||
filter_stat_set(Stats) -> | ||
filter_stat_set(Stats, []). | ||
|
||
filter_stat_set([], FilteredStatSet) -> | ||
FilteredStatSet; | ||
filter_stat_set([{percentile, Percentiles}|Rest], FilteredStatSet) -> | ||
filter_stat_set(Rest, FilteredStatSet ++ [{percentile, keys_to_strings(Percentiles)}]); | ||
filter_stat_set([{histogram, _}|Rest], FilteredStatSet) -> | ||
filter_stat_set(Rest, FilteredStatSet); | ||
filter_stat_set([Pair|Rest], FilteredStatSet) -> | ||
filter_stat_set(Rest, FilteredStatSet ++ [Pair]). | ||
|
||
keys_to_strings(Pairs) -> | ||
lists:map( | ||
fun({K, V}) -> | ||
{list_to_binary(lists:flatten(io_lib:format("~p", [K]))), V} | ||
end, Pairs). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters