Commit f73afea5 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

core: implemented basic zero config gauges

parent 406fbe73
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -33,6 +33,10 @@ class FnordMetric::Context
    @proxy ||= Proxy.new(self)
  end

  def namespace
    @opts[:namespace]
  end

  def dispatch(method, *args, &block)
    if args.size > 0 && @opts[:gauges][args[0]].try(:renderable?)
      @opts[:gauges][args.delete_at(0)].execute(method, *args.unshift(self), &block)
@@ -81,4 +85,3 @@ protected


end
    
+29 −1
Original line number Diff line number Diff line
@@ -4,6 +4,34 @@ FnordMetric::TICKS = lambda{ |tick, span| [tick, 60, 300, 1200, 3600, 86400]
  .select{ |t| (t >= tick) && ((span/t) > 5) }
  .uniq }


FnordMetric::DEFAULT_PROC = lambda{ |arg| }

FnordMetric::ZERO_CONFIG_TYPES = [:_incr, :_decr, :_avg, :_min, :_max]

FnordMetric::ZERO_CONFIG_HANDLER = proc {
  if data[:gauge]
    gauge_key = data[:gauge].to_sym
  else
    FnordMetric.error("missing key for zero config event: gauge")
    next
  end

  unless data[:flush_interval]
    FnordMetric.error("missing key for zero config event: flush_interval")
    next
  end

  unless data[:value]
    FnordMetric.error("missing key for zero config event: value")
    next
  end

  gauge = if namespace.gauges.has_key?(gauge_key)
    namespace.gauges[gauge_key]
  else
    namespace.opt_gauge(gauge_key,
      :tick => data[:flush_interval].to_i)
  end

  incr_tick gauge, data[:value]
}
+8 −3
Original line number Diff line number Diff line
@@ -10,13 +10,18 @@ class FnordMetric::Namespace
  def initialize(key, opts)
    @gauges = Hash.new
    @dashboards = Hash.new
    @handlers = Hash.new
    @flags = Hash.new
    @title = key
    @active_users_available = true
    @gauge_explorer_available = true
    @opts = opts
    @key = key

    @handlers = Hash.new.with_indifferent_access

    ZERO_CONFIG_TYPES.each do |type|
      opt_event(type, &ZERO_CONFIG_HANDLER)
    end
  end

  def ready!(redis)
@@ -127,7 +132,7 @@ class FnordMetric::Namespace
  end

  def opt_event(event_type, opts={}, &block)
    opts.merge!(:redis => @redis, :gauges => @gauges)   
    opts.merge!(:redis => @redis, :gauges => @gauges, :namespace => self)
    FnordMetric::Context.new(opts, block).tap do |context|
      @handlers[event_type.to_s] ||= []
      @handlers[event_type.to_s] << context
+42 −4
Original line number Diff line number Diff line
@@ -10,6 +10,44 @@ describe FnordMetric::GaugeModifiers do

  before(:each) do
    @redis.keys("fnordmetrics-myns*").each { |k| @redis.del(k) }
    @redis.keys("fnordmetric-myns*").each { |k| @redis.del(k) } 
  end

  describe "increment zero-config gauges" do

    before(:each) do
      @namespace = FnordMetric::Namespace.new(:myns_213,
        :redis_prefix => "fnordmetric")
      @namespace.ready!(@redis_wrap)
    end

    it "should create and increment a zero-config gauge by 1" do
      @namespace.announce(
        :_type => "_incr",
        :_eid  => 1234,
        :_time => 1360584960,
        :value => 42,
        :gauge => "sales-per-second",
        :flush_interval => 10
      )

      @namespace.gauges[:"sales-per-second"].should be_a(Gauge)

      gauge_key = "fnordmetric-myns_213-gauge-sales-per-second-10"
      @redis.hget(gauge_key, "1360584960").should == "42"

      @namespace.announce(
        :_type => "_incr",
        :_eid  => 1234,
        :_time => 1360584960,
        :value => 11,
        :gauge => "sales-per-second",
        :flush_interval => 10
      )

      @redis.hget(gauge_key, "1360584960").should == "53"
    end

  end

  describe "increment non-progressive gauges" do
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ sitemap:
      title: "Sending Data"
      url: "/classic_sending_data"
    -
      title: "Gauges"
      title: "Events and Gauges"
      url: "/classic_gauges"
    -
      title: "Event Handlers"