Skip to content

Commit

Permalink
Document and test histogram's pobserve/6 function
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Mar 9, 2025
1 parent af3adeb commit 36387e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/metrics/prometheus_histogram.erl
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ observe_n(_Registry, _Name, _LabelValues, Value, Count) when is_number(Value) ->
observe_n(_Registry, _Name, _LabelValues, Value, _Count) ->
erlang:error({invalid_value, Value, "observe_n accepts only number values"}).

?DOC(false).
?DOC("""
Observes the given `Value`, `Count` times, in the bucket position of the bucket list.
Useful when the bucket position is known in advance, as it avoids having to compute such value
during the observation.
""").
-spec pobserve(Registry, Name, LabelValues, Buckets, BucketPos, Value) -> ok when
Registry :: prometheus_registry:registry(),
Name :: prometheus_metric:name(),
Expand All @@ -289,7 +294,7 @@ pobserve(Registry, Name, LabelValues, Buckets, BucketPos, Value) when is_integer
insert_metric(Registry, Name, LabelValues, Value, Fun)
end,
ok;
pobserve(Registry, Name, LabelValues, Buckets, BucketPos, Value) when is_number(Value) ->
pobserve(Registry, Name, LabelValues, Buckets, BucketPos, Value) when is_float(Value) ->
Key = key(Registry, Name, LabelValues),
case fobserve_impl(Key, Buckets, BucketPos, Value, 1) of
0 ->
Expand Down
38 changes: 38 additions & 0 deletions test/eunit/metric/prometheus_histogram_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ prometheus_format_test_() ->
fun test_errors/1,
fun test_buckets/1,
fun test_observe/1,
fun test_pobserve/1,
fun test_observe_n/0,
fun test_observe_duration_seconds/1,
fun test_observe_duration_milliseconds/1,
Expand Down Expand Up @@ -274,6 +275,43 @@ test_observe(_) ->
?_assertEqual({[0, 0, 0, 0, 0, 0], 0}, RValue)
].

test_pobserve(_) ->
Name = http_request_duration_milliseconds,
Buckets = [100, 300, 500, 750, 1000, infinity],
prometheus_histogram:new([
{name, Name},
{labels, [method]},
{buckets, Buckets},
{help, "Http Request execution time"},
{duration_unit, false}
]),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 0, 95),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 0, 100),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 1, 102),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 1, 150),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 1, 250),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 0, 75),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 2, 350),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 3, 550),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 4, 950),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 3, 500.2),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 1, 150.4),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 2, 450.5),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 4, 850.3),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 4, 750.9),
prometheus_histogram:pobserve(default, Name, [get], Buckets, 5, 1650.23),
Value = prometheus_histogram:value(Name, [get]),
prometheus_histogram:reset(Name, [get]),
RValue = prometheus_histogram:value(Name, [get]),
[
?_assertMatch(
{[3, 4, 2, 2, 3, 1], Sum} when
Sum > 6974.5 andalso Sum < 6974.55,
Value
),
?_assertEqual({[0, 0, 0, 0, 0, 0], 0}, RValue)
].

test_observe_n() ->
prometheus_histogram:new([{name, temp}, {help, "temp"}, {buckets, [10, 20, 30, 40, 50]}]),
?assertEqual({[0, 0, 0, 0, 0, 0], 0}, prometheus_histogram:value(temp)),
Expand Down

0 comments on commit 36387e6

Please sign in to comment.