Module: Pinbo

Defined in:
lib/pinbo.rb,
lib/pinbo/request.rb,
lib/pinbo/middleware.rb

Defined Under Namespace

Modules: Timer Classes: Middleware, Request

Constant Summary collapse

Config =
{
  :host => Socket.gethostname,
  :pinba_host => '74.86.33.154', #:pinba_host => 'localhost',
  :pinba_port => 30002
}

Class Method Summary collapse

Class Method Details

.counterObject



22
23
24
# File 'lib/pinbo.rb', line 22

def counter
  Thread.current[:counter] ||= 1
end

.counter=(val) ⇒ Object



26
27
28
# File 'lib/pinbo.rb', line 26

def counter=(val)
  Thread.current[:counter] = val
end

.dataObject



30
31
32
# File 'lib/pinbo.rb', line 30

def data
  Thread.current[:data]
end

.data=(val) ⇒ Object



34
35
36
# File 'lib/pinbo.rb', line 34

def data=(val)
  Thread.current[:data] = val
end

.get_timer(tags) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/pinbo.rb', line 91

def get_timer(tags)
  unless (t = self.timers.detect { |v| v[:tags] == tags } ) 
    t = { :tags => tags, :normalized_tags => normalize_tags(tags), :counter => 0, :period => 0 }
    self.timers << t
  end
  t
end

.increase_counterObject



46
47
48
# File 'lib/pinbo.rb', line 46

def increase_counter
  self.counter += 1
end

.normalize_tags(raw_tags) ⇒ Object



87
88
89
# File 'lib/pinbo.rb', line 87

def normalize_tags(raw_tags)
  raw_tags.map { |k, v| { :name_id => timer_counter, :value_id => timer_counter, :name => k.to_s, :value => v.to_s } }
end

.start(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pinbo.rb', line 50

def start(options = {})
  Thread.current[:request_start] = Time.now
  Thread.current[:timer_counter] = 0
  self.data = { 
                :server_name => Socket.gethostname,
                :script_name => '/test.rb',
                :request_count => counter,
                :document_size => 0,
                :memory_peak => 0,
                :request_time => 0,
                :ru_utime => 0,
                :ru_stime => 0,
                :status => 200 }.merge(options)
  self.timers = []
end

.stop(options = {}) ⇒ Object



71
72
73
74
75
# File 'lib/pinbo.rb', line 71

def stop(options = {})
  data.merge!( :request_time => Time.now - Thread.current[:request_start] ).merge!(options)
  Request.new( :data => data, :timers => timers).perform
  increase_counter
end

.timer(tags = {}, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/pinbo.rb', line 77

def timer(tags = {}, &block)
  t = get_timer(tags)
  t[:start] = Time.now
  block.call
  time = Time.now
  t[:counter] += 1
  t[:stop] = time
  t[:period] += t[:stop] - t[:start]
end

.timer_counterObject



66
67
68
69
# File 'lib/pinbo.rb', line 66

def timer_counter
  Thread.current[:timer_counter] += 1
  Thread.current[:timer_counter] - 1
end

.timersObject



38
39
40
# File 'lib/pinbo.rb', line 38

def timers
  Thread.current[:timers]
end

.timers=(val) ⇒ Object



42
43
44
# File 'lib/pinbo.rb', line 42

def timers=(val)
  Thread.current[:timers] = val
end