Timber v1.1.11 Timber.Events.HTTPClientRequestEvent

The HTTPClientRequestEvent tracks outgoing HTTP requests giving you structured insight into communication with external services.

This event is HTTP client agnostic, use it with your HTTP client of choice.

Hackney Example

req_method = :get
req_url = "https://some.api.com/path?query=1"
req_headers = [{"Accept", "application/json"}]
req_body = "{"example": "payload"}"

# Log the outgoing request
{req_event, req_message} = Timber.Events.HTTPClientRequestEvent.new_with_message(method: req_method, url: req_url, headers: req_headers, body: req_body)
Logger.info req_message, event: req_event

# Make the request
timer = Timber.Timer.start()
{:ok, status, headers, body} = :hackney.request(req_method, req_url, req_headers, "")

# Log the response
time_ms = Timber.duration_ms(timer)
{resp_event, resp_message} = Timber.Events.HTTPClientResponseEvent.new(headers: headers, body: body, status: status, time_ms: time_ms)
Logger.info resp_message, event: resp_event

Summary

Functions

Message to be used when logging. The format looks like

Builds a new struct taking care to

Convenience methods for creating an event and getting the message at the same time

Types

t()
t() :: %Timber.Events.HTTPClientRequestEvent{body: String.t | nil, headers: map | nil, host: String.t, method: String.t, path: String.t, port: pos_integer | nil, query_string: String.t | nil, request_id: String.t | nil, scheme: String.t, service_name: String.t | nil}

Functions

message(h_t_t_p_client_request_event)
message(t) :: IO.chardata

Message to be used when logging. The format looks like:

Outgoing HTTP request to :service_name [GET] /path, ID: :request_id

Taking care to format the string properly if optional attributes like :service_name and :request_id are not present.

new(opts)
new(Keyword.t) :: t

Builds a new struct taking care to:

  • Parsing the :url and mapping it to the appropriate attributes.
  • Truncate the body if it is too large.
  • Normalize header values so they are consistent.
  • Normalize the method.
  • Removes “” or nil values.
new_with_message(opts)
new_with_message(Keyword.t) :: {t, IO.chardata}

Convenience methods for creating an event and getting the message at the same time.