Timber v2.2.1 Timber.Events.HTTPClientRequestEvent View Source
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.start_timer()
{: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
Link to this section 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
Link to this section Types
Link to this section Functions
Message to be used when logging. The format looks like:
Outgoing HTTP request XdF21 to :service_name [GET] /path
Taking care to format the string properly if optional attributes like :service_name
and
:request_id
are not present.
Builds a new struct taking care to:
- Parsing the
:url
and mapping it to the appropriate attributes. - Truncates the body if it is too large.
- Normalize header values so they are consistent.
- Normalize the method.
- Removes “” or nil values.
Link to this function
new_with_message(opts)
View Source
new_with_message(Keyword.t) :: {t, IO.chardata}
Convenience methods for creating an event and getting the message at the same time.