technoweenie / guillotine

URL shortening hobby kit
http://techno-weenie.net/guillotine/
MIT License
486 stars 54 forks source link

Please Statistics #17

Closed JosefJezek closed 10 years ago

JosefJezek commented 10 years ago

Could you add Statistics as Google URL Shortener?

technoweenie commented 10 years ago

Nope. Stats are explicitly mentioned in the "Not TODO" section of the README. You can do this yourself with a custom adapter that wraps whatever DB adapter that you're using.

Here's some code we use in production to write to statsd:

  def self.Stats(base_class)
    Class.new base_class do
      def initialize(statsd, *args)
        @stats = statsd
        super
      end

      def add(url, code = nil, options = nil)
        @stats.time("guillotine.add") { super }
      ensure
        @stats.increment("guillotine.count")
      end

      def find(code)
        url = nil
        @stats.time("guillotine.find") { url = super }
        url
      end
    end
  end

Basically, it subclasses an existing adapter, and adds timing and counting. You'd use it like:

# use statsd gem
statsd = Statsd.new('192.30.252.84', 8126)
adapter_class = Stats(Guillotine::RedisAdapter)
adapter = adapter_class.new(statsd, redis_connection)