diff options
author | Loïc Hoguin <[email protected]> | 2019-10-02 15:23:23 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-10-02 15:23:23 +0200 |
commit | f673e191b30ab440440c924476bb03000fff52c6 (patch) | |
tree | 0f74c7940c1a8f38ef1ca4e5e86552924072eccf /src | |
parent | a14ecf19c68ba5b9eb828a41356b1adbc1c5739c (diff) | |
download | cowboy-f673e191b30ab440440c924476bb03000fff52c6.tar.gz cowboy-f673e191b30ab440440c924476bb03000fff52c6.tar.bz2 cowboy-f673e191b30ab440440c924476bb03000fff52c6.zip |
Add {set_options, #{metrics_user_data := Map}}
This allows giving custom metadata to the metrics stream handler.
This can be useful to for example provide the name of the
module handling the request which is only known after routing.
But any user data is allowed.
When called multiple times the user data maps are merged.
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_metrics_h.erl | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/cowboy_metrics_h.erl b/src/cowboy_metrics_h.erl index 309afde..4107aac 100644 --- a/src/cowboy_metrics_h.erl +++ b/src/cowboy_metrics_h.erl @@ -102,7 +102,10 @@ %% Length of the request and response bodies. This does %% not include the framing. req_body_length => non_neg_integer(), - resp_body_length => non_neg_integer() + resp_body_length => non_neg_integer(), + + %% Additional metadata set by the user. + user_data => map() }. -export_type([metrics/0]). @@ -126,7 +129,8 @@ procs = #{} :: proc_metrics(), informational = [] :: [informational_metrics()], req_body_length = 0 :: non_neg_integer(), - resp_body_length = 0 :: non_neg_integer() + resp_body_length = 0 :: non_neg_integer(), + user_data = #{} :: map() }). -spec init(cowboy_stream:streamid(), cowboy_req:req(), cowboy:opts()) @@ -255,6 +259,14 @@ fold([{data, fin, Data}|Tail], State=#state{resp_body_length=RespBodyLen}) -> resp_end=RespEnd, resp_body_length=RespBodyLen + resp_body_length(Data) }); +fold([{set_options, SetOpts}|Tail], State0=#state{user_data=OldUserData}) -> + State = case SetOpts of + #{metrics_user_data := NewUserData} -> + State0#state{user_data=maps:merge(OldUserData, NewUserData)}; + _ -> + State0 + end, + fold(Tail, State); fold([_|Tail], State) -> fold(Tail, State). @@ -263,7 +275,7 @@ terminate(StreamID, Reason, #state{next=Next, callback=Fun, req=Req, resp_status=RespStatus, resp_headers=RespHeaders, ref=Ref, req_start=ReqStart, req_body_start=ReqBodyStart, req_body_end=ReqBodyEnd, resp_start=RespStart, resp_end=RespEnd, - procs=Procs, informational=Infos, + procs=Procs, informational=Infos, user_data=UserData, req_body_length=ReqBodyLen, resp_body_length=RespBodyLen}) -> Res = cowboy_stream:terminate(StreamID, Reason, Next), ReqEnd = erlang:monotonic_time(), @@ -284,7 +296,8 @@ terminate(StreamID, Reason, #state{next=Next, callback=Fun, procs => Procs, informational => lists:reverse(Infos), req_body_length => ReqBodyLen, - resp_body_length => RespBodyLen + resp_body_length => RespBodyLen, + user_data => UserData }, Fun(Metrics), Res. |