lib/fnordmetric/namespace.rb in fnordmetric-1.0.1 vs lib/fnordmetric/namespace.rb in fnordmetric-1.2.0
- old
+ new
@@ -1,41 +1,56 @@
class FnordMetric::Namespace
-
+
attr_reader :handlers, :gauges, :opts, :key, :dashboards, :flags
- @@opts = [:event, :gauge, :widget, :set_title, :hide_active_users, :hide_overview, :dashboard]
+ @@opts = [:event, :gauge, :widget, :set_title, :hide_active_users, :hide_overview,
+ :hide_gauge_explorer, :dashboard]
+
@@multi_gauges = [:timeseries_gauge, :toplist_gauge, :distribution_gauge]
def initialize(key, opts)
@gauges = Hash.new
@dashboards = Hash.new
- @handlers = Hash.new
- @flags = Hash.new
+ @handlers = Hash.new.with_indifferent_access
@title = key
- @active_users_available = true
@opts = opts
@key = key
+
+ @flags = {
+ :hide_active_users => (FnordMetric.options[:enable_active_users] == false),
+ :hide_gauge_explorer => (FnordMetric.options[:enable_gauge_explorer] == false)
+ }
end
- def ready!(redis)
+ def ready!(redis, sync_redis = nil)
@redis = redis
+ @sync_redis = sync_redis
+ load_gauges
self
end
def announce(event)
- announce_to_timeline(event)
- announce_to_typelist(event)
+ if !@flags[:hide_active_users]
+ announce_to_timeline(event)
+ announce_to_typelist(event)
+ end
if event[:_session]
event[:_session_key] = announce_to_session(event).session_key
end
+ if FnordMetric::ZeroConfigGauge::TYPES.include?(event[:_type].to_sym)
+ ctx = FnordMetric::Context.new(opts, FnordMetric::ZeroConfigGauge::Handler)
+ ctx.call(event, @redis, self)
+ return self
+ end
+
res = [
@handlers[event[:_type].to_s],
@handlers["*"]
].flatten.compact.each do |context|
- context.call(event, @redis)
+ context.call(event, @redis, self)
end.size
if res == 0
FnordMetric.error("no handler for event-type: #{event[:_type]}")
end
@@ -73,14 +88,10 @@
def title
@title
end
- def active_users_available
- @active_users_available
- end
-
def dashboards(name=nil, opts = {})
return @dashboards unless name
dash = FnordMetric::Dashboard.new(opts.merge(:title => name))
@dashboards[dash.token.to_s] ||= dash
end
@@ -103,28 +114,33 @@
def opt_hide_active_users
@flags[:hide_active_users] = true
end
+ def opt_hide_gauge_explorer
+ @flags[:hide_gauge_explorer] = true
+ end
+
def opt_hide_overview
@flags[:hide_overview] = true
end
def opt_set_title(title)
@title = title
end
- def opt_event(event_type, opts={}, &block)
- opts.merge!(:redis => @redis, :gauges => @gauges)
+ def opt_event(event_type, opts={}, &block)
FnordMetric::Context.new(opts, block).tap do |context|
@handlers[event_type.to_s] ||= []
@handlers[event_type.to_s] << context
end
end
def opt_gauge(gauge_key, opts={})
- opts.merge!(:key => gauge_key, :key_prefix => key_prefix)
+ opts.merge!(:key => gauge_key)
+ store_gauge(gauge_key, opts) if opts[:zero_config]
+ opts.merge!(:key_prefix => key_prefix)
klass = "FnordMetric::#{(opts[:type] || "").to_s.camelize}Gauge".constantize
@gauges[gauge_key] ||= klass.new(opts)
end
def opt_multigauge(gauge_type, gauge_key, opts={})
@@ -141,12 +157,32 @@
def opt_dashboard(dashboard, opts)
dashboards(dashboard, opts)
end
def build_widget(opts)
- _gauges = [opts[:gauges]].flatten.map{ |g| @gauges.fetch(g) }
+ _gauges = [opts[:gauges]].flatten.map do |g|
+ @gauges[g] || FnordMetric::ZeroConfigGauge.new(g, self)
+ end
widget_klass = "FnordMetric::#{opts.fetch(:type).to_s.capitalize}Widget"
widget_klass.constantize.new(opts.merge(:gauges => _gauges))
+ end
+
+ def store_gauge(gauge_key, opts)
+ gaugelist_key = key_prefix("zero-config-gauges")
+ sync_redis.hset(gaugelist_key, gauge_key, opts.to_json)
+ end
+
+ def load_gauges
+ gaugelist_key = key_prefix("zero-config-gauges")
+ sync_redis.hgetall(gaugelist_key).each do |gauge_key, gauge_opts|
+ gopts = JSON.parse(gauge_opts).symbolize_keys
+ gopts.delete(:zero_config)
+ opt_gauge(gauge_key.to_sym, gopts)
+ end
+ end
+
+ def sync_redis
+ @sync_redis || @redis
end
def extend_opts(opts)
opts.merge(
:namespace_prefix => key_prefix,