Module: Ramaze::Helper::Fnordmetric
- Defined in:
- lib/ramaze/helper/fnordmetric.rb
Overview
This helper provides a convenience wrapper for sending events to Fnordmetric.
events can be anything, its just an indication that something happened. Fnordmetric can then make some agregates on events received per period.
Since events can carry arbitrary data, this helper adds methods that send performance data to Fnordmetric, so one can easily measure code execution times.
events are associated to the Innate session id, and thus are linked to visitors of your site. this is really usefull since you can, for instance, see how long a controller action took for a particular user.
If you want so use a redis server other than the usual localhost:6379, you need to define :fnord_redis_url trait, e.g. :
trait :fnord_redis_url => "redis://redis.example.com:6332"
TODO: @example Basic usage here… TODO: Implement optional with_id that uses specific id instead of innate.sid in conjunction with… TODO: simple keys instead of list, for the above
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- @@fnord =
@@fnord will hold Fnordmetric API instance @@redis holds a Redis connection A timer is an Array holding the event name, a Hash of arguments and a timestamp
nil
- @@redis =
nil
- @@sstack_key_root =
"fnordmetric.%s.%s.%s" % [ ENV['HOSTNAME'] || "localhost", ENV['USER'], Ramaze..app.name.to_s ]
Class Method Summary collapse
-
.included(base) ⇒ Object
We need clock as a class method Let’s extend the includer when it includes us.
Instance Method Summary collapse
-
#clear_timers ⇒ Object
Removes all timers in the stack.
-
#event(evt, args = {}) ⇒ Object
Sends an event to Fnordmetric.
-
#pageview(url = ) ⇒ Object
Sends a _pageview Fnordmetric event.
-
#pop_timer ⇒ Object
Pops a timer and sends an event.
-
#push_timer(event_name, args = {}) ⇒ Object
Starts a timer and pushes it to the timers stack.
-
#set_name(name) ⇒ Object
Sets username for the current session.
-
#set_picture(url = "http://placekitten.com/80/80") ⇒ Object
Sets the picture URL for the user.
-
#times(event_name, args = {}, &block) ⇒ Object
All in one timing function for a block.
Class Method Details
.included(base) ⇒ Object
We need clock as a class method Let’s extend the includer when it includes us
47 48 49 50 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 47 def self.included(base) Ramaze::Log.debug("Fnordmetric helper is being included in %s" % base.name) base.extend(ClassMethods) end |
Instance Method Details
#clear_timers ⇒ Object
Removes all timers in the stack
148 149 150 151 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 148 def clear_timers Ramaze::Log.debug("Cleared %s timers for %s" % [ @@redis.llen(_key), _key ]) @@redis.del _key end |
#event(evt, args = {}) ⇒ Object
Sends an event to Fnordmetric
This helper method sends an event to Fnordmetric, populating the :_session field with the current innate sid.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 61 def event(evt, args = {}) # Let's connect first, it will have to be done anyway return unless evt _connect unless @@fnord evt = { :_type => evt.to_s, :_session => session.sid } evt.merge!(args) Ramaze::Log.debug("Logging Fnordmetric event %s" % evt.inspect) @@fnord.event(evt) end |
#pageview(url = ) ⇒ Object
Sends a _pageview Fnordmetric event
This method sends a specific _pageview event Fnordmetric event This event is treated in a special way by Fnordmetric (see doc).
If all your controllers inherit ‘Controller’, you can log all page view very easily :
class Controller < Ramaze::Controller
helper :fnordmetric
before_all do
pageview
end
173 174 175 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 173 def pageview(url=request.env['REQUEST_PATH']) event(:_pageview, :url => url) end |
#pop_timer ⇒ Object
Pops a timer and sends an event
This method pops the last pushed timer and sends an event No arguments are needed, since they were stored by push_timer
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 128 def pop_timer len = @@redis.llen(_key) if len > 0 json = @@redis.lpop(_key) wat, args, wen = JSON.parse(json) Ramaze::Log.debug("Timer popped for %s (stack level is now %s)" % [ wat, len - 1]) # We log millisecs time = Time.now-Time.at(wen) time *= 1000 event(wat, args.merge(:time => time.to_i)) else Ramaze::Log.error("Unable to pop timer in %s (no event in stack)" % _key) #raise RuntimeError, "Unable to pop timer in %s (no event in stack)" % _key end end |
#push_timer(event_name, args = {}) ⇒ Object
Starts a timer and pushes it to the timers stack
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 111 def push_timer(event_name, args = {}) _connect unless @@redis @@redis.lpush(_key, [event_name, args, Time.now.to_f].to_json) @@redis.expire(_key, _ttl) Ramaze::Log.debug("Timer pushed and TTL set to %s for %s to %s (stack level is now %s)" % [ _ttl, event_name, _key, @@redis.llen(_key) ]) end |
#set_name(name) ⇒ Object
Sets username for the current session
This manually sets a user name for the current session. It calls the specific :_set_name Fnordmetric event This comes handy for user tracking
186 187 188 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 186 def set_name(name) event(:_set_name, :name => name) end |
#set_picture(url = "http://placekitten.com/80/80") ⇒ Object
Sets the picture URL for the user
This manually sets a user picture for the current session. It calls the specific :_set_picture Fnordmetric event. Using this method, you’ll be able to have a picture associated to the user in Fnordmetric’s user tracking panel
217 218 219 220 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 217 def set_picture(url="http://placekitten.com/80/80") url = url.to_s if url.class.to_s == 'URI::HTTP' event(:_set_picture, :url => url) end |
#times(event_name, args = {}, &block) ⇒ Object
All in one timing function for a block
This method will send an event containing the execution time of the passed block.
90 91 92 93 94 95 96 97 |
# File 'lib/ramaze/helper/fnordmetric.rb', line 90 def times(event_name, args = {}, &block) push_timer(event_name, args) # THINK: may be raise since there is no point in using times without a # block yield if block_given? ensure pop_timer end |