time_log v1.1.7 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
Get the correlation_id
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
call(conn, opts)
call(Plug.Conn, Map) :: Plug.Conn
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.
from_config(atom)
from_config(atom, default)
get_correlation_id(conn)
get_correlation_id(Plug.Conn) :: String
get_correlation_id(Plug.Conn) :: String
Get the correlation_id.
get_duration(start_time)
get_duration(DateTime) :: Integer
get_duration(Plug.Conn) :: Integer
get_duration(DateTime) :: Integer
get_duration(Plug.Conn) :: Integer
Returns the time in ms the request took so far.
May be called with either a DateTime struct DateTime
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.7.1/Plug.Conn.html) struct
65
iex> start_time = DateTime.utc_now
...
...
iex> TimeLog.TimePlug.get_duration(start_time) # Request the duration with a [`DateTime`](https://hexdocs.pm/elixir/DateTime.html) struct
65
get_ip(conn)
get_ip(Plug.Conn) :: String
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"]
).
log_response(conn)
log_response(Plug.Conn) :: Plug.Conn
log_response(Plug.Conn) :: Plug.Conn
Log the response including the IP address, correlation_id and headers.
sanitize(maps)
sanitize_query_string(conn)
set_controller_body(conn, body)
set_controller_body(Plug.Conn, Map) :: Plug.Conn
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