Class: MetricLogger
- Inherits:
-
Object
- Object
- MetricLogger
- 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
-
.log_event(data, options = {}) ⇒ Object
This is a shortcut for one-line logs, where no current_user is required.
Instance Method Summary collapse
- #current_user ⇒ Object
-
#initialize(options = {}) ⇒ MetricLogger
constructor
A new instance of MetricLogger.
- #log_cpu_usage ⇒ Object
-
#log_event(data, options = {}) ⇒ Object
Log the event with the given information.
-
#register_session ⇒ Object
These options tells fnordmetric, which user does the request.
- #session_id ⇒ Object
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( = {}) @current_user = [:current_user] @session_id = [: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, = {}) self.new.log_event(data, ) end |
Instance Method Details
#current_user ⇒ Object
28 29 30 |
# File 'app/models/metric_logger.rb', line 28 def current_user @current_user end |
#log_cpu_usage ⇒ Object
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, = {}) register_session unless [:type].in? ["_set_name", "_set_picture"] data.merge!({ _type: [:type] }) data.merge!({ _session: session_id }) FNORD_METRIC.event(data) unless Rails.env.test? end |
#register_session ⇒ Object
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_id ⇒ Object
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 |