diff options
Diffstat (limited to 'src/couch_prometheus/src/couch_prometheus_util.erl')
-rw-r--r-- | src/couch_prometheus/src/couch_prometheus_util.erl | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/src/couch_prometheus/src/couch_prometheus_util.erl b/src/couch_prometheus/src/couch_prometheus_util.erl index c3b58cb3a..ea2cdf737 100644 --- a/src/couch_prometheus/src/couch_prometheus_util.erl +++ b/src/couch_prometheus/src/couch_prometheus_util.erl @@ -10,7 +10,7 @@ % License for the specific language governing permissions and limitations under % the License. --module(couch_prometheus_util ). +-module(couch_prometheus_util). -export([ couch_to_prom/3, @@ -25,7 +25,6 @@ couch_to_prom([couch_log, level, alert], Info, _All) -> to_prom(couch_log_requests_total, counter, {[{level, alert}], val(Info)}); couch_to_prom([couch_log, level, Level], Info, _All) -> to_prom(couch_log_requests_total, {[{level, Level}], val(Info)}); - couch_to_prom([couch_replicator, checkpoints, failure], Info, _All) -> to_prom(couch_replicator_checkpoints_failure_total, counter, val(Info)); couch_to_prom([couch_replicator, checkpoints, success], Info, All) -> @@ -41,7 +40,6 @@ couch_to_prom([couch_replicator, stream_responses, failure], Info, _All) -> couch_to_prom([couch_replicator, stream_responses, success], Info, All) -> Total = val(Info) + val([couch_replicator, stream_responses, failure], All), to_prom(couch_replicator_stream_responses_total, counter, Total); - couch_to_prom([couchdb, auth_cache_hits], Info, All) -> Total = val(Info) + val([couchdb, auth_cache_misses], All), to_prom(auth_cache_requests_total, counter, Total); @@ -53,7 +51,6 @@ couch_to_prom([couchdb, httpd_request_methods, Method], Info, _All) -> to_prom(httpd_request_methods, {[{method, Method}], val(Info)}); couch_to_prom([couchdb, httpd_status_codes, Code], Info, _All) -> to_prom(httpd_status_codes, {[{code, Code}], val(Info)}); - couch_to_prom([ddoc_cache, hit], Info, All) -> Total = val(Info) + val([ddoc_cache, miss], All), to_prom(ddoc_cache_requests_total, counter, Total); @@ -61,21 +58,17 @@ couch_to_prom([ddoc_cache, miss], Info, _All) -> to_prom(ddoc_cache_requests_failures_total, counter, val(Info)); couch_to_prom([ddoc_cache, recovery], Info, _All) -> to_prom(ddoc_cache_requests_recovery_total, counter, val(Info)); - couch_to_prom([fabric, read_repairs, failure], Info, _All) -> to_prom(fabric_read_repairs_failures_total, counter, val(Info)); couch_to_prom([fabric, read_repairs, success], Info, All) -> Total = val(Info) + val([fabric, read_repairs, failure], All), to_prom(fabric_read_repairs_total, counter, Total); - couch_to_prom([rexi, streams, timeout, init_stream], Info, _All) -> to_prom(rexi_streams_timeout_total, counter, {[{stage, init_stream}], val(Info)}); couch_to_prom([rexi_streams, timeout, Stage], Info, _All) -> to_prom(rexi_streams_timeout_total, {[{stage, Stage}], val(Info)}); - couch_to_prom([couchdb | Rest], Info, All) -> couch_to_prom(Rest, Info, All); - couch_to_prom(Path, Info, _All) -> case lists:keyfind(type, 1, Info) of {type, counter} -> @@ -94,16 +87,20 @@ to_prom(Metric, Type, Data) -> to_prom(Metric, Instances) when is_list(Instances) -> lists:flatmap(fun(Inst) -> to_prom(Metric, Inst) end, Instances); to_prom(Metric, {Labels, Value}) -> - LabelParts = lists:map(fun({K, V}) -> - lists:flatten(io_lib:format("~s=\"~s\"", [to_bin(K), to_bin(V)])) - end, Labels), - MetricStr = case length(LabelParts) > 0 of - true -> - LabelStr = string:join(LabelParts, ", "), - lists:flatten(io_lib:format("~s{~s}", [to_prom_name(Metric), LabelStr])); - false -> - lists:flatten(io_lib:format("~s", [to_prom_name(Metric)])) - end, + LabelParts = lists:map( + fun({K, V}) -> + lists:flatten(io_lib:format("~s=\"~s\"", [to_bin(K), to_bin(V)])) + end, + Labels + ), + MetricStr = + case length(LabelParts) > 0 of + true -> + LabelStr = string:join(LabelParts, ", "), + lists:flatten(io_lib:format("~s{~s}", [to_prom_name(Metric), LabelStr])); + false -> + lists:flatten(io_lib:format("~s", [to_prom_name(Metric)])) + end, [to_bin(io_lib:format("~s ~p", [MetricStr, Value]))]; to_prom(Metric, Value) -> [to_bin(io_lib:format("~s ~p", [to_prom_name(Metric), Value]))]. @@ -114,18 +111,21 @@ to_prom_summary(Path, Info) -> {arithmetic_mean, Mean} = lists:keyfind(arithmetic_mean, 1, Value), {percentile, Percentiles} = lists:keyfind(percentile, 1, Value), {n, Count} = lists:keyfind(n, 1, Value), - Quantiles = lists:map(fun({Perc, Val0}) -> - % Prometheus uses seconds, so we need to covert milliseconds to seconds - Val = Val0/1000, - case Perc of - 50 -> {[{quantile, <<"0.5">>}], Val}; - 75 -> {[{quantile, <<"0.75">>}], Val}; - 90 -> {[{quantile, <<"0.9">>}], Val}; - 95 -> {[{quantile, <<"0.95">>}], Val}; - 99 -> {[{quantile, <<"0.99">>}], Val}; - 999 -> {[{quantile, <<"0.999">>}], Val} - end - end, Percentiles), + Quantiles = lists:map( + fun({Perc, Val0}) -> + % Prometheus uses seconds, so we need to covert milliseconds to seconds + Val = Val0 / 1000, + case Perc of + 50 -> {[{quantile, <<"0.5">>}], Val}; + 75 -> {[{quantile, <<"0.75">>}], Val}; + 90 -> {[{quantile, <<"0.9">>}], Val}; + 95 -> {[{quantile, <<"0.95">>}], Val}; + 99 -> {[{quantile, <<"0.99">>}], Val}; + 999 -> {[{quantile, <<"0.999">>}], Val} + end + end, + Percentiles + ), SumMetric = path_to_name(Path ++ ["seconds", "sum"]), SumStat = to_prom(SumMetric, Count * Mean), CountMetric = path_to_name(Path ++ ["seconds", "count"]), @@ -136,9 +136,12 @@ to_prom_name(Metric) -> to_bin(io_lib:format("couchdb_~s", [Metric])). path_to_name(Path) -> - Parts = lists:map(fun(Part) -> - io_lib:format("~s", [Part]) - end, Path), + Parts = lists:map( + fun(Part) -> + io_lib:format("~s", [Part]) + end, + Path + ), string:join(Parts, "_"). counter_metric(Path) -> @@ -163,4 +166,4 @@ val(Data) -> val(Key, Stats) -> {Key, Data} = lists:keyfind(Key, 1, Stats), - val(Data).
\ No newline at end of file + val(Data). |