Class: Timber::Events::HTTPResponse
- Inherits:
-
Timber::Event
- Object
- Timber::Event
- Timber::Events::HTTPResponse
- Defined in:
- lib/timber/events/http_response.rb
Overview
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.
Constant Summary collapse
- BODY_MAX_BYTES =
8192.freeze
- HEADERS_JSON_MAX_BYTES =
256.freeze
- HEADERS_TO_SANITIZE =
['authorization', 'x-amz-security-token'].freeze
- REQUEST_ID_MAX_BYTES =
256.freeze
- SERVICE_NAME_MAX_BYTES =
256.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_context ⇒ Object
readonly
Returns the value of attribute http_context.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#service_name ⇒ Object
readonly
Returns the value of attribute service_name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#time_ms ⇒ Object
readonly
Returns the value of attribute time_ms.
Instance Method Summary collapse
-
#as_json(_options = {}) ⇒ Object
Builds a hash representation containing simple objects, suitable for serialization (JSON).
-
#initialize(attributes) ⇒ HTTPResponse
constructor
A new instance of HTTPResponse.
-
#message ⇒ Object
Returns the human readable log message for this event.
- #status_description ⇒ Object
- #to_hash ⇒ Object (also: #to_h)
Constructor Details
#initialize(attributes) ⇒ HTTPResponse
Returns a new instance of HTTPResponse.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/timber/events/http_response.rb', line 21 def initialize(attributes) normalizer = Util::AttributeNormalizer.new(attributes) body_limit = Config.instance.http_body_limit || BODY_MAX_BYTES headers_to_sanitize = HEADERS_TO_SANITIZE + (Config.instance.http_header_filters || []) @body = normalizer.fetch(:body, :string, :limit => body_limit) @content_length = normalizer.fetch(:content_length, :integer) @headers = normalizer.fetch(:headers, :hash, :sanitize => headers_to_sanitize) @http_context = attributes[:http_context] @request_id = normalizer.fetch(:request_id, :string, :limit => REQUEST_ID_MAX_BYTES) @service_name = normalizer.fetch(:service_name, :string, :limit => SERVICE_NAME_MAX_BYTES) @status = normalizer.fetch!(:status, :integer) @time_ms = normalizer.fetch!(:time_ms, :float, :precision => 6) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def body @body end |
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def content_length @content_length end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def headers @headers end |
#http_context ⇒ Object (readonly)
Returns the value of attribute http_context.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def http_context @http_context end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def request_id @request_id end |
#service_name ⇒ Object (readonly)
Returns the value of attribute service_name.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def service_name @service_name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def status @status end |
#time_ms ⇒ Object (readonly)
Returns the value of attribute time_ms.
18 19 20 |
# File 'lib/timber/events/http_response.rb', line 18 def time_ms @time_ms end |
Instance Method Details
#as_json(_options = {}) ⇒ Object
Builds a hash representation containing simple objects, suitable for serialization (JSON).
50 51 52 |
# File 'lib/timber/events/http_response.rb', line 50 def as_json( = {}) {:http_response => to_hash} end |
#message ⇒ Object
Returns the human readable log message for this event.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/timber/events/http_response.rb', line 55 def if http_context = "#{http_context[:method]} #{http_context[:path]} completed with " \ "#{status} #{status_description} " if content_length << ", #{content_length} bytes, " end << "in #{time_ms}ms" else = "Completed #{status} #{status_description} " if content_length << ", #{content_length} bytes, " end << "in #{time_ms}ms" end end |
#status_description ⇒ Object
76 77 78 |
# File 'lib/timber/events/http_response.rb', line 76 def status_description Rack::Utils::HTTP_STATUS_CODES[status] end |
#to_hash ⇒ Object Also known as: to_h
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/timber/events/http_response.rb', line 36 def to_hash @to_hash ||= Util::NonNilHashBuilder.build do |h| h.add(:body, body) h.add(:content_length, content_length) h.add(:headers_json, headers, :json_encode => true, :limit => HEADERS_JSON_MAX_BYTES) h.add(:request_id, request_id) h.add(:service_name, service_name) h.add(:status, status) h.add(:time_ms, time_ms) end end |