Skip to content

Commit

Permalink
Fix scientific format in cumulative counters of histograms
Browse files Browse the repository at this point in the history
Fixes:
- #130
  • Loading branch information
surik committed Apr 5, 2022
1 parent 3ad4df5 commit 18e1a45
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/formats/prometheus_text_format.erl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ escape_help_char(X) ->
bound_to_label_value(Bound) when is_integer(Bound) ->
integer_to_list(Bound);
bound_to_label_value(Bound) when is_float(Bound) ->
float_to_list(Bound);
float_to_list(Bound, [{decimals, 10}, compact]);
bound_to_label_value(infinity) ->
"+Inf".

Expand Down
29 changes: 29 additions & 0 deletions test/eunit/format/prometheus_text_format_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ prometheus_format_test_() ->
fun test_quantile_summary/1,
fun test_quantile_dsummary/1,
fun test_histogram/1,
fun test_histogram_float/1,
fun test_dhistogram/1]}.

content_type_test() ->
Expand Down Expand Up @@ -176,6 +177,34 @@ http_request_duration_milliseconds_sum{method=\"get\"} 2622
">>, prometheus_text_format:format()).

test_histogram_float(_) ->
prometheus_histogram:new([{name, http_request_duration_seconds},
{labels, [method]},
{buckets, [0.01, 0.1, 0.5, 1, 3]},
{help, "Http Request execution time"},
{duration_unit, false}]),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.95),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.1),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.102),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.15),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.25),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.35),
prometheus_histogram:observe(http_request_duration_seconds, [get], 0.55),
prometheus_histogram:observe(http_request_duration_seconds, [get], 1.55),
prometheus_histogram:observe(http_request_duration_seconds, [get], 2.05),
?_assertEqual(<<"# TYPE http_request_duration_seconds histogram
# HELP http_request_duration_seconds Http Request execution time
http_request_duration_seconds_bucket{method=\"get\",le=\"0.01\"} 0
http_request_duration_seconds_bucket{method=\"get\",le=\"0.1\"} 1
http_request_duration_seconds_bucket{method=\"get\",le=\"0.5\"} 5
http_request_duration_seconds_bucket{method=\"get\",le=\"1\"} 7
http_request_duration_seconds_bucket{method=\"get\",le=\"3\"} 9
http_request_duration_seconds_bucket{method=\"get\",le=\"+Inf\"} 9
http_request_duration_seconds_count{method=\"get\"} 9
http_request_duration_seconds_sum{method=\"get\"} 6.052
">>, prometheus_text_format:format()).

test_dhistogram(_) ->
prometheus_histogram:new([{name, http_request_duration_milliseconds},
{labels, [method]},
Expand Down

0 comments on commit 18e1a45

Please sign in to comment.