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 /test | |
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 'test')
-rw-r--r-- | test/handlers/set_options_h.erl | 7 | ||||
-rw-r--r-- | test/metrics_SUITE.erl | 29 |
2 files changed, 26 insertions, 10 deletions
diff --git a/test/handlers/set_options_h.erl b/test/handlers/set_options_h.erl index 1cefe92..ef88a6f 100644 --- a/test/handlers/set_options_h.erl +++ b/test/handlers/set_options_h.erl @@ -32,4 +32,9 @@ set_options(<<"idle_timeout_long">>, Req0, State) -> #{pid := Pid, streamid := StreamID} = Req0, Pid ! {{Pid, StreamID}, {set_options, #{idle_timeout => 60000}}}, {_, Body, Req} = cowboy_req:read_body(Req0), - {ok, cowboy_req:reply(200, #{}, Body, Req), State}. + {ok, cowboy_req:reply(200, #{}, Body, Req), State}; +set_options(<<"metrics_user_data">>, Req, State) -> + %% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast. + #{pid := Pid, streamid := StreamID} = Req, + Pid ! {{Pid, StreamID}, {set_options, #{metrics_user_data => #{handler => ?MODULE}}}}, + {ok, cowboy_req:reply(200, #{}, <<"Hello world!">>, Req), State}. diff --git a/test/metrics_SUITE.erl b/test/metrics_SUITE.erl index 4ead60c..d9ff7ea 100644 --- a/test/metrics_SUITE.erl +++ b/test/metrics_SUITE.erl @@ -76,6 +76,7 @@ init_routes(_) -> [ {"/default", default_h, []}, {"/full/:key", echo_h, []}, {"/resp/:key[/:arg]", resp_h, []}, + {"/set_options/:key", set_options_h, []}, {"/ws_echo", ws_echo, []} ]} ]. @@ -98,9 +99,13 @@ do_metrics_callback() -> hello_world(Config) -> doc("Confirm metrics are correct for a normal GET request."), - do_get("/", Config). + do_get("/", #{}, Config). -do_get(Path, Config) -> +user_data(Config) -> + doc("Confirm user data can be attached to metrics."), + do_get("/set_options/metrics_user_data", #{handler => set_options_h}, Config). + +do_get(Path, UserData, Config) -> %% Perform a GET request. ConnPid = gun_open(Config), Ref = gun:get(ConnPid, Path, [ @@ -153,7 +158,8 @@ do_get(Path, Config) -> streamid := 1, reason := normal, req := #{}, - informational := [] + informational := [], + user_data := UserData } = Metrics, %% All good! ok @@ -216,7 +222,8 @@ post_body(Config) -> streamid := 1, reason := normal, req := #{}, - informational := [] + informational := [], + user_data := #{} } = Metrics, %% All good! ok @@ -273,7 +280,8 @@ no_resp_body(Config) -> streamid := 1, reason := normal, req := #{}, - informational := [] + informational := [], + user_data := #{} } = Metrics, %% All good! ok @@ -361,7 +369,7 @@ do_early_error_request_line(Config) -> %% This test is identical to normal GET except for the handler. stream_reply(Config) -> doc("Confirm metrics are correct for long polling."), - do_get("/resp/stream_reply2/200", Config). + do_get("/resp/stream_reply2/200", #{}, Config). ws(Config) -> case config(protocol, Config) of @@ -421,7 +429,8 @@ do_ws(Config) -> <<"sec-websocket-accept">> := _ }, time := _ - }] + }], + user_data := #{} } = Metrics, %% All good! ok @@ -487,7 +496,8 @@ error_response(Config) -> streamid := 1, reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'}, req := #{}, - informational := [] + informational := [], + user_data := #{} } = Metrics, %% All good! ok @@ -546,7 +556,8 @@ error_response_after_reply(Config) -> streamid := 1, reason := {internal_error, {'EXIT', _Pid, {crash, _StackTrace}}, 'Stream process crashed.'}, req := #{}, - informational := [] + informational := [], + user_data := #{} } = Metrics, %% All good! ok |