This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce metrics export framework for prometheus
- Loading branch information
1 parent
90579f9
commit 58482f1
Showing
9 changed files
with
278 additions
and
12 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
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,19 @@ | ||
-module(bn_metrics_exporter). | ||
|
||
-behaviour(elli_handler). | ||
|
||
-include_lib("elli/include/elli.hrl"). | ||
|
||
-export([handle/2, handle_event/3]). | ||
|
||
handle(Req, _Args) -> | ||
handle(Req#req.method, elli_request:path(Req), Req). | ||
|
||
%% Expose `/metrics` for Prometheus as a scrape target | ||
handle('GET', [<<"metrics">>], _Req) -> | ||
{ok, [], prometheus_text_format:format()}; | ||
handle(_Verb, _Path, _Req) -> | ||
ignore. | ||
|
||
handle_event(_Event, _Data, _Args) -> | ||
ok. |
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,175 @@ | ||
-module(bn_metrics_server). | ||
|
||
-behaviour(gen_server). | ||
|
||
-include("metrics/metrics.hrl"). | ||
|
||
-export([ | ||
handle_metric/4, | ||
start_link/0 | ||
]). | ||
|
||
-export([ | ||
init/1, | ||
handle_call/3, | ||
handle_cast/2, | ||
handle_info/2, | ||
terminate/2, | ||
code_change/3 | ||
]). | ||
|
||
-define(SERVER, ?MODULE). | ||
|
||
-type metric() :: { | ||
Metric :: string(), | ||
Event :: [atom()], | ||
PrometheusHandler :: module(), | ||
Labels :: [atom()], | ||
Description :: string() | ||
}. | ||
|
||
-type metrics() :: [metric()]. | ||
|
||
-type exporter_opts() :: [ | ||
{callback, module()} | | ||
{callback_args, map()} | | ||
{port, integer()} | ||
]. | ||
|
||
-record(state, { | ||
metrics :: metrics(), | ||
exporter_opts :: exporter_opts(), | ||
exporter_pid :: pid() | undefined | ||
}). | ||
|
||
handle_metric(Event, Measurements, Metadata, _Config) -> | ||
handle_metric_event(Event, Measurements, Metadata). | ||
|
||
start_link() -> | ||
gen_server:start_link({local, ?SERVER}, ?SERVER, [], []). | ||
|
||
init(_Args) -> | ||
case get_configs() of | ||
{[], []} -> ignore; | ||
{[_ | _], [_ | _]} = Metrics -> | ||
erlang:process_flag(trap_exit, true), | ||
|
||
ok = setup_metrics(Metrics), | ||
|
||
ElliOpts = [ | ||
{callback, be_metrics_exporter}, | ||
{callback_args, #{}}, | ||
{port, application:get_env(blockchain_node, metrics_port, 9090)} | ||
], | ||
{ok, ExporterPid} = elli:start_link(ElliOpts), | ||
{ok, #state{ | ||
metrics = Metrics, | ||
exporter_opts = ElliOpts, | ||
exporter_pid = ExporterPid}} | ||
end. | ||
|
||
handle_call(_Msg, _From, State) -> | ||
lager:debug("Received unknown call msg: ~p from ~p", [_Msg, _From]), | ||
{reply, ok, State}. | ||
|
||
handle_cast(_Msg, State) -> | ||
{noreply, State}. | ||
|
||
handle_info({'EXIT', ExporterPid, Reason}, #state{exporter_pid=ExporterPid} = State) -> | ||
lager:warning("Metrics exporter exited with reason ~p, restarting", [Reason]), | ||
{ok, NewExporter} = elli:start_link(State#state.exporter_opts), | ||
{noreply, State#state{exporter_pid = NewExporter}}; | ||
handle_info(_Msg, State) -> | ||
lager:debug("Received unknown info msg: ~p", [_Msg]), | ||
{noreply, State}. | ||
|
||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. | ||
|
||
terminate(Reason, #state{metrics = Metrics, exporter_pid = Exporter}) -> | ||
true = erlang:exit(Exporter, Reason), | ||
lists:foreach( | ||
fun({Metric, Module, _, _}) -> | ||
lager:info("De-registering metric ~p as ~p", [Metric, Module]), | ||
Module:deregister(Metric) | ||
end, | ||
Metrics | ||
). | ||
|
||
setup_metrics({EventNames, EventSpecs}) -> | ||
lager:warning("METRICS ~p", [EventSpecs]), | ||
lists:foreach( | ||
fun({Metric, Module, Meta, Description}) -> | ||
lager:info("Declaring metric ~p as ~p meta=~p", [Metric, Module, Meta]), | ||
MetricOpts = [{name, Metric}, {help, Description}, {labels, Meta}], | ||
case Module of | ||
prometheus_histogram -> | ||
Module:declare(MetricOpts ++ [{buckets, ?METRICS_HISTOGRAM_BUCKETS}]); | ||
_ -> | ||
Module:declare(MetricOpts) | ||
end | ||
end, | ||
EventSpecs | ||
), | ||
|
||
ok = telemetry:attach_many(<<"bn-metrics-handler">>, EventNames, fun bn_metrics_server:handle_metric/4, []). | ||
|
||
get_configs() -> | ||
lists:foldl( | ||
fun(Metric, {Names, Specs} = Acc) -> | ||
case maps:get(Metric, ?METRICS, undefined) of | ||
undefined -> Acc; | ||
{N, S} -> {Names ++ N, Specs ++ S} | ||
end | ||
end, | ||
{[], []}, | ||
application:get_env(blockchain_node, metrics, []) | ||
). | ||
|
||
handle_metric_event([blockchain, block, absorb], #{duration := Duration}, #{stage := Stage}) -> | ||
prometheus_histogram:observe(?METRICS_BLOCK_ABSORB, [Stage], Duration), | ||
ok; | ||
handle_metric_event([blockchain, block, height], #{height := Height}, #{time := Time}) -> | ||
prometheus_gauge:set(?METRICS_BLOCK_HEIGHT, [Time], Height), | ||
ok; | ||
handle_metric_event([blockchain, block, unvalidated_absorb], #{duration := Duration}, #{stage := Stage}) -> | ||
prometheus_histogram:observe(?METRICS_BLOCK_UNVAL_ABSORB, [Stage], Duration), | ||
ok; | ||
handle_metric_event([blockchain, block, unvalidated_height], #{height := Height}, #{time := Time}) -> | ||
prometheus_gauge:set(?METRICS_BLOCK_UNVAL_HEIGHT, [Time], Height), | ||
ok; | ||
handle_metric_event([blockchain, txn, absorb], #{duration := Duration}, #{type := Type}) -> | ||
prometheus_histogram:observe(?METRICS_TXN_ABSORB_DURATION, [Type], Duration), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, submit], _Measurements, #{type := Type}) -> | ||
prometheus_counter:inc(?METRICS_TXN_SUBMIT_COUNT, [Type]), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, reject], #{block_span := Span}, #{type := Type}) -> | ||
prometheus_counter:inc(?METRICS_TXN_REJECT_COUNT, [Type]), | ||
prometheus_gauge:set(?METRICS_TXN_BLOCK_SPAN, [], Span), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, accept], #{block_span := Span, queue_len := QLen}, #{type := Type}) -> | ||
prometheus_counter:inc(?METRICS_TXN_ACCEPT_COUNT, [Type]), | ||
prometheus_gauge:set(?METRICS_TXN_BLOCK_SPAN, [], Span), | ||
prometheus_gauge:set(?METRICS_TXN_QUEUE, [], QLen), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, update], #{block_span := Span, queue_len := QLen}, #{type := Type}) -> | ||
prometheus_counter:inc(?METRICS_TXN_UPDATE_COUNT, [Type]), | ||
prometheus_gauge:set(?METRICS_TXN_BLOCK_SPAN, [], Span), | ||
prometheus_gauge:set(?METRICS_TXN_QUEUE, [], QLen), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, process], #{duration := Duration}, #{stage := Stage}) -> | ||
prometheus_histogram:observe(?METRICS_TXN_PROCESS_DURATION, [Stage], Duration), | ||
ok; | ||
handle_metric_event([blockchain, txn_mgr, add_block], #{cache := Cache, block_time := BlockTime, block_age := BlockAge}, #{height := Height}) -> | ||
prometheus_gauge:set(?METRICS_TXN_CACHE_SIZE, [Height], Cache), | ||
prometheus_gauge:set(?METRICS_TXN_BLOCK_TIME, [Height], BlockTime), | ||
prometheus_gauge:set(?METRICS_TXN_BLOCK_AGE, [Height], BlockAge), | ||
ok; | ||
handle_metric_event([grpcbox, server, rpc_end], #{server_latency := Latency}, #{grpc_server_method := Method, grpc_server_status := Status}) -> | ||
prometheus_gauge:dec(?METRICS_GRPC_SESSIONS, [Method]), | ||
prometheus_histogram:observe(?METRICS_GRPC_LATENCY, [Method, Status], Latency), | ||
ok; | ||
handle_metric_event([grpcbox, server, rpc_begin], _Measurements, #{grpc_server_method := Method}) -> | ||
prometheus_gauge:inc(?METRICS_GRPC_SESSIONS, [Method]), | ||
ok. |
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 @@ | ||
-define(METRICS_HISTOGRAM_BUCKETS, [50, 100, 250, 500, 1000, 2000, 5000, 10000, 30000, 60000]). | ||
|
||
-define(METRICS_BLOCK_ABSORB, "blockchain_block_absorb_duration"). | ||
-define(METRICS_BLOCK_UNVAL_ABSORB, "blockchain_block_unval_absorb_duration"). | ||
-define(METRICS_BLOCK_HEIGHT, "blockchain_block_height"). | ||
-define(METRICS_BLOCK_UNVAL_HEIGHT, "blockchain_block_unval_height"). | ||
-define(METRICS_TXN_ABSORB_DURATION, "blockchain_txn_absorb_duration"). | ||
-define(METRICS_TXN_BLOCK_SPAN, "blockchain_txn_mgr_block_span"). | ||
-define(METRICS_TXN_QUEUE, "blockchain_txn_mgr_queue"). | ||
-define(METRICS_TXN_SUBMIT_COUNT, "blockchain_txn_mgr_submitted_count"). | ||
-define(METRICS_TXN_REJECT_COUNT, "blockchain_txn_mgr_rejected_count"). | ||
-define(METRICS_TXN_ACCEPT_COUNT, "blockchain_txn_mgr_accepted_count"). | ||
-define(METRICS_TXN_UPDATE_COUNT, "blockchain_txn_mgr_updated_count"). | ||
-define(METRICS_TXN_PROCESS_DURATION, "blockchain_txn_mgr_process_duration"). | ||
-define(METRICS_TXN_CACHE_SIZE, "blockchain_txn_mgr_cache_size"). | ||
-define(METRICS_TXN_BLOCK_TIME, "blockchain_txn_mgr_block_time"). | ||
-define(METRICS_TXN_BLOCK_AGE, "blockchain_txn_mgr_block_age"). | ||
-define(METRICS_GRPC_SESSIONS, "grpc_session_count"). | ||
-define(METRICS_GRPC_LATENCY, "grpcbox_session_latency"). | ||
|
||
-define(METRICS, #{ | ||
block_metrics => { | ||
[ [blockchain, block, absorb], | ||
[blockchain, block, height], | ||
[blockchain, block, unvalidated_absorb], | ||
[blockchain, block, unvalidated_height] ], | ||
[ {?METRICS_BLOCK_ABSORB, prometheus_histogram, [stage], "Block absorb duration"}, | ||
{?METRICS_BLOCK_HEIGHT, prometheus_gauge, [time], "Most recent block height"}, | ||
{?METRICS_BLOCK_UNVAL_ABSORB, prometheus_histogram, [stage], "Block unvalidated absorb duration"}, | ||
{?METRICS_BLOCK_UNVAL_HEIGHT, prometheus_gauge, [time], "Most recent unvalidated block height"} ] | ||
}, | ||
txn_metrics => { | ||
[ [blockchain, txn, absorb], | ||
[blockchain, txn_mgr, submit], | ||
[blockchain, txn_mgr, reject], | ||
[blockchain, txn_mgr, accept], | ||
[blockchain, txn_mgr, update], | ||
[blockchain, txn_mgr, process], | ||
[blockchain, txn_mgr, add_block] ], | ||
[ {?METRICS_TXN_ABSORB_DURATION, prometheus_histogram, [stage], "Txn absorb duration"}, | ||
{?METRICS_TXN_BLOCK_SPAN, prometheus_gauge, [], "Block span on transactions"}, | ||
{?METRICS_TXN_QUEUE, prometheus_gauge, [], "Txn manager submission queue length"}, | ||
{?METRICS_TXN_SUBMIT_COUNT, prometheus_counter, [type], "Count of submitted transactions"}, | ||
{?METRICS_TXN_REJECT_COUNT, prometheus_counter, [type], "Count of rejected transactions"}, | ||
{?METRICS_TXN_ACCEPT_COUNT, prometheus_counter, [type], "Count of accepted transactions"}, | ||
{?METRICS_TXN_UPDATE_COUNT, prometheus_counter, [type], "Count of updated transaction"}, | ||
{?METRICS_TXN_PROCESS_DURATION, prometheus_histogram, [stage], "Transaction manager cache process duration"}, | ||
{?METRICS_TXN_CACHE_SIZE, prometheus_gauge, [height], "Transaction manager buffer size"}, | ||
{?METRICS_TXN_BLOCK_TIME, prometheus_gauge, [height], "Block time observed from the transaction manager"}, | ||
{?METRICS_TXN_BLOCK_AGE, prometheus_gauge, [height], "Block age observed from the transaction manager"} ] | ||
}, | ||
grpc_metrics => { | ||
[ [grpcbox, server, rpc_begin], | ||
[grpcbox, server, rpc_end] ], | ||
[ {?METRICS_GRPC_SESSIONS, prometheus_gauge, [method], "GRPC session count"}, | ||
{?METRICS_GRPC_LATENCY, prometheus_histogram, [method, status], "GRPC session latency"} ] | ||
} | ||
}). |