graphite-api 0.0.3.beta1 → 0.0.3.beta3

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,9 +30,9 @@ module GraphiteAPI
30
30
 
31
31
  def initialize options
32
32
  @options = options
33
- @keys_to_sync = Hash.new { |h,k| h[k] = Set.new }
33
+ @keys_to_sync = Hash.new { |h,k| h[k] = Set.new }
34
34
  @streamer_buff = Hash.new {|h,k| h[k] = ""}
35
- @reanimation_mode = !options[:reanimation_exp].nil?
35
+ @reanimation_mode = !options[:cache].nil?
36
36
  start_cleaner if reanimation_mode
37
37
  end
38
38
 
@@ -40,7 +40,7 @@ module GraphiteAPI
40
40
 
41
41
  def push hash
42
42
  debug [:buffer,:add, hash]
43
- time = Utils.normalize_time(hash[:time],options[:slice])
43
+ time = normalize_time(hash[:time],options[:slice])
44
44
  hash[:metric].each { |k,v| cache_set(time,k,v) }
45
45
  end
46
46
 
@@ -130,7 +130,7 @@ module GraphiteAPI
130
130
  end
131
131
 
132
132
  def start_cleaner
133
- Reactor::every(options[:cleaner_interval]) { clean(options[:reanimation_exp]) }
133
+ Reactor::every(options[:cleaner_interval]) { clean(options[:cache]) }
134
134
  end
135
135
 
136
136
  end
@@ -1,9 +1,10 @@
1
1
  module GraphiteAPI
2
2
  module Cache
3
3
  class Memory
4
-
4
+ include Utils
5
+
5
6
  def initialize options
6
- Reactor.every(60) { clean(options[:age]) }
7
+ Reactor.every(120) { clean(options[:cache]) }
7
8
  end
8
9
 
9
10
  def get time, key
@@ -14,8 +15,8 @@ module GraphiteAPI
14
15
  cache[time][key] = value.to_f
15
16
  end
16
17
 
17
- def incr *args
18
- set(value.to_f + get(*args))
18
+ def incr time, key, value
19
+ set(time, key, value.to_f + get(time, key))
19
20
  end
20
21
 
21
22
  private
@@ -25,7 +26,9 @@ module GraphiteAPI
25
26
  end
26
27
 
27
28
  def clean age
29
+ debug [:MemoryCache, :before_clean, cache]
28
30
  cache.delete_if {|t,k| Time.now.to_i - t > age}
31
+ debug [:MemoryCache, :after_clean, cache]
29
32
  end
30
33
 
31
34
  end
@@ -42,7 +42,7 @@ module GraphiteAPI
42
42
  end
43
43
 
44
44
  opts.on("-r", "--reanimation HOURS","reanimate records that are younger than X hours, please see README") do |exp|
45
- (options[:reanimation_exp] = exp.to_i * 3600) if exp.to_i > 0
45
+ (options[:cache] = exp.to_i * 3600) if exp.to_i > 0
46
46
  end
47
47
 
48
48
  opts.on("-v", "--version","Show version and exit") do |exp|
@@ -24,6 +24,7 @@ module GraphiteAPI
24
24
  def puts message
25
25
  begin
26
26
  Logger.debug [:connector,:puts,[host,port].join(":"),message]
27
+ return
27
28
  socket.puts message + "\n"
28
29
  rescue Errno::EPIPE, Errno::EINVAL
29
30
  @socket = nil
@@ -57,7 +57,7 @@ module GraphiteAPI
57
57
  EventMachine.run do
58
58
  GraphiteAPI::Logger.info "Server running on port #{options[:port]}"
59
59
 
60
- buffer = GraphiteAPI::Buffer.new options
60
+ buffer = GraphiteAPI::SafeBuffer.new options
61
61
  group = GraphiteAPI::ConnectorGroup.new options
62
62
 
63
63
  # Starting server
@@ -1,3 +1,22 @@
1
+ # -----------------------------------------------------
2
+ # Buffer Object
3
+ # Handle Socket & Client data streams
4
+ # -----------------------------------------------------
5
+ # Usage:
6
+ # buff = GraphiteAPI::SafeBuffer.new(GraphiteAPI::Utils.default_options)
7
+ # buff << {:metric => {"load_avg" => 10},:time => Time.now}
8
+ # buff << {:metric => {"load_avg" => 30},:time => Time.now}
9
+ # buff.stream "mem.usage 1"
10
+ # buff.stream "90 1326842563\n"
11
+ # buff.stream "shuki.tuki 999 1326842563\n"
12
+ # buff.pull.each {|o| p o}
13
+ #
14
+ # Produce:
15
+ # ["load_avg", 40.0, 1326881160]
16
+ # ["mem.usage", 190.0, 1326842520]
17
+ # ["shuki.tuki", 999.0, 1326842520]
18
+ # -----------------------------------------------------
19
+
1
20
  require 'thread'
2
21
  require 'set'
3
22
 
@@ -5,30 +24,24 @@ module GraphiteAPI
5
24
  class SafeBuffer
6
25
  include Utils
7
26
 
