Skip to content

Instantly share code, notes, and snippets.

@leucos
Created June 15, 2012 08:54
Show Gist options
  • Select an option

  • Save leucos/2935496 to your computer and use it in GitHub Desktop.

Select an option

Save leucos/2935496 to your computer and use it in GitHub Desktop.
Fnordmetric helper

Goal

The point is to write a Fnordmetric helper. Fnordmetric is a "statsd-like" metric collector without the Carbon hassle.

Examples

  • I would like to be able to do things like :
  push_timer :hell_yeah
    # do some code
  pop_timer

=> pop_timer pops the last timer pushed and sends an event to Fnormetric with the id hell_yeah. Thus, timers can be nested :

  push_timer :hell_yeah
    # do some code
    push_timer :some_nested_timer
    # some more code
    pop_timer
  pop_timer

=> this will send two events (:some_nested_timer first, then :hell_yeah)

  • Other uses could be :
  times(:some_id) do
    # some code block
  end

=> this uses {push,pop}_timer behind the scenes

  • Also, another style :
  class Users < Controller
    helper ::fnordmetric
    clock :login

=> this measure the login method execution time, by wrapping it automatically between push_timer/pop_timer calls

  • Another usage, implemented in before/after blocks, to measuse several methods :
    before_all do |m|
      push_timer :performance, :this => "some args", :method => m
    end

    after_all do |m|
      pop_timer
    end

Problem

I basically tryied implementing all of this, and it always break when redirect* is called.

class Users < Controller
helper ::fnordmetric
before_all do |m|
push_timer :performance, :this => "some args", :method => m
end
after_all do |m|
pop_timer
end
def login
redirect_referer if logged_in?
return unless request.post?
times :user_login_performance do
user_login(request.subset(:email, :password))
end
# do whatever now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment