Class: Timber::Events::HTTPClientResponse

Inherits:
Timber::Event
  • Object
show all
Defined in:
lib/timber/events/http_client_response.rb

Overview

Note:

This event should be installed automatically through integrations, such as the Integrations::NetHTTP integration.

The HTTP client response event tracks responses for outgoing HTTP requests. This gives you structured insight into communication with external services.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ HTTPClientResponse

Returns a new instance of HTTPClientResponse.



11
12
13
14
15
16
17
18
19
20
# File 'lib/timber/events/http_client_response.rb', line 11

def initialize(attributes)
  @headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
  @request_id = attributes[:request_id]
  @service_name = attributes[:service_name]
  @status = attributes[:status] || raise(ArgumentError.new(":status is required"))
  @time_ms = attributes[:time_ms] || raise(ArgumentError.new(":time_ms is required"))
  @time_ms = @time_ms.round(6)

  @body = Util::HTTPEvent.normalize_body(@headers["content-type"], attributes[:body])
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def headers
  @headers
end

#request_idObject (readonly)

Returns the value of attribute request_id.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def request_id
  @request_id
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def service_name
  @service_name
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def status
  @status
end

#time_msObject (readonly)

Returns the value of attribute time_ms.



9
10
11
# File 'lib/timber/events/http_client_response.rb', line 9

def time_ms
  @time_ms
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



28
29
30
# File 'lib/timber/events/http_client_response.rb', line 28

def as_json(_options = {})
  {:http_client_response => to_hash}
end

#messageObject



32
33
34
35
36
37
38
39
40
# File 'lib/timber/events/http_client_response.rb', line 32

def message
  message = "Outgoing HTTP response"

  if service_name
    message << " from #{service_name}"
  end

  message << " #{status_description} in #{time_ms}ms"
end

#status_descriptionObject



42
43
44
# File 'lib/timber/events/http_client_response.rb', line 42

def status_description
  Rack::Utils::HTTP_STATUS_CODES[status]
end

#to_hashObject Also known as: to_h



22
23
24
25
# File 'lib/timber/events/http_client_response.rb', line 22

def to_hash
  {body: body, headers: headers, request_id: request_id, service_name: service_name,
    status: status, time_ms: time_ms}
end