Class: Timber::Events::HTTPResponse

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

Overview

Note:

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

The HTTP server response event tracks outgoing HTTP responses that you send to clients.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ HTTPResponse

Returns a new instance of HTTPResponse.



14
15
16
17
18
19
20
21
22
23
# File 'lib/timber/events/http_response.rb', line 14

def initialize(attributes)
  @body = attributes[:body] && Util::HTTPEvent.normalize_body(attributes[:body])
  @content_length = Timber::Util::Object.try(attributes[:content_length], :to_i)
  @headers = Util::HTTPEvent.normalize_headers(attributes[:headers])
  @http_context = attributes[:http_context]
  @request_id = attributes[:request_id]
  @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)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def body
  @body
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def content_length
  @content_length
end

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def headers
  @headers
end

#http_contextObject (readonly)

Returns the value of attribute http_context.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def http_context
  @http_context
end

#request_idObject (readonly)

Returns the value of attribute request_id.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def request_id
  @request_id
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def service_name
  @service_name
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def status
  @status
end

#time_msObject (readonly)

Returns the value of attribute time_ms.



12
13
14
# File 'lib/timber/events/http_response.rb', line 12

def time_ms
  @time_ms
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

Builds a hash representation containing simple objects, suitable for serialization (JSON).



32
33
34
# File 'lib/timber/events/http_response.rb', line 32

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

#messageObject

Returns the human readable log message for this event.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/timber/events/http_response.rb', line 37

def message
  if http_context
    message = "#{http_context[:method]} #{http_context[:path]} completed with " \
      "#{status} #{status_description} "

    if content_length
      message += ", #{content_length} bytes, "
    end

    message + "in #{time_ms}ms"
  else
    message = "Completed #{status} #{status_description} "

    if content_length
      message += ", #{content_length} bytes, "
    end

    message + "in #{time_ms}ms"
  end
end

#status_descriptionObject



58
59
60
# File 'lib/timber/events/http_response.rb', line 58

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

#to_hashObject Also known as: to_h



25
26
27
28
# File 'lib/timber/events/http_response.rb', line 25

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