Current section

Files

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

src/internal@metric@counter.erl

-module(internal@metric@counter).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([create_record/2, increment_by/3, increment/2, delete_record/2, print/2, new_name/1, new/2]).
-export_type([counter/0, counter_error/0]).
-type counter() :: any().
-type counter_error() :: record_already_exists |
record_not_found |
{name_error, internal@metric:metric_error()}.
-file("/git/Themis/src/internal/metric/counter.gleam", 34).
-spec create_record(
internal@metric:metric(counter(), internal@prometheus:number_()),
internal@label:label_set()
) -> {ok, internal@metric:metric(counter(), internal@prometheus:number_())} |
{error, counter_error()}.
create_record(From, Labels) ->
gleam@bool:guard(
gleam@dict:has_key(erlang:element(3, From), Labels),
{error, record_already_exists},
fun() ->
{ok,
erlang:setelement(
3,
From,
begin
_pipe = erlang:element(3, From),
gleam@dict:insert(_pipe, Labels, {int, 0})
end
)}
end
).
-file("/git/Themis/src/internal/metric/counter.gleam", 57).
-spec increment_by(
internal@metric:metric(counter(), internal@prometheus:number_()),
internal@label:label_set(),
internal@prometheus:number_()
) -> {ok, internal@metric:metric(counter(), internal@prometheus:number_())} |
{error, counter_error()}.
increment_by(From, Labels, By) ->
New_val_result = case gleam_stdlib:map_get(erlang:element(3, From), Labels) of
{error, _} ->
{error, record_not_found};
{ok, Number} ->
{ok, themis@number:add(Number, By)}
end,
gleam@result:map(
New_val_result,
fun(New_val) ->
erlang:setelement(
3,
From,
begin
_pipe = erlang:element(3, From),
gleam@dict:insert(_pipe, Labels, New_val)
end
)
end
).
-file("/git/Themis/src/internal/metric/counter.gleam", 50).
-spec increment(
internal@metric:metric(counter(), internal@prometheus:number_()),
internal@label:label_set()
) -> {ok, internal@metric:metric(counter(), internal@prometheus:number_())} |
{error, counter_error()}.
increment(From, Labels) ->
increment_by(From, Labels, {int, 1}).
-file("/git/Themis/src/internal/metric/counter.gleam", 70).
-spec delete_record(
internal@metric:metric(counter(), internal@prometheus:number_()),
internal@label:label_set()
) -> internal@metric:metric(counter(), internal@prometheus:number_()).
delete_record(From, Labels) ->
erlang:setelement(
3,
From,
gleam@dict:delete(erlang:element(3, From), Labels)
).
-file("/git/Themis/src/internal/metric/counter.gleam", 77).
-spec print(
internal@metric:metric(counter(), internal@prometheus:number_()),
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, " counter"/utf8>>,
_pipe = (gleam@dict:fold(
erlang:element(3, Metric),
[<<<<<<Help/binary, "\n"/utf8>>/binary, Type_/binary>>/binary,
"\n"/utf8>>],
fun(Current, Labels, Value) ->
[<<<<<<<<Name@1/binary, (internal@label:print(Labels))/binary>>/binary,
" "/utf8>>/binary,
(internal@prometheus:print(Value))/binary>>/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/counter.gleam", 98).
-spec new_name(binary()) -> {ok, internal@metric:metric_name()} |
{error, internal@metric:metric_error()}.
new_name(Name) ->
_pipe = (<<Name/binary, "_total"/utf8>>),
internal@metric:new_name(_pipe, [<<"counter"/utf8>>]).
-file("/git/Themis/src/internal/metric/counter.gleam", 23).
-spec new(binary(), binary()) -> {ok,
{internal@metric:metric_name(),
internal@metric:metric(counter(), internal@prometheus:number_())}} |
{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
).