From 3015cd8f579c6c9db3073c5426a0192bbf983166 Mon Sep 17 00:00:00 2001 From: Sascha Kattelmann Date: Fri, 23 Jun 2017 15:35:23 +0200 Subject: [PATCH] Allow floats --- README.md | 2 +- src/exometer_report_prometheus.erl | 20 +++++++------------- test/exometer_prometheus_test.erl | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2472912..3ad0fec 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ some_other_counter 1 # HELP some_gauge some exometer gauge # TYPE some_gauge gauge -some_gauge 1 +some_gauge 1.1235 # HELP some_spiral some exometer spiral # TYPE some_spiral gauge diff --git a/src/exometer_report_prometheus.erl b/src/exometer_report_prometheus.erl index f56ceca..a4728fb 100644 --- a/src/exometer_report_prometheus.erl +++ b/src/exometer_report_prometheus.erl @@ -101,22 +101,16 @@ format_metrics([{Metric, DataPoints, Name, Type, Help} | Metrics], Akk) -> format_metrics(Metrics, [Payload1, <<"\n">> | Akk]). make_metric_name(Metric) -> - lists:reverse(make_metric_name(Metric, [])). - -make_metric_name([], Akk) -> - Akk; -make_metric_name([Elem], Akk) -> - [normalize(Elem) | Akk]; -make_metric_name([Elem | Metric], Akk) -> - make_metric_name(Metric, [<<"_">>, normalize(Elem) | Akk]). - -normalize(Something) -> - re:replace(ioize(Something), "-|\\.", "_", [global, {return,binary}]). + NameList = lists:join($_, lists:map(fun ioize/1, Metric)), + NameBin = iolist_to_binary(NameList), + re:replace(NameBin, "-|\\.", "_", [global, {return,binary}]). ioize(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8); -ioize(Number) when is_integer(Number) or is_float(Number) -> - list_to_binary(integer_to_list(trunc(Number))); +ioize(Number) when is_float(Number) -> + float_to_binary(Number, [{decimals, 4}]); +ioize(Number) when is_integer(Number) -> + integer_to_binary(Number); ioize(Something) -> Something. diff --git a/test/exometer_prometheus_test.erl b/test/exometer_prometheus_test.erl index 5642fd2..d94e450 100644 --- a/test/exometer_prometheus_test.erl +++ b/test/exometer_prometheus_test.erl @@ -21,7 +21,7 @@ basic_test() -> ok = exometer_report:subscribe(?REPORTER, [some, other, counter], [value, ms_since_reset], manual, [{type, counter}, {help, "some other exometer counter with forced type"}]), - ok = exometer:update_or_create([some, gauge], 1, gauge, []), + ok = exometer:update_or_create([some, gauge], 1.1234567, gauge, []), ok = exometer_report:subscribe(?REPORTER, [some, gauge], [value, ms_since_reset], manual, [{help, "some exometer gauge"}]), ok = exometer:update_or_create([some, spiral], 1, spiral, []),