Module: FnordMetric

Defined in:
lib/fnordmetric.rb

Defined Under Namespace

Modules: GaugeCalculations, GaugeModifiers Classes: App, AverageMetric, Cache, CombineMetric, Context, CountMetric, Dashboard, Event, FunnelWidget, Gauge, InboundStream, Logger, Metric, MetricAPI, Namespace, NumbersWidget, Report, Session, SumMetric, TimelineWidget, Widget, Worker

Constant Summary collapse

@@namespaces =
{}

Class Method Summary collapse

Class Method Details

.connect_redis(redis_url) ⇒ Object



105
106
107
# File 'lib/fnordmetric.rb', line 105

def self.connect_redis(redis_url)
  EM::Hiredis.connect(redis_url)
end

.default_options(opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fnordmetric.rb', line 19

def self.default_options(opts)

  opts[:redis_uri] = "redis://localhost:6379"
  opts[:redis_prefix] ||= "fnordmetric"            

  opts[:inbound_stream] ||= ["0.0.0.0", "1337"]
  opts[:web_interface] ||= ["0.0.0.0", "4242"]

  opts[:start_worker] ||= true
  opts[:print_stats] ||= 3

  # events that aren't processed after 2 min get dropped
  opts[:event_queue_ttl] ||= 120

  # event data is kept for one month
  opts[:event_data_ttl] ||= 3600*24*30

  # session data is kept for one month
  opts[:session_data_ttl] ||= 3600*24*30 
  
  opts
end

.error!(msg) ⇒ Object



88
89
90
91
# File 'lib/fnordmetric.rb', line 88

def self.error!(msg)
  raise msg if ENV['FNORDMETRIC_ENV'] == 'test'
  puts(msg); exit!
end

.log(msg) ⇒ Object



84
85
86
# File 'lib/fnordmetric.rb', line 84

def self.log(msg)
  puts "[#{Time.now.strftime("%y-%m-%d %H:%M:%S")}] #{msg}"
end

.namespace(key = nil, &block) ⇒ Object



15
16
17
# File 'lib/fnordmetric.rb', line 15

def self.namespace(key=nil, &block)    
  @@namespaces[key] = block    
end

FIXME: refactor this mess



109
110
111
112
113
114
115
116
117
# File 'lib/fnordmetric.rb', line 109

def self.print_stats(opts, redis) # FIXME: refactor this mess
  keys = [:events_received, :events_processed]
  redis.llen("#{opts[:redis_prefix]}-queue") do |queue_length|      
    redis.hmget("#{opts[:redis_prefix]}-stats", *keys) do |data|
      data_human = keys.size.times.map{|n|"#{keys[n]}: #{data[n]}"}
      log "#{data_human.join(", ")}, queue_length: #{queue_length}"
    end  
  end
end

.run(opts = {}) ⇒ Object



93
94
95
96
97
98
# File 'lib/fnordmetric.rb', line 93

def self.run(opts={})
  start_em(opts) 
rescue Exception => e
  log "!!! eventmachine died, restarting... #{e.message}"
  sleep(1); run(opts)  
end

.shutdownObject



100
101
102
103
# File 'lib/fnordmetric.rb', line 100

def self.shutdown
  log "shutting down, byebye"
  EM.stop
end

.standaloneObject



119
120
121
122
# File 'lib/fnordmetric.rb', line 119

def self.standalone    
  require "fnordmetric/logger"
  require "fnordmetric/standalone"
end

.start_em(opts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fnordmetric.rb', line 42

def self.start_em(opts)
  EM.run do

    trap("TERM", &method(:shutdown))
    trap("INT",  &method(:shutdown))

    opts = default_options(opts)      

    if opts[:start_worker]
      worker = Worker.new(@@namespaces.clone, opts)
      worker.ready!   
    end

    if opts[:inbound_stream]
      begin   
        inbound_stream = InboundStream.start(opts)           
        log "listening on tcp##{opts[:inbound_stream].join(":")}"
      rescue
        log "cant start FnordMetric::InboundStream. port in use?"
      end
    end

    if opts[:web_interface]
      begin             
        app = FnordMetric::App.new(@@namespaces.clone, opts)
        Thin::Server.start(*opts[:web_interface], app)
        log "listening on http##{opts[:web_interface].join(":")}"
      rescue Exception => e
        log "cant start FnordMetric::App. port in use?"
      end
    end

    if opts[:print_stats]        
      redis = connect_redis(opts[:redis_url])
      EM::PeriodicTimer.new(opts[:print_stats]) do 
        print_stats(opts, redis) 
      end
    end

  end 
end