Class: Timber::Events::ControllerCall

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

Overview

Note:

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

The controller call event tracks controller invocations. For example, this line in Rails:

Processing by PagesController#home as HTML

Constant Summary collapse

ACTION_MAX_BYTES =
256.freeze
FORMAT_MAX_BYTES =
256.freeze
CONTROLLER_MAX_BYTES =
256.freeze
PARAMS_JSON_MAX_BYTES =
32_768.freeze
PASSWORD_NAME =
'password'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ControllerCall

Returns a new instance of ControllerCall.



21
22
23
24
25
26
27
# File 'lib/timber/events/controller_call.rb', line 21

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @controller = normalizer.fetch!(:controller, :string, :limit => CONTROLLER_MAX_BYTES)
  @action = normalizer.fetch!(:action, :string, :limit => ACTION_MAX_BYTES)
  @params = normalizer.fetch(:params, :hash, :sanitize => [PASSWORD_NAME])
  @format = normalizer.fetch(:format, :string, :limit => FORMAT_MAX_BYTES)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



19
20
21
# File 'lib/timber/events/controller_call.rb', line 19

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



19
20
21
# File 'lib/timber/events/controller_call.rb', line 19

def controller
  @controller
end

#formatObject (readonly)

Returns the value of attribute format.



19
20
21
# File 'lib/timber/events/controller_call.rb', line 19

def format
  @format
end

#paramsObject (readonly)

Returns the value of attribute params.



19
20
21
# File 'lib/timber/events/controller_call.rb', line 19

def params
  @params
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

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



39
40
41
# File 'lib/timber/events/controller_call.rb', line 39

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

#messageObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/timber/events/controller_call.rb', line 43

def message
  message = "Processing by #{controller}##{action}"
  if !message.nil?
    message << " as #{format}"
  end
  if !params.nil? && params.length > 0
    message << "\n  Parameters: #{params.inspect}"
  end
  message
end

#to_hashObject Also known as: to_h



29
30
31
32
33
34
35
# File 'lib/timber/events/controller_call.rb', line 29

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:controller, controller)
    h.add(:action, action)
    h.add(:params_json, params.to_json.byteslice(0, PARAMS_JSON_MAX_BYTES))
  end
end