Skip to content

Commit

Permalink
feat: add request start event (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
javiergarea authored Oct 10, 2024
1 parent d1eaff7 commit 6fb53c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
26 changes: 22 additions & 4 deletions src/erf_http_server/erf_http_server_elli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

%%% BEHAVIOURS
-behaviour(erf_http_server).
-behaviour(elli_handler).

%%% INCLUDE FILES
-include_lib("kernel/include/logger.hrl").
Expand All @@ -28,6 +29,7 @@

%%% ELLI HANDLER EXPORTS
-export([
init/2,
handle/2,
handle_event/3
]).
Expand Down Expand Up @@ -67,6 +69,19 @@ start_link(Name, Conf, ExtraConf) ->
%%%-----------------------------------------------------------------------------
%%% ELLI HANDLER EXPORTS
%%%-----------------------------------------------------------------------------
-spec init(InitialRequest, CallbackArgs) -> Result when
InitialRequest :: elli:req(),
CallbackArgs :: [Name :: atom()],
Result :: {ok, Behaviour :: standard}.
init(Req, [Name]) ->
erf_telemetry:event(
{request_start, #{monotonic_time => erlang:monotonic_time()}},
Name,
preprocess(Name, Req),
undefined
),
{ok, standard}.

-spec handle(InitialRequest, CallbackArgs) -> Result when
InitialRequest :: elli:req(),
CallbackArgs :: [Name :: atom()],
Expand All @@ -87,7 +102,7 @@ handle(ElliRequest, [Name]) ->
handle_event(request_complete, Args, CallbackArgs) ->
handle_full_response(request_complete, Args, CallbackArgs);
handle_event(chunk_complete, Args, CallbackArgs) ->
handle_full_response(chunk_complete, Args, CallbackArgs);
handle_full_response(request_complete, Args, CallbackArgs);
handle_event(invalid_return, [Request, Unexpected], CallbackArgs) ->
handle_exception(Request, Unexpected, CallbackArgs);
handle_event(request_throw, [Request, Exception, Stacktrace], [Name]) ->
Expand Down Expand Up @@ -172,6 +187,7 @@ duration(StartKey, EndKey, Timings) ->
handle_full_response(Event, [RawReq, StatusCode, Hs, Body, {Timings, Sizes}], [Name]) ->
Metrics = #{
duration => duration(Timings, request),
monotonic_time => erlang:monotonic_time(),
req_body_duration => duration(Timings, req_body),
req_body_length => size(Sizes, request_body),
resp_body_length => size(Sizes, response_body),
Expand All @@ -187,14 +203,16 @@ handle_full_response(Event, [RawReq, StatusCode, Hs, Body, {Timings, Sizes}], [N
handle_exception(RawReq, [Exception, Stacktrace], [Name]) ->
Req = preprocess(Name, RawReq),
ExceptionData = #{
stacktrace => erlang:list_to_binary(io_lib:format("~p", [Stacktrace])),
error => erlang:list_to_binary(io_lib:format("~p", [Exception]))
error => erlang:list_to_binary(io_lib:format("~p", [Exception])),
monotonic_time => erlang:monotonic_time(),
stacktrace => erlang:list_to_binary(io_lib:format("~p", [Stacktrace]))
},
erf_telemetry:event({request_exception, ExceptionData}, Name, Req, {500, [], undefined});
handle_exception(RawReq, Unexpected, [Name]) ->
Req = preprocess(Name, RawReq),
ExceptionData = #{
error => erlang:list_to_binary(io_lib:format("~p", [Unexpected]))
error => erlang:list_to_binary(io_lib:format("~p", [Unexpected])),
monotonic_time => erlang:monotonic_time()
},
erf_telemetry:event({request_exception, ExceptionData}, Name, Req, {500, [], undefined}).

Expand Down
30 changes: 19 additions & 11 deletions src/erf_telemetry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@

%%% TYPES
-type event() ::
{request_complete, req_measurements()}
| {chunk_complete, req_measurements()}
{request_start, #{monotonic_time := monotonic_time()}}
| {request_complete, req_measurements()}
| {request_exception, exception_data()}.

-type exception_data() :: #{
error := binary(),
monotonic_time := monotonic_time(),
stacktrace => binary()
}.

-type monotonic_time() :: integer().
-type req_measurements() :: #{
duration := integer(),
monotonic_time := monotonic_time(),
req_body_duration => integer(),
req_body_length => integer(),
resp_body_length => integer(),
Expand All @@ -43,6 +45,7 @@
-export_type([
event/0,
exception_data/0,
monotonic_time/0,
req_measurements/0
]).

Expand All @@ -53,7 +56,7 @@
Event :: event(),
Name :: atom(),
Req :: erf:request(),
Resp :: erf:response().
Resp :: undefined | erf:response().
event({request_exception, ExceptionData} = Event, Name, Req, Resp) ->
case code:is_loaded(telemetry) of
{file, _TelemetryBeam} ->
Expand All @@ -80,17 +83,22 @@ event({_EventName, Measurements} = Event, Name, Req, Resp) ->
%%%-----------------------------------------------------------------------------
%%% INTERNAL FUNCTIONS
%%%-----------------------------------------------------------------------------
metadata(Name, Req, undefined, RawMetadata) ->
RawMetadata#{
name => Name,
req => Req
};
metadata(Name, Req, {RespStatus, RespHeaders, _Body}, RawMetadata) ->
RawMetadata#{
name => Name,
req => Req,
resp_headers => RespHeaders,
resp_status => RespStatus,
name => Name
resp_status => RespStatus
}.

metric({request_complete, _}) ->
[erf, request, stop];
metric({chunk_complete, _}) ->
metric({request_complete, _Measurements}) ->
[erf, request, stop];
metric({request_exception, _}) ->
[erf, request, fail].
metric({request_exception, _ExceptionData}) ->
[erf, request, fail];
metric({request_start, _Measurements}) ->
[erf, request, start].

0 comments on commit 6fb53c7

Please sign in to comment.