Packages

A compile-time safe database library for Gleam - Ecto-inspired schemas, composable queries, and adapter-based persistence

Current section

Files

Jump to
cquill src cquill@telemetry.erl
Raw

src/cquill@telemetry.erl

-module(cquill@telemetry).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/cquill/telemetry.gleam").
-export([empty_metadata/0, with_string/3, with_int/3, with_bool/3, stop/0, attach/3, detach/1, list_handlers/0, handler_count/0, emit/2, emit_event/1, now_us/0, duration_since/1, timed/1, query_span/5, transaction_span/4, metrics_handler/3, start/0, event_type_name/1, slow_query_handler/1, logger_handler/0, debug_handler/0, source_location/3, query_start/3, query_stop/5, query_exception/5, pool_timeout/3, transaction_start/2, transaction_commit/4, transaction_rollback/4, batch_start/3, batch_stop/4]).
-export_type([source_location/0, event/0, event_type/0, query_start_event/0, query_stop_event/0, query_exception_event/0, pool_checkout_event/0, pool_checkin_event/0, pool_timeout_event/0, transaction_start_event/0, transaction_commit_event/0, transaction_rollback_event/0, savepoint_create_event/0, savepoint_rollback_event/0, savepoint_release_event/0, batch_start_event/0, batch_stop_event/0, attach_error/0, detach_error/0, telemetry_message/0, telemetry_state/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type source_location() :: {source_location, binary(), integer(), binary()}.
-type event() :: {query_start, query_start_event()} |
{query_stop, query_stop_event()} |
{query_exception, query_exception_event()} |
{pool_checkout, pool_checkout_event()} |
{pool_checkin, pool_checkin_event()} |
{pool_timeout, pool_timeout_event()} |
{transaction_start, transaction_start_event()} |
{transaction_commit, transaction_commit_event()} |
{transaction_rollback, transaction_rollback_event()} |
{savepoint_create, savepoint_create_event()} |
{savepoint_rollback, savepoint_rollback_event()} |
{savepoint_release, savepoint_release_event()} |
{batch_start, batch_start_event()} |
{batch_stop, batch_stop_event()}.
-type event_type() :: query_start_type |
query_stop_type |
query_exception_type |
pool_checkout_type |
pool_checkin_type |
pool_timeout_type |
transaction_start_type |
transaction_commit_type |
transaction_rollback_type |
savepoint_create_type |
savepoint_rollback_type |
savepoint_release_type |
batch_start_type |
batch_stop_type.
-type query_start_event() :: {query_start_event,
binary(),
list(cquill@query@ast:value()),
gleam@option:option(source_location()),
integer()}.
-type query_stop_event() :: {query_stop_event,
binary(),
list(cquill@query@ast:value()),
integer(),
integer(),
gleam@option:option(source_location())}.
-type query_exception_event() :: {query_exception_event,
binary(),
list(cquill@query@ast:value()),
cquill@error:adapter_error(),
integer(),
gleam@option:option(source_location())}.
-type pool_checkout_event() :: {pool_checkout_event,
binary(),
integer(),
integer()}.
-type pool_checkin_event() :: {pool_checkin_event, binary(), integer()}.
-type pool_timeout_event() :: {pool_timeout_event,
binary(),
integer(),
integer()}.
-type transaction_start_event() :: {transaction_start_event,
binary(),
gleam@option:option(source_location()),
integer()}.
-type transaction_commit_event() :: {transaction_commit_event,
binary(),
integer(),
integer(),
gleam@option:option(source_location())}.
-type transaction_rollback_event() :: {transaction_rollback_event,
binary(),
integer(),
gleam@option:option(binary()),
gleam@option:option(source_location())}.
-type savepoint_create_event() :: {savepoint_create_event,
binary(),
binary(),
integer()}.
-type savepoint_rollback_event() :: {savepoint_rollback_event,
binary(),
binary(),
gleam@option:option(binary())}.
-type savepoint_release_event() :: {savepoint_release_event, binary(), binary()}.
-type batch_start_event() :: {batch_start_event,
binary(),
binary(),
integer(),
integer()}.
-type batch_stop_event() :: {batch_stop_event,
binary(),
binary(),
integer(),
integer()}.
-type attach_error() :: {handler_already_exists, binary()} |
telemetry_not_running.
-type detach_error() :: {handler_not_found, binary()} |
detach_telemetry_not_running.
-type telemetry_message() :: {attach,
binary(),
list(event_type()),
fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil),
gleam@erlang@process:subject({ok, nil} | {error, attach_error()})} |
{detach,
binary(),
gleam@erlang@process:subject({ok, nil} | {error, detach_error()})} |
{emit, event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())} |
{list_handlers,
gleam@erlang@process:subject(list({binary(), list(event_type())}))} |
{get_handler_count, gleam@erlang@process:subject(integer())}.
-type telemetry_state() :: {telemetry_state,
gleam@dict:dict(binary(), {list(event_type()),
fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil)})}.
-file("src/cquill/telemetry.gleam", 298).
?DOC(" Create empty metadata\n").
-spec empty_metadata() -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
empty_metadata() ->
maps:new().
-file("src/cquill/telemetry.gleam", 303).
?DOC(" Add a string to metadata\n").
-spec with_string(
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
binary(),
binary()
) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
with_string(Metadata, Key, Value) ->
gleam@dict:insert(Metadata, Key, gleam_stdlib:identity(Value)).
-file("src/cquill/telemetry.gleam", 308).
?DOC(" Add an int to metadata\n").
-spec with_int(
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
binary(),
integer()
) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
with_int(Metadata, Key, Value) ->
gleam@dict:insert(Metadata, Key, gleam_stdlib:identity(Value)).
-file("src/cquill/telemetry.gleam", 313).
?DOC(" Add a bool to metadata\n").
-spec with_bool(
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
binary(),
boolean()
) -> gleam@dict:dict(binary(), gleam@dynamic:dynamic_()).
with_bool(Metadata, Key, Value) ->
gleam@dict:insert(Metadata, Key, gleam_stdlib:identity(Value)).
-file("src/cquill/telemetry.gleam", 453).
?DOC(" Stop the telemetry server\n").
-spec stop() -> nil.
stop() ->
cquill_telemetry_ffi:clear_server().
-file("src/cquill/telemetry.gleam", 462).
?DOC(" Attach a telemetry handler for specific event types\n").
-spec attach(
binary(),
list(event_type()),
fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil)
) -> {ok, nil} | {error, attach_error()}.
attach(Handler_id, Events, Handler) ->
case cquill_telemetry_ffi:get_server() of
{ok, Server} ->
gleam@erlang@process:call(
Server,
5000,
fun(Reply_to) ->
{attach, Handler_id, Events, Handler, Reply_to}
end
);
{error, _} ->
{error, telemetry_not_running}
end.
-file("src/cquill/telemetry.gleam", 478).
?DOC(" Detach a handler by ID\n").
-spec detach(binary()) -> {ok, nil} | {error, detach_error()}.
detach(Handler_id) ->
case cquill_telemetry_ffi:get_server() of
{ok, Server} ->
gleam@erlang@process:call(
Server,
5000,
fun(Reply_to) -> {detach, Handler_id, Reply_to} end
);
{error, _} ->
{error, detach_telemetry_not_running}
end.
-file("src/cquill/telemetry.gleam", 488).
?DOC(" List all attached handlers\n").
-spec list_handlers() -> list({binary(), list(event_type())}).
list_handlers() ->
case cquill_telemetry_ffi:get_server() of
{ok, Server} ->
gleam@erlang@process:call(
Server,
5000,
fun(Reply_to) -> {list_handlers, Reply_to} end
);
{error, _} ->
[]
end.
-file("src/cquill/telemetry.gleam", 498).
?DOC(" Get the number of attached handlers\n").
-spec handler_count() -> integer().
handler_count() ->
case cquill_telemetry_ffi:get_server() of
{ok, Server} ->
gleam@erlang@process:call(
Server,
5000,
fun(Reply_to) -> {get_handler_count, Reply_to} end
);
{error, _} ->
0
end.
-file("src/cquill/telemetry.gleam", 508).
?DOC(" Emit an event to all registered handlers\n").
-spec emit(event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil.
emit(Event, Metadata) ->
case cquill_telemetry_ffi:get_server() of
{ok, Server} ->
gleam@erlang@process:send(Server, {emit, Event, Metadata});
{error, _} ->
nil
end.
-file("src/cquill/telemetry.gleam", 518).
?DOC(" Emit an event with empty metadata\n").
-spec emit_event(event()) -> nil.
emit_event(Event) ->
emit(Event, empty_metadata()).
-file("src/cquill/telemetry.gleam", 529).
?DOC(
" Get current monotonic time in microseconds\n"
" Uses Erlang's monotonic time converted to microseconds\n"
).
-spec now_us() -> integer().
now_us() ->
cquill_telemetry_ffi:now_us().
-file("src/cquill/telemetry.gleam", 532).
?DOC(" Calculate duration in microseconds between start time and now\n").
-spec duration_since(integer()) -> integer().
duration_since(Start_time_us) ->
cquill_telemetry_ffi:now_us() - Start_time_us.
-file("src/cquill/telemetry.gleam", 537).
?DOC(" Time a function and return its result along with duration\n").
-spec timed(fun(() -> QNB)) -> {QNB, integer()}.
timed(F) ->
Start = cquill_telemetry_ffi:now_us(),
Result = F(),
Duration = duration_since(Start),
{Result, Duration}.
-file("src/cquill/telemetry.gleam", 549).
?DOC(" Execute a function with query start/stop telemetry\n").
-spec query_span(
binary(),
list(cquill@query@ast:value()),
gleam@option:option(source_location()),
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
fun(() -> {ok, {QNE, integer()}} | {error, cquill@error:adapter_error()})
) -> {ok, {QNE, integer()}} | {error, cquill@error:adapter_error()}.
query_span(Query, Params, Source, Metadata, F) ->
Start_time = cquill_telemetry_ffi:now_us(),
emit(
{query_start, {query_start_event, Query, Params, Source, Start_time}},
Metadata
),
Result = F(),
Duration = duration_since(Start_time),
case Result of
{ok, {Value, Row_count}} ->
emit(
{query_stop,
{query_stop_event,
Query,
Params,
Duration,
Row_count,
Source}},
Metadata
),
{ok, {Value, Row_count}};
{error, Error} ->
emit(
{query_exception,
{query_exception_event,
Query,
Params,
Error,
Duration,
Source}},
Metadata
),
{error, Error}
end.
-file("src/cquill/telemetry.gleam", 605).
?DOC(" Execute a function with transaction start/commit/rollback telemetry\n").
-spec transaction_span(
binary(),
gleam@option:option(source_location()),
gleam@dict:dict(binary(), gleam@dynamic:dynamic_()),
fun(() -> {ok, {QNK, integer()}} | {error, binary()})
) -> {ok, QNK} | {error, binary()}.
transaction_span(Transaction_id, Source, Metadata, F) ->
Start_time = cquill_telemetry_ffi:now_us(),
emit(
{transaction_start,
{transaction_start_event, Transaction_id, Source, Start_time}},
Metadata
),
Result = F(),
Duration = duration_since(Start_time),
case Result of
{ok, {Value, Query_count}} ->
emit(
{transaction_commit,
{transaction_commit_event,
Transaction_id,
Duration,
Query_count,
Source}},
Metadata
),
{ok, Value};
{error, Reason} ->
emit(
{transaction_rollback,
{transaction_rollback_event,
Transaction_id,
Duration,
{some, Reason},
Source}},
Metadata
),
{error, Reason}
end.
-file("src/cquill/telemetry.gleam", 889).
?DOC(" Metrics handler builder - returns a handler that calls the provided callbacks\n").
-spec metrics_handler(
fun((binary(), integer(), integer()) -> nil),
fun((binary()) -> nil),
fun((binary()) -> nil)
) -> fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil).
metrics_handler(On_query_complete, On_query_error, On_pool_timeout) ->
fun(Event, _) -> case Event of
{query_stop, E} ->
On_query_complete(
erlang:element(2, E),
erlang:element(4, E),
erlang:element(5, E)
);
{query_exception, _} ->
On_query_error(<<"query_error"/utf8>>);
{pool_timeout, E@1} ->
On_pool_timeout(erlang:element(2, E@1));
_ ->
nil
end end.
-file("src/cquill/telemetry.gleam", 909).
?DOC(" Convert an event to its type enum\n").
-spec event_to_type(event()) -> event_type().
event_to_type(Event) ->
case Event of
{query_start, _} ->
query_start_type;
{query_stop, _} ->
query_stop_type;
{query_exception, _} ->
query_exception_type;
{pool_checkout, _} ->
pool_checkout_type;
{pool_checkin, _} ->
pool_checkin_type;
{pool_timeout, _} ->
pool_timeout_type;
{transaction_start, _} ->
transaction_start_type;
{transaction_commit, _} ->
transaction_commit_type;
{transaction_rollback, _} ->
transaction_rollback_type;
{savepoint_create, _} ->
savepoint_create_type;
{savepoint_rollback, _} ->
savepoint_rollback_type;
{savepoint_release, _} ->
savepoint_release_type;
{batch_start, _} ->
batch_start_type;
{batch_stop, _} ->
batch_stop_type
end.
-file("src/cquill/telemetry.gleam", 380).
?DOC(
" Start the telemetry server\n"
" Returns Ok(Nil) on success, Error on failure\n"
).
-spec start() -> {ok, nil} | {error, gleam@otp@actor:start_error()}.
start() ->
Initial_state = {telemetry_state, maps:new()},
_pipe = gleam@otp@actor:new(Initial_state),
_pipe@2 = gleam@otp@actor:on_message(_pipe, fun(State, Msg) -> case Msg of
{attach, Handler_id, Events, Handler, Reply_to} ->
case gleam@dict:has_key(
erlang:element(2, State),
Handler_id
) of
true ->
gleam@erlang@process:send(
Reply_to,
{error, {handler_already_exists, Handler_id}}
),
gleam@otp@actor:continue(State);
false ->
New_handlers = gleam@dict:insert(
erlang:element(2, State),
Handler_id,
{Events, Handler}
),
gleam@erlang@process:send(Reply_to, {ok, nil}),
gleam@otp@actor:continue(
{telemetry_state, New_handlers}
)
end;
{detach, Handler_id@1, Reply_to@1} ->
case gleam@dict:has_key(
erlang:element(2, State),
Handler_id@1
) of
true ->
New_handlers@1 = gleam@dict:delete(
erlang:element(2, State),
Handler_id@1
),
gleam@erlang@process:send(Reply_to@1, {ok, nil}),
gleam@otp@actor:continue(
{telemetry_state, New_handlers@1}
);
false ->
gleam@erlang@process:send(
Reply_to@1,
{error, {handler_not_found, Handler_id@1}}
),
gleam@otp@actor:continue(State)
end;
{emit, Event, Metadata} ->
Event_type = event_to_type(Event),
gleam@dict:each(
erlang:element(2, State),
fun(_, Handler_info) ->
{Registered_events, Handler@1} = Handler_info,
case gleam@list:contains(
Registered_events,
Event_type
) of
true ->
Handler@1(Event, Metadata);
false ->
nil
end
end
),
gleam@otp@actor:continue(State);
{list_handlers, Reply_to@2} ->
Handler_list = begin
_pipe@1 = maps:to_list(erlang:element(2, State)),
gleam@list:map(
_pipe@1,
fun(Entry) ->
{Id, {Events@1, _}} = Entry,
{Id, Events@1}
end
)
end,
gleam@erlang@process:send(Reply_to@2, Handler_list),
gleam@otp@actor:continue(State);
{get_handler_count, Reply_to@3} ->
gleam@erlang@process:send(
Reply_to@3,
maps:size(erlang:element(2, State))
),
gleam@otp@actor:continue(State)
end end),
_pipe@3 = gleam@otp@actor:start(_pipe@2),
gleam@result:map(
_pipe@3,
fun(Started) ->
cquill_telemetry_ffi:set_server(erlang:element(3, Started)),
nil
end
).
-file("src/cquill/telemetry.gleam", 929).
?DOC(" Get a human-readable name for an event type\n").
-spec event_type_name(event_type()) -> binary().
event_type_name(Event_type) ->
case Event_type of
query_start_type ->
<<"query.start"/utf8>>;
query_stop_type ->
<<"query.stop"/utf8>>;
query_exception_type ->
<<"query.exception"/utf8>>;
pool_checkout_type ->
<<"pool.checkout"/utf8>>;
pool_checkin_type ->
<<"pool.checkin"/utf8>>;
pool_timeout_type ->
<<"pool.timeout"/utf8>>;
transaction_start_type ->
<<"transaction.start"/utf8>>;
transaction_commit_type ->
<<"transaction.commit"/utf8>>;
transaction_rollback_type ->
<<"transaction.rollback"/utf8>>;
savepoint_create_type ->
<<"savepoint.create"/utf8>>;
savepoint_rollback_type ->
<<"savepoint.rollback"/utf8>>;
savepoint_release_type ->
<<"savepoint.release"/utf8>>;
batch_start_type ->
<<"batch.start"/utf8>>;
batch_stop_type ->
<<"batch.stop"/utf8>>
end.
-file("src/cquill/telemetry.gleam", 949).
?DOC(" Format source location for logging\n").
-spec format_source_location(gleam@option:option(source_location())) -> binary().
format_source_location(Source) ->
case Source of
{some, Loc} ->
<<<<<<<<<<<<" at "/utf8, (erlang:element(2, Loc))/binary>>/binary,
":"/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(3, Loc)))/binary>>/binary,
" in "/utf8>>/binary,
(erlang:element(4, Loc))/binary>>/binary,
"()"/utf8>>;
none ->
<<""/utf8>>
end.
-file("src/cquill/telemetry.gleam", 964).
?DOC(" Truncate a query for display\n").
-spec truncate_query(binary(), integer()) -> binary().
truncate_query(Query, Max_length) ->
Length = string:length(Query),
case Length > Max_length of
true ->
<<(gleam@string:slice(Query, 0, Max_length))/binary, "..."/utf8>>;
false ->
Query
end.
-file("src/cquill/telemetry.gleam", 752).
?DOC(" Slow query handler that logs queries exceeding a threshold\n").
-spec slow_query_handler(integer()) -> fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil).
slow_query_handler(Threshold_ms) ->
Threshold_us = Threshold_ms * 1000,
fun(Event, _) -> case Event of
{query_stop, E} ->
case erlang:element(4, E) > Threshold_us of
true ->
Duration_ms = erlang:element(4, E) div 1000,
Location_str = format_source_location(
erlang:element(6, E)
),
gleam_stdlib:println_error(
erlang:list_to_binary(
[<<"[cquill] SLOW QUERY ("/utf8>>,
erlang:integer_to_binary(Duration_ms),
<<"ms): "/utf8>>,
truncate_query(erlang:element(2, E), 200),
Location_str]
)
);
false ->
nil
end;
_ ->
nil
end end.
-file("src/cquill/telemetry.gleam", 973).
?DOC(" Format an adapter error for logging (simplified)\n").
-spec format_adapter_error(cquill@error:adapter_error()) -> binary().
format_adapter_error(Err) ->
case Err of
not_found ->
<<"NotFound"/utf8>>;
{too_many_rows, Expected, Got} ->
<<<<<<<<"TooManyRows(expected="/utf8,
(erlang:integer_to_binary(Expected))/binary>>/binary,
", got="/utf8>>/binary,
(erlang:integer_to_binary(Got))/binary>>/binary,
")"/utf8>>;
{connection_failed, Reason} ->
<<"ConnectionFailed: "/utf8, Reason/binary>>;
connection_timeout ->
<<"ConnectionTimeout"/utf8>>;
pool_exhausted ->
<<"PoolExhausted"/utf8>>;
{connection_lost, Reason@1} ->
<<"ConnectionLost: "/utf8, Reason@1/binary>>;
{query_failed, Message, _} ->
<<"QueryFailed: "/utf8, Message/binary>>;
{decode_failed, Row, Column, Expected@1, Got@1} ->
<<<<<<<<<<<<<<<<"DecodeFailed at row "/utf8,
(erlang:integer_to_binary(Row))/binary>>/binary,
", column "/utf8>>/binary,
Column/binary>>/binary,
" (expected "/utf8>>/binary,
Expected@1/binary>>/binary,
", got "/utf8>>/binary,
Got@1/binary>>/binary,
")"/utf8>>;
timeout ->
<<"Timeout"/utf8>>;
{unique_violation, Constraint, _} ->
<<"UniqueViolation: "/utf8, Constraint/binary>>;
{foreign_key_violation, Constraint@1, _} ->
<<"ForeignKeyViolation: "/utf8, Constraint@1/binary>>;
{check_violation, Constraint@2, _} ->
<<"CheckViolation: "/utf8, Constraint@2/binary>>;
{not_null_violation, Column@1} ->
<<"NotNullViolation: "/utf8, Column@1/binary>>;
{constraint_violation, Constraint@3, _} ->
<<"ConstraintViolation: "/utf8, Constraint@3/binary>>;
{stale_data, Expected@2, Actual} ->
<<<<<<<<"StaleData(expected="/utf8, Expected@2/binary>>/binary,
", actual="/utf8>>/binary,
Actual/binary>>/binary,
")"/utf8>>;
{data_integrity_error, Message@1} ->
<<"DataIntegrityError: "/utf8, Message@1/binary>>;
{not_supported, Operation} ->
<<"NotSupported: "/utf8, Operation/binary>>;
{adapter_specific, Code, Message@2} ->
<<<<<<"AdapterSpecific["/utf8, Code/binary>>/binary, "]: "/utf8>>/binary,
Message@2/binary>>
end.
-file("src/cquill/telemetry.gleam", 661).
?DOC(" Logger handler that prints query events to stdout/stderr\n").
-spec logger_handler() -> fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil).
logger_handler() ->
fun(Event, _) -> case Event of
{query_stop, E} ->
Duration_ms = erlang:element(4, E) div 1000,
Location_str = format_source_location(erlang:element(6, E)),
gleam_stdlib:println(
erlang:list_to_binary(
[<<"[cquill] "/utf8>>,
truncate_query(erlang:element(2, E), 100),
<<" ["/utf8>>,
erlang:integer_to_binary(Duration_ms),
<<"ms, "/utf8>>,
erlang:integer_to_binary(erlang:element(5, E)),
<<" rows]"/utf8>>,
Location_str]
)
);
{query_exception, E@1} ->
Duration_ms@1 = erlang:element(5, E@1) div 1000,
Location_str@1 = format_source_location(erlang:element(6, E@1)),
gleam_stdlib:println_error(
erlang:list_to_binary(
[<<"[cquill] QUERY ERROR: "/utf8>>,
truncate_query(erlang:element(2, E@1), 100),
<<" - "/utf8>>,
format_adapter_error(erlang:element(4, E@1)),
<<" ["/utf8>>,
erlang:integer_to_binary(Duration_ms@1),
<<"ms]"/utf8>>,
Location_str@1]
)
);
{transaction_commit, E@2} ->
Duration_ms@2 = erlang:element(3, E@2) div 1000,
gleam_stdlib:println(
erlang:list_to_binary(
[<<"[cquill] TRANSACTION COMMIT: "/utf8>>,
erlang:element(2, E@2),
<<" ["/utf8>>,
erlang:integer_to_binary(Duration_ms@2),
<<"ms, "/utf8>>,
erlang:integer_to_binary(erlang:element(4, E@2)),
<<" queries]"/utf8>>]
)
);
{transaction_rollback, E@3} ->
Duration_ms@3 = erlang:element(3, E@3) div 1000,
Reason_str = case erlang:element(4, E@3) of
{some, R} ->
<<" - "/utf8, R/binary>>;
none ->
<<""/utf8>>
end,
gleam_stdlib:println_error(
erlang:list_to_binary(
[<<"[cquill] TRANSACTION ROLLBACK: "/utf8>>,
erlang:element(2, E@3),
Reason_str,
<<" ["/utf8>>,
erlang:integer_to_binary(Duration_ms@3),
<<"ms]"/utf8>>]
)
);
{pool_timeout, E@4} ->
Wait_ms = erlang:element(3, E@4) div 1000,
gleam_stdlib:println_error(
erlang:list_to_binary(
[<<"[cquill] POOL TIMEOUT: "/utf8>>,
erlang:element(2, E@4),
<<" after "/utf8>>,
erlang:integer_to_binary(Wait_ms),
<<"ms (queue: "/utf8>>,
erlang:integer_to_binary(erlang:element(4, E@4)),
<<")"/utf8>>]
)
);
_ ->
nil
end end.
-file("src/cquill/telemetry.gleam", 780).
?DOC(" Debug handler that logs all events\n").
-spec debug_handler() -> fun((event(), gleam@dict:dict(binary(), gleam@dynamic:dynamic_())) -> nil).
debug_handler() ->
fun(Event, Metadata) ->
Event_name = event_type_name(event_to_type(Event)),
Metadata_str = case maps:size(Metadata) > 0 of
true ->
<<" metadata="/utf8,
(gleam@string:inspect(maps:to_list(Metadata)))/binary>>;
false ->
<<""/utf8>>
end,
gleam_stdlib:println(
erlang:list_to_binary(
[<<"[cquill:debug] "/utf8>>, Event_name, Metadata_str]
)
),
case Event of
{query_start, E} ->
gleam_stdlib:println(
<<" query: "/utf8,
(truncate_query(erlang:element(2, E), 150))/binary>>
);
{query_stop, E@1} ->
gleam_stdlib:println(
<<<<<<<<<<" query: "/utf8,
(truncate_query(
erlang:element(2, E@1),
150
))/binary>>/binary,
" duration="/utf8>>/binary,
(erlang:integer_to_binary(
erlang:element(4, E@1)
))/binary>>/binary,
"us rows="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(5, E@1)))/binary>>
);
{query_exception, E@2} ->
gleam_stdlib:println(
<<<<<<" query: "/utf8,
(truncate_query(erlang:element(2, E@2), 150))/binary>>/binary,
" error="/utf8>>/binary,
(format_adapter_error(erlang:element(4, E@2)))/binary>>
);
{transaction_start, E@3} ->
gleam_stdlib:println(
<<" transaction_id: "/utf8,
(erlang:element(2, E@3))/binary>>
);
{transaction_commit, E@4} ->
gleam_stdlib:println(
<<<<<<<<<<" transaction_id: "/utf8,
(erlang:element(2, E@4))/binary>>/binary,
" duration="/utf8>>/binary,
(erlang:integer_to_binary(
erlang:element(3, E@4)
))/binary>>/binary,
"us queries="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(4, E@4)))/binary>>
);
{transaction_rollback, E@5} ->
gleam_stdlib:println(
<<<<<<" transaction_id: "/utf8,
(erlang:element(2, E@5))/binary>>/binary,
" reason="/utf8>>/binary,
(gleam@option:unwrap(
erlang:element(4, E@5),
<<"unknown"/utf8>>
))/binary>>
);
{pool_checkout, E@6} ->
gleam_stdlib:println(
<<<<<<<<<<" pool: "/utf8, (erlang:element(2, E@6))/binary>>/binary,
" wait="/utf8>>/binary,
(erlang:integer_to_binary(
erlang:element(3, E@6)
))/binary>>/binary,
"us queue="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(4, E@6)))/binary>>
);
{pool_checkin, E@7} ->
gleam_stdlib:println(
<<<<<<<<" pool: "/utf8, (erlang:element(2, E@7))/binary>>/binary,
" usage="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(3, E@7)))/binary>>/binary,
"us"/utf8>>
);
{pool_timeout, E@8} ->
gleam_stdlib:println(
<<<<<<<<<<" pool: "/utf8, (erlang:element(2, E@8))/binary>>/binary,
" wait="/utf8>>/binary,
(erlang:integer_to_binary(
erlang:element(3, E@8)
))/binary>>/binary,
"us queue="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(4, E@8)))/binary>>
);
{savepoint_create, E@9} ->
gleam_stdlib:println(
<<<<<<" savepoint: "/utf8,
(erlang:element(3, E@9))/binary>>/binary,
" in tx="/utf8>>/binary,
(erlang:element(2, E@9))/binary>>
);
{savepoint_rollback, E@10} ->
gleam_stdlib:println(
<<<<<<" savepoint: "/utf8,
(erlang:element(3, E@10))/binary>>/binary,
" in tx="/utf8>>/binary,
(erlang:element(2, E@10))/binary>>
);
{savepoint_release, E@11} ->
gleam_stdlib:println(
<<<<<<" savepoint: "/utf8,
(erlang:element(3, E@11))/binary>>/binary,
" in tx="/utf8>>/binary,
(erlang:element(2, E@11))/binary>>
);
{batch_start, E@12} ->
gleam_stdlib:println(
<<<<<<<<<<" batch: "/utf8,
(erlang:element(2, E@12))/binary>>/binary,
" on "/utf8>>/binary,
(erlang:element(3, E@12))/binary>>/binary,
" size="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(4, E@12)))/binary>>
);
{batch_stop, E@13} ->
gleam_stdlib:println(
<<<<<<<<<<<<<<<<" batch: "/utf8,
(erlang:element(2, E@13))/binary>>/binary,
" on "/utf8>>/binary,
(erlang:element(3, E@13))/binary>>/binary,
" affected="/utf8>>/binary,
(erlang:integer_to_binary(
erlang:element(4, E@13)
))/binary>>/binary,
" duration="/utf8>>/binary,
(erlang:integer_to_binary(erlang:element(5, E@13)))/binary>>/binary,
"us"/utf8>>
)
end
end.
-file("src/cquill/telemetry.gleam", 1021).
?DOC(" Create a source location\n").
-spec source_location(binary(), integer(), binary()) -> source_location().
source_location(File, Line, Function) ->
{source_location, File, Line, Function}.
-file("src/cquill/telemetry.gleam", 1030).
?DOC(" Create a query start event\n").
-spec query_start(
binary(),
list(cquill@query@ast:value()),
gleam@option:option(source_location())
) -> event().
query_start(Query, Params, Source) ->
{query_start,
{query_start_event,
Query,
Params,
Source,
cquill_telemetry_ffi:now_us()}}.
-file("src/cquill/telemetry.gleam", 1044).
?DOC(" Create a query stop event\n").
-spec query_stop(
binary(),
list(cquill@query@ast:value()),
integer(),
integer(),
gleam@option:option(source_location())
) -> event().
query_stop(Query, Params, Duration_us, Row_count, Source) ->
{query_stop,
{query_stop_event, Query, Params, Duration_us, Row_count, Source}}.
-file("src/cquill/telemetry.gleam", 1061).
?DOC(" Create a query exception event\n").
-spec query_exception(
binary(),
list(cquill@query@ast:value()),
cquill@error:adapter_error(),
integer(),
gleam@option:option(source_location())
) -> event().
query_exception(Query, Params, Err, Duration_us, Source) ->
{query_exception,
{query_exception_event, Query, Params, Err, Duration_us, Source}}.
-file("src/cquill/telemetry.gleam", 1078).
?DOC(" Create a pool timeout event\n").
-spec pool_timeout(binary(), integer(), integer()) -> event().
pool_timeout(Pool_name, Wait_time_us, Queue_length) ->
{pool_timeout, {pool_timeout_event, Pool_name, Wait_time_us, Queue_length}}.
-file("src/cquill/telemetry.gleam", 1091).
?DOC(" Create a transaction start event\n").
-spec transaction_start(binary(), gleam@option:option(source_location())) -> event().
transaction_start(Transaction_id, Source) ->
{transaction_start,
{transaction_start_event,
Transaction_id,
Source,
cquill_telemetry_ffi:now_us()}}.
-file("src/cquill/telemetry.gleam", 1103).
?DOC(" Create a transaction commit event\n").
-spec transaction_commit(
binary(),
integer(),
integer(),
gleam@option:option(source_location())
) -> event().
transaction_commit(Transaction_id, Duration_us, Query_count, Source) ->
{transaction_commit,
{transaction_commit_event,
Transaction_id,
Duration_us,
Query_count,
Source}}.
-file("src/cquill/telemetry.gleam", 1118).
?DOC(" Create a transaction rollback event\n").
-spec transaction_rollback(
binary(),
integer(),
gleam@option:option(binary()),
gleam@option:option(source_location())
) -> event().
transaction_rollback(Transaction_id, Duration_us, Reason, Source) ->
{transaction_rollback,
{transaction_rollback_event,
Transaction_id,
Duration_us,
Reason,
Source}}.
-file("src/cquill/telemetry.gleam", 1133).
?DOC(" Create a batch start event\n").
-spec batch_start(binary(), binary(), integer()) -> event().
batch_start(Operation, Table, Batch_size) ->
{batch_start,
{batch_start_event,
Operation,
Table,
Batch_size,
cquill_telemetry_ffi:now_us()}}.
-file("src/cquill/telemetry.gleam", 1143).
?DOC(" Create a batch stop event\n").
-spec batch_stop(binary(), binary(), integer(), integer()) -> event().
batch_stop(Operation, Table, Affected_count, Duration_us) ->
{batch_stop,
{batch_stop_event, Operation, Table, Affected_count, Duration_us}}.