Sha256: 0b25244aad71952e0eae7c4f3b2dd71c6f33ecf1af0d39506f4e18d8094df2ba

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require "fnordmetric-client/version"
require 'securerandom'
require 'json/add/core'

class FnordmetricClient
  DEFAULT_EVENT_TTL = 3600
  DEFAULT_PREFIX = 'fnordmetric'
  DEFAULT_OVERFLOW_LIMIT = 50000

  def initialize(opts ={})
    raise ArgumentError, "Bad options" unless opts.is_a?(Hash)

    @redis = opts[:redis]
    raise ArgumentError, "Bad :redis passed" unless %w{hincrby set lpush expire llen}.all?{|m| @redis.respond_to?(m) }

    @prefix = opts[:prefix] || DEFAULT_PREFIX
    @event_ttl = opts[:event_ttl] || DEFAULT_EVENT_TTL
    @overflow_limit = opts[:overflow_limit] || DEFAULT_OVERFLOW_LIMIT
  end

  def event(type, args = {})
    push_event({'_type' => type.to_s}.merge(args))
  end

  private 

    def key(*elements)
      elements.map(&:to_s).unshift(@prefix).join('-')
    end

    def push_event(event_data)    
      return if @redis.llen(key(:queue)) >= @overflow_limit

      event_id = get_next_uuid
      @redis.hincrby(key(:testdata), "events_received", 1)
      @redis.hincrby(key(:stats), "events_received", 1)
      @redis.set(key(:event, event_id), event_data.to_json)
      @redis.expire(key(:event, event_id), @event_ttl)
      @redis.lpush(key(:queue), event_id)
      event_id
    end
  
    def get_next_uuid
      SecureRandom.hex(16)
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fnordmetric-client-0.0.1 lib/fnordmetric-client.rb