Skip to content

Commit

Permalink
Allow floats
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Kattelmann committed Jun 23, 2017
1 parent 55638e7 commit 3015cd8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions src/exometer_report_prometheus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion test/exometer_prometheus_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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, []),
Expand Down

0 comments on commit 3015cd8

Please sign in to comment.