TimeLog.TimePlug (time_log v1.3.14)

Copy Markdown

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.

Summary

Functions

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

create http map for logging

Get the correlation_id.

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.

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.

Functions

call(conn, opts)

@spec 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.

check_sanitize_fields(key, origin_list, wildcard)

check_sanitize_list(key, sanitize_list)

create_http_map(conn)

@spec create_http_map(Plug.Conn) :: Map

create http map for logging

from_config(atom)

from_config(atom, default)

get_correlation_id(conn)

@spec get_correlation_id(Plug.Conn) :: String

Get the correlation_id.

get_duration(start_time)

@spec get_duration(DateTime) :: Integer
@spec 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` struct
65

iex> start_time = DateTime.utc_now
...
...
iex> TimeLog.TimePlug.get_duration(start_time) # Request the duration with a `DateTime` struct
65

get_ip(conn)

@spec 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)

@spec log_response(Plug.Conn) :: Plug.Conn

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

sanitize(maps)

sanitize_body(private_map)

sanitize_check(key, sanitize_field)

sanitize_query_string(conn)

sanitize_recursiv(arg, parent, sanitize_list)

sanitize_value(key, value, sanitize)

set_controller_body(conn, body)

@spec 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