Current section

Files

Jump to
themis src internal@metric@histogram.erl
Raw

src/internal@metric@histogram.erl

-module(internal@metric@histogram).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([create_record/3, measure/3, delete_record/2, print/2, new_name/1, new/2]).
-export_type([histogram/0, histogram_record/0, histogram_error/0]).
-type histogram() :: any().
-type histogram_record() :: {histogram_record,
internal@prometheus:number_(),
internal@prometheus:number_(),
gleam@dict:dict(internal@prometheus:number_(), internal@prometheus:number_())}.
-type histogram_error() :: record_not_found |
invalid_na_n_label |
{number_error, themis@number:comparison_error()}.
-file("/git/Themis/src/internal/metric/histogram.gleam", 41).
-spec create_record(
internal@metric:metric(histogram(), histogram_record()),
internal@label:label_set(),
gleam@set:set(internal@prometheus:number_())
) -> {ok, internal@metric:metric(histogram(), histogram_record())} |
{error, histogram_error()}.
create_record(To, Labels, Thresholds) ->
Keys = begin
_pipe = Thresholds,
_pipe@1 = gleam@set:insert(_pipe, pos_inf),
gleam@set:to_list(_pipe@1)
end,
R = (gleam@list:any(Keys, fun(Key) -> Key =:= na_n end)),
gleam@bool:guard(
R,
{error, invalid_na_n_label},
fun() ->
Values = gleam@list:repeat(
themis@number:int(0),
erlang:length(Keys)
),
Buckets = begin
_pipe@2 = gleam@list:zip(Keys, Values),
maps:from_list(_pipe@2)
end,
Record = {histogram_record,
themis@number:int(0),
themis@number:int(0),
Buckets},
{ok,
erlang:setelement(
3,
To,
gleam@dict:insert(erlang:element(3, To), Labels, Record)
)}
end
).
-file("/git/Themis/src/internal/metric/histogram.gleam", 63).
-spec measure(
internal@metric:metric(histogram(), histogram_record()),
internal@label:label_set(),
internal@prometheus:number_()
) -> {ok, internal@metric:metric(histogram(), histogram_record())} |
{error, histogram_error()}.
measure(To, Labels, Value) ->
gleam@result:'try'(
begin
_pipe = gleam_stdlib:map_get(erlang:element(3, To), Labels),
gleam@result:replace_error(_pipe, record_not_found)
end,
fun(Record) ->
R = (gleam@list:try_map(
begin
_pipe@1 = erlang:element(4, Record),
maps:to_list(_pipe@1)
end,
fun(_use0) ->
{Le, Bucket_count} = _use0,
case themis@number:compare(Value, Le) of
{error, E} ->
{error, {number_error, E}};
{ok, lt} ->
{ok,
{Le,
themis@number:add(
Bucket_count,
themis@number:int(1)
)}};
{ok, eq} ->
{ok,
{Le,
themis@number:add(
Bucket_count,
themis@number:int(1)
)}};
{ok, gt} ->
{ok, {Le, Bucket_count}}
end
end
)),
gleam@result:map(
R,
fun(New_record_list) ->
New_record_buckets = maps:from_list(New_record_list),
New_record = {histogram_record,
themis@number:add(
erlang:element(2, Record),
themis@number:int(1)
),
themis@number:add(erlang:element(3, Record), Value),
New_record_buckets},
erlang:setelement(
3,
To,
gleam@dict:insert(
erlang:element(3, To),
Labels,
New_record
)
)
end
)
end
).
-file("/git/Themis/src/internal/metric/histogram.gleam", 93).
-spec delete_record(
internal@metric:metric(histogram(), histogram_record()),
internal@label:label_set()
) -> internal@metric:metric(histogram(), histogram_record()).
delete_record(From, Labels) ->
erlang:setelement(
3,
From,
gleam@dict:delete(erlang:element(3, From), Labels)
).
-file("/git/Themis/src/internal/metric/histogram.gleam", 156).
-spec print_bucket(
binary(),
internal@label:label_set(),
internal@prometheus:number_(),
internal@prometheus:number_()
) -> binary().
print_bucket(Name, Labels, Le, Count) ->
Label_string = begin
_pipe = internal@label:add_label(
Labels,
<<"le"/utf8>>,
internal@prometheus:print(Le)
),
_pipe@1 = gleam@result:lazy_unwrap(
_pipe,
fun() -> erlang:error(#{gleam_error => panic,
message => <<"`le` could not be added as a label"/utf8>>,
module => <<"internal/metric/histogram"/utf8>>,
function => <<"print_bucket"/utf8>>,
line => 164}) end
),
internal@label:print(_pipe@1)
end,
<<<<<<<<<<Name/binary, "_bucket"/utf8>>/binary, Label_string/binary>>/binary,
" "/utf8>>/binary,
(internal@prometheus:print(Count))/binary>>/binary,
"\n"/utf8>>.
-file("/git/Themis/src/internal/metric/histogram.gleam", 118).
-spec print_histogram_record(
binary(),
internal@label:label_set(),
histogram_record()
) -> binary().
print_histogram_record(Name, Labels, Record) ->
Count_line = <<<<<<<<<<Name/binary, "_count"/utf8>>/binary,
(internal@label:print(Labels))/binary>>/binary,
" "/utf8>>/binary,
(internal@prometheus:print(erlang:element(2, Record)))/binary>>/binary,
"\n"/utf8>>,
Sum_line = <<<<<<<<<<Name/binary, "_sum"/utf8>>/binary,
(internal@label:print(Labels))/binary>>/binary,
" "/utf8>>/binary,
(internal@prometheus:print(erlang:element(2, Record)))/binary>>/binary,
"\n"/utf8>>,
Bucket_lines = begin
_pipe = erlang:element(4, Record),
_pipe@1 = maps:to_list(_pipe),
_pipe@2 = gleam@list:sort(
_pipe@1,
fun(Bucket1, Bucket2) ->
{Le1, _} = Bucket1,
{Le2, _} = Bucket2,
gleam@result:lazy_unwrap(
themis@number:compare(Le1, Le2),
fun() -> erlang:error(#{gleam_error => panic,
message => <<"`le` values comparison failed, one of them is NaN"/utf8>>,
module => <<"internal/metric/histogram"/utf8>>,
function => <<"print_histogram_record"/utf8>>,
line => 144}) end
)
end
),
gleam@list:map(
_pipe@2,
fun(Bucket) ->
{Le, Count} = Bucket,
print_bucket(Name, Labels, Le, Count)
end
)
end,
_pipe@3 = lists:append(Bucket_lines, [Sum_line, Count_line]),
_pipe@4 = gleam_stdlib:identity(_pipe@3),
unicode:characters_to_binary(_pipe@4).
-file("/git/Themis/src/internal/metric/histogram.gleam", 100).
-spec print(
internal@metric:metric(histogram(), histogram_record()),
internal@metric:metric_name()
) -> binary().
print(Metric, Name) ->
Name@1 = internal@metric:name_to_string(Name),
Help = <<<<<<"# HELP "/utf8, Name@1/binary>>/binary, " "/utf8>>/binary,
(erlang:element(2, Metric))/binary>>,
Type_ = <<<<"# TYPE "/utf8, Name@1/binary>>/binary, " histogram"/utf8>>,
_pipe = (gleam@dict:fold(
erlang:element(3, Metric),
[<<<<<<Help/binary, "\n"/utf8>>/binary, Type_/binary>>/binary,
"\n"/utf8>>],
fun(Current, Labels, Record) ->
[<<(print_histogram_record(Name@1, Labels, Record))/binary,
"\n"/utf8>> |
Current]
end
)),
_pipe@1 = lists:reverse(_pipe),
_pipe@2 = gleam_stdlib:identity(_pipe@1),
unicode:characters_to_binary(_pipe@2).
-file("/git/Themis/src/internal/metric/histogram.gleam", 169).
-spec new_name(binary()) -> {ok, internal@metric:metric_name()} |
{error, internal@metric:metric_error()}.
new_name(Name) ->
_pipe = Name,
internal@metric:new_name(_pipe, [<<"histogram"/utf8>>]).
-file("/git/Themis/src/internal/metric/histogram.gleam", 27).
-spec new(binary(), binary()) -> {ok,
{internal@metric:metric_name(),
internal@metric:metric(histogram(), histogram_record())}} |
{error, internal@metric:metric_error()}.
new(Name, Description) ->
R = begin
_pipe = Name,
new_name(_pipe)
end,
gleam@result:map(
R,
fun(Name@1) -> {Name@1, {metric, Description, maps:new()}} end
).