| 1 |
= cowboy_metrics_h(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_metrics_h - Metrics stream handler |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The module `cowboy_metrics_h` gathers metrics and |
| 10 |
other information about a stream. It then calls |
| 11 |
the configured callback with this data. |
| 12 |
|
| 13 |
== Types |
| 14 |
|
| 15 |
=== metrics() |
| 16 |
|
| 17 |
[source,erlang] |
| 18 |
---- |
| 19 |
metrics() :: #{ |
| 20 |
%% The identifier for this listener. |
| 21 |
ref := ranch:ref(), |
| 22 |
|
| 23 |
%% The pid for this connection. |
| 24 |
pid := pid(), |
| 25 |
|
| 26 |
%% The streamid also indicates the total number of requests on |
| 27 |
%% this connection (StreamID div 2 + 1). |
| 28 |
streamid := cowboy_stream:streamid(), |
| 29 |
|
| 30 |
%% The terminate reason is always useful. |
| 31 |
reason := cowboy_stream:reason(), |
| 32 |
|
| 33 |
%% A filtered Req object or a partial Req object |
| 34 |
%% depending on how far the request got to. |
| 35 |
req => cowboy_req:req(), |
| 36 |
partial_req => cowboy_stream:partial_req(), |
| 37 |
|
| 38 |
%% Response status. |
| 39 |
resp_status := cowboy:http_status(), |
| 40 |
|
| 41 |
%% Filtered response headers. |
| 42 |
resp_headers := cowboy:http_headers(), |
| 43 |
|
| 44 |
%% Start/end of the processing of the request. |
| 45 |
%% |
| 46 |
%% This represents the time from this stream handler's init |
| 47 |
%% to terminate. |
| 48 |
req_start => integer(), |
| 49 |
req_end => integer(), |
| 50 |
|
| 51 |
%% Start/end of the receiving of the request body. |
| 52 |
%% Begins when the first packet has been received. |
| 53 |
req_body_start => integer(), |
| 54 |
req_body_end => integer(), |
| 55 |
|
| 56 |
%% Start/end of the sending of the response. |
| 57 |
%% Begins when we send the headers and ends on the final |
| 58 |
%% packet of the response body. If everything is sent at |
| 59 |
%% once these values are identical. |
| 60 |
resp_start => integer(), |
| 61 |
resp_end => integer(), |
| 62 |
|
| 63 |
%% For early errors all we get is the time we received it. |
| 64 |
early_error_time => integer(), |
| 65 |
|
| 66 |
%% Start/end of spawned processes. This is where most of |
| 67 |
%% the user code lies, excluding stream handlers. On a |
| 68 |
%% default Cowboy configuration there should be only one |
| 69 |
%% process: the request process. |
| 70 |
procs => ProcMetrics, |
| 71 |
|
| 72 |
%% Informational responses sent before the final response. |
| 73 |
informational => [InformationalMetrics], |
| 74 |
|
| 75 |
%% Length of the request and response bodies. This does |
| 76 |
%% not include the framing. |
| 77 |
req_body_length => non_neg_integer(), |
| 78 |
resp_body_length => non_neg_integer(), |
| 79 |
|
| 80 |
%% Additional metadata set by the user. |
| 81 |
user_data => map() |
| 82 |
} |
| 83 |
|
| 84 |
InformationalMetrics :: #{ |
| 85 |
%% Informational response status. |
| 86 |
status := cowboy:http_status(), |
| 87 |
|
| 88 |
%% Headers sent with the informational response. |
| 89 |
headers := cowboy:http_headers(), |
| 90 |
|
| 91 |
%% Time when the informational response was sent. |
| 92 |
time := integer() |
| 93 |
} |
| 94 |
|
| 95 |
ProcMetrics :: #{pid() => #{ |
| 96 |
%% Time at which the process spawned. |
| 97 |
spawn := integer(), |
| 98 |
|
| 99 |
%% Time at which the process exited. |
| 100 |
exit => integer(), |
| 101 |
|
| 102 |
%% Reason for the process exit. |
| 103 |
reason => any() |
| 104 |
}} |
| 105 |
---- |
| 106 |
|
| 107 |
Metrics given to the callback function. |
| 108 |
|
| 109 |
Depending on the life of the stream the metrics may include |
| 110 |
more or less information. |
| 111 |
|
| 112 |
The `set_options` command can be used to add additional |
| 113 |
metadata in the `user_data` metric. This can be used for |
| 114 |
example to add the handler module which was selected by |
| 115 |
the router. The option to be set is `metrics_user_data`. |
| 116 |
It takes a map which will be merged in the existing |
| 117 |
`user_data` map. |
| 118 |
|
| 119 |
== Options |
| 120 |
|
| 121 |
[source,erlang] |
| 122 |
---- |
| 123 |
opts() :: #{ |
| 124 |
metrics_callback => fun((metrics()) -> any()), |
| 125 |
metrics_req_filter => fun((cowboy_req:req()) -> map()), |
| 126 |
metrics_resp_headers_filter => fun((cowboy:http_headers()) -> cowboy:http_headers()) |
| 127 |
} |
| 128 |
---- |
| 129 |
|
| 130 |
Configuration for the metrics stream handler. |
| 131 |
|
| 132 |
metrics_callback - mandatory:: |
| 133 |
|
| 134 |
The function that will be called upon completion |
| 135 |
of the stream. It only takes a single argument, |
| 136 |
the `metrics()`. |
| 137 |
|
| 138 |
metrics_req_filter:: |
| 139 |
|
| 140 |
A function applied to the Req to compact it and |
| 141 |
only keep required information. By default no |
| 142 |
filtering is done. |
| 143 |
|
| 144 |
metrics_resp_headers_filter:: |
| 145 |
|
| 146 |
A function applied to the response headers to |
| 147 |
filter them and only keep required information. |
| 148 |
By default no filtering is done. |
| 149 |
|
| 150 |
== Events |
| 151 |
|
| 152 |
The metrics stream handler does not produce any event. |
| 153 |
|
| 154 |
== Changelog |
| 155 |
|
| 156 |
* *2.7*: Module introduced. |
| 157 |
|
| 158 |
== See also |
| 159 |
|
| 160 |
link:man:cowboy(7)[cowboy(7)], |
| 161 |
link:man:cowboy_stream(3)[cowboy_stream(3)], |
| 162 |
link:man:cowboy_compress_h(3)[cowboy_compress_h(3)], |
| 163 |
link:man:cowboy_decompress_h(3)[cowboy_decompress_h(3)], |
| 164 |
link:man:cowboy_stream_h(3)[cowboy_stream_h(3)], |
| 165 |
link:man:cowboy_tracer_h(3)[cowboy_tracer_h(3)] |