27
+ CHARS_TO_BE_IGNORED = ["\r"]
28
+ END_OF_STREAM = "\n"
29
+ VALID_MESSAGE = /^[\w|\.]+ \d+(?:\.|\d)* \d+$/
30
+
8
31
  def initialize options
9
32
  @options = options
10
33
  @queue = Queue.new
11
34
  @streamer = Hash.new {|h,k| h[k] = ""}
12
35
 
13
- if options[:reanimation_exp]
14
- @cache = Cache::Memory.new options[:reanimation_exp]
15
- end
36
+ if options[:cache]
37
+ @cache = Cache::Memory.new options
38
+ end
16
39
  end
17
40
 
18
41
  private_reader :queue, :options, :streamer, :cache
19
42
 
20
- # {:metric => {'a' => 10},:time => today}
21
- def push hash
22
- debug [:buffer,:add, hash]
23
- time = Utils.normalize_time(hash[:time],options[:slice])
24
- hash[:metric].each { |k,v| queue.push [time,k,v] }
25
- end
26
-
27
- alias_method :<<, :push
28
-
29
43
  # this method isn't thread safe
30
- # if you are running with multiple threads
31
- # use #push instead
44
+ # use #push for multiple threads support
32
45
  def stream message, client_id = nil
33
46
  message.gsub(/\t/,' ').each_char do |char|
34
47
  next if invalid_char? char
@@ -43,12 +56,27 @@ module GraphiteAPI
43
56
  end
44
57
  end
45
58
 
59
+ # Add records to buffer
60
+ # push({:metric => {'a' => 10},:time => Time.now})
61
+ def push obj
62
+ debug [:buffer,:add, obj]
63
+ queue.push obj
64
+ nil
65
+ end
66
+
67
+ alias_method :<<, :push
68
+
46
69
  def pull format = nil
47
- data = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = 0} }
70
+ data = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = 0} }
71
+
72
+ counter = 0
48
73
  while new_records?
49
- time, key, value = queue.pop
50
- data[time][key] += value.to_f
74
+ break if ( counter += 1 ) > 10_000
75
+ hash = queue.pop
76
+ time = normalize_time(hash[:time],options[:slice])
77
+ hash[:metric].each { |k,v| data[time][k] += v.to_f }
51
78
  end
79
+
52
80
  data.map do |time, hash|
53
81
  hash.map do |key, value|
54
82
  value = cache.incr(time,key,value) if cache
@@ -62,6 +90,10 @@ module GraphiteAPI
62
90
  !queue.empty?
63
91
  end
64
92
 
93
+ def inspect
94
+ "#<GraphiteAPI::SafeBuffer:#{object_id} @quque#size=#{queue.size} @streamer=#{streamer.inspect}>"
95
+ end
96
+
65
97
  private
66
98
 
67
99
  def stream_message_to_obj message
@@ -70,19 +102,23 @@ module GraphiteAPI
70
102
  end
71
103
 
72
104
  def invalid_char? char
73
- ["\r"].include? char
105
+ CHARS_TO_BE_IGNORED.include? char
74
106
  end
75
107
 
76
108
  def closed_stream? string
77
- string[-1,1] == "\n"
109
+ string[-1,1] == END_OF_STREAM
78
110
  end
79
111
 
80
112
  def valid_stream_message message
81
- message =~ /^[\w|\.]+ \d+(?:\.|\d)* \d+$/
113
+ message =~ VALID_MESSAGE
82
114
  end
83
-
115
+
84
116
  def prefix
85
- @prefix ||= options[:prefix].empty? ? '' : Array(options[:prefix]).join('.') << '.'
117
+ @prefix ||= if options[:prefix] and !options[:prefix].empty?
118
+ Array(options[:prefix]).join('.') << '.'
119
+ else
120
+ ""
121
+ end
86
122
  end
87
123
 
88
124
  end
@@ -23,13 +23,14 @@ module GraphiteAPI
23
23
  private *args
24
24
  end
25
25
  end
26
-
27
- module_function
28
-
29
- def normalize_time time, slice = 60
26
+
27
+ def normalize_time time, slice
28
+ slice = 60 if slice.nil?
30
29
  ((time || Time.now).to_i / slice * slice).to_i
31
30
  end
32
31
 
32
+ module_function
33
+
33
34
  def expand_host host
34
35
  host,port = host.split(":")
35
36
  port = port.nil? ? default_options[:port] : port.to_i
@@ -42,7 +43,7 @@ module GraphiteAPI
42
43
  :cleaner_interval => 43200,
43
44
  :port => 2003,
44
45
  :log_level => :info,
45
- :reanimation_exp => nil,
46
+ :cache => nil,
46
47
  :host => "localhost",
47
48
  :prefix => [],
48
49
  :interval => 60,
@@ -1,5 +1,5 @@
1
1
  module GraphiteAPI
2
2
  class Version
3
- VERSION = "0.0.3.beta1"
3
+ VERSION = "0.0.3.beta3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphite-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.beta1
4
+ version: 0.0.3.beta3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-04 00:00:00.000000000 Z
12
+ date: 2013-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine