Class: MetricLogger

Inherits:
Object
  • Object
show all
Defined in:
app/models/metric_logger.rb

Overview

This class provides an interface to log events in a metrics database. Here, fnordmetric and redis are used.

For more information, check out this resources:

* http://railscasts.com/episodes/378-fnordmetric
* https://github.com/paulasmuth/fnordmetric

In the ApplicationController, this class is instanciated as metric_logger, which is made available as controller helper method.

This, you may access this in a controller like this:

class UsersController
  def show
    # ...
    metric_logger.log_event(@user, type: 'show_user')
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MetricLogger

Returns a new instance of MetricLogger.



23
24
25
26
# File 'app/models/metric_logger.rb', line 23

def initialize(options = {})
  @current_user = options[:current_user]
  @session_id = options[:session_id]
end

Class Method Details

.log_event(data, options = {}) ⇒ Object

This is a shortcut for one-line logs, where no current_user is required. Then, one may just call:

MetricLogger.log_event(...)


57
58
59
# File 'app/models/metric_logger.rb', line 57

def self.log_event(data, options = {})
  self.new.log_event(data, options)
end

Instance Method Details

#current_userObject



28
29
30
# File 'app/models/metric_logger.rb', line 28

def current_user
  @current_user
end

#log_cpu_usageObject



72
73
74
75
76
77
78
79
80
81
# File 'app/models/metric_logger.rb', line 72

def log_cpu_usage
  if RUBY_PLATFORM.to_s.include? "linux"
    cpu_usage = `top -bn1 | grep "Cpu(s)" | \
           sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
           awk '{print 100 - $1}'`.to_i 
  end
  if cpu_usage
    log_event({ percentage: cpu_usage }, type: :cpu_usage)
  end
end

#log_event(data, options = {}) ⇒ Object

Log the event with the given information. The FNORD_METRIC constant, which provides a direct connection to the redis database, is defined in the fnordmetric initializer:

config/initializers/fnordmetric.rb

options:

type :    the type of event that is recorded here


45
46
47
48
49
50
# File 'app/models/metric_logger.rb', line 45

def log_event(data, options = {})
  register_session unless options[:type].in? ["_set_name", "_set_picture"]
  data.merge!({ _type: options[:type] })
  data.merge!({ _session: session_id })
  FNORD_METRIC.event(data) unless Rails.env.test?
end

#register_sessionObject

These options tells fnordmetric, which user does the request.

http://fnordmetric.io/documentation/classic_event_handlers/

We use the user’s unique id as session id, since



65
66
67
68
69
70
# File 'app/models/metric_logger.rb', line 65

def register_session
  if current_user
    log_event({ name: current_user.title }, type: "_set_name")
    log_event({ url: ApplicationController.helpers.user_avatar_url(current_user) }, type: "_set_picture")
  end
end

#session_idObject



32
33
34
35
# File 'app/models/metric_logger.rb', line 32

def session_id
  #@session_id
  @current_user.try(:id).try(:to_s)
end