Class: Timber::Contexts::HTTP

Inherits:
Timber::Context
  • Object
show all
Defined in:
lib/timber/contexts/http.rb

Overview

Note:

This context should be installed automatically through the, Intregrations::Rack::HTTPContext Rack middleware.

The HTTP context adds data about the current HTTP request being processed to your logs. This allows you to tail and filter by this data. A very useful piece of data this captures is the request ID. This gives you the ability to trace requests and view logs for a specific request only. For example, say you’ve searched your logs and found the specific line you are looking for, but it lacks context. With Timber you can simply click the request ID and “zoom out” to view all logs for that request. This gives you complete picture of how the log line in questio was generated.

Constant Summary collapse

HOST_MAX_BYTES =
256.freeze
METHOD_MAX_BYTES =
20.freeze
PATH_MAX_BYTES =
2048.freeze
REMOTE_ADDR_MAX_BYTES =
256.freeze
REQUEST_ID_MAX_BYTES =
256.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ HTTP

Returns a new instance of HTTP.



26
27
28
29
30
31
32
33
# File 'lib/timber/contexts/http.rb', line 26

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @host = normalizer.fetch(:host, :string, :limit => HOST_MAX_BYTES)
  @method = normalizer.fetch!(:method, :string, :upcase => true, :limit => METHOD_MAX_BYTES)
  @path = normalizer.fetch(:path, :string, :limit => PATH_MAX_BYTES)
  @remote_addr = normalizer.fetch(:remote_addr, :string, :limit => REMOTE_ADDR_MAX_BYTES)
  @request_id = normalizer.fetch(:request_id, :string, :limit => REQUEST_ID_MAX_BYTES)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/timber/contexts/http.rb', line 24

def host
  @host
end

#methodObject (readonly)

Returns the value of attribute method.



24
25
26
# File 'lib/timber/contexts/http.rb', line 24

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/timber/contexts/http.rb', line 24

def path
  @path
end

#remote_addrObject (readonly)

Returns the value of attribute remote_addr.



24
25
26
# File 'lib/timber/contexts/http.rb', line 24

def remote_addr
  @remote_addr
end

#request_idObject (readonly)

Returns the value of attribute request_id.



24
25
26
# File 'lib/timber/contexts/http.rb', line 24

def request_id
  @request_id
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



46
47
48
# File 'lib/timber/contexts/http.rb', line 46

def as_json(_options = {})
  to_hash
end

#to_hashObject

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



36
37
38
39
40
41
42
43
44
# File 'lib/timber/contexts/http.rb', line 36

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:host, host)
    h.add(:method, method)
    h.add(:path, path)
    h.add(:remote_addr, remote_addr)
    h.add(:request_id, request_id)
  end
end