time_log v0.1.5-rc TimeLog.TimePlug

This plug stops the time a request takes. Also, this module delivers additional useful functionality The plug needs to be called as early in the endpoint.ex as possible, ideally right after the socked received a request. Additionally the get_duration function can be called from everywhere in order to know how long the request took so far. Allows calls to get_ip from other apps too.

Link to this section Summary

Functions

Call to TimePlug that initiates the duration measurement and schedules Response logging to take place before sending the response

Returns the time in ms the request took so far. May be called with either a Time struct Time or with the current conn Plug.Conn

Returns the IP address in a Format fit for logging. If set in config the IP addresses retourned are anonymized by replacing the last digits with ‘XXX’. Requests that use internal (e.g. company) Ip addresses can be filtered if set in config(config :time_log, internal_ips: ["127.0.0.1"])

Log the response including the IP address, correlation_id and headers

Sets the body to be logged in the conn in the private part for the key :controller_body. The body will be included in the response logs if present

Link to this section Functions

Link to this function call(conn, opts)
call(Plug.Conn, Map) :: Plug.Conn

Call to TimePlug that initiates the duration measurement and schedules Response logging to take place before sending the response.

Link to this function get_duration(start_time)
get_duration(Plug.Conn) :: Time
get_duration(Time) :: Integer

Returns the time in ms the request took so far. May be called with either a Time struct Time or with the current conn Plug.Conn.

Examples

iex> TimeLog.TimePlug.get_duration(conn) # Request the duration with a [`Plug.Conn`](https://hexdocs.pm/plug/1.4.3/Plug.Conn.html) struct
65

iex> start_time = Time.utc_now
...
...
iex> TimeLog.TimePlug.get_duration(start_time) # Request the duration with a [`Time`](https://hexdocs.pm/elixir/Time.html) struct
65
Link to this function get_ip(conn)
get_ip(Plug.Conn) :: String

Returns the IP address in a Format fit for logging. If set in config the IP addresses retourned are anonymized by replacing the last digits with ‘XXX’. Requests that use internal (e.g. company) Ip addresses can be filtered if set in config(config :time_log, internal_ips: ["127.0.0.1"]).

Link to this function log_response(conn)
log_response(Plug.Conn) :: Plug.Conn

Log the response including the IP address, correlation_id and headers.

Link to this function set_controller_body(conn, body)
set_controller_body(Plug.Conn, Map) :: Plug.Conn

Sets the body to be logged in the conn in the private part for the key :controller_body. The body will be included in the response logs if present.

Example

In a REST Api function that is called to send the result JSON. The output paramter may be any Map or Struct.

defp send_response(conn, output) do
  conn
  |> TimeLog.TimePlug.set_controller_body(output)
  |> json(output)
end