graphite-api 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -50,7 +50,7 @@ Creating a new client instance
50
50
  ```ruby
51
51
  require 'graphite-api'
52
52
 
53
- GraphiteAPI::Client.new(
53
+ GraphiteAPI.new(
54
54
  graphite: "graphite.example.com:2003", # required argument
55
55
  prefix: ["example","prefix"], # add example.prefix to each key
56
56
  slice: 60, # results are aggregated in 60 seconds slices
@@ -64,7 +64,7 @@ Adding simple metrics
64
64
  ```ruby
65
65
  require 'graphite-api'
66
66
 
67
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
67
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
68
68
 
69
69
  client.metrics "webServer.web01.loadAvg" => 10.7
70
70
  # => webServer.web01.loadAvg 10.7 time.now.to_i
@@ -81,7 +81,7 @@ Adding metrics with timestamp
81
81
  ```ruby
82
82
  require 'graphite-api'
83
83
 
84
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
84
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
85
85
 
86
86
  client.metrics({
87
87
  "webServer.web01.loadAvg" => 10.7,
@@ -95,7 +95,7 @@ Increment records
95
95
  ```ruby
96
96
  require 'graphite-api'
97
97
 
98
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
98
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
99
99
 
100
100
  client.increment("jobs_in_queue", "num_errors")
101
101
  # => jobs_in_queue 1 Time.now.to_i
@@ -115,7 +115,7 @@ Some DSL sweetness
115
115
  ```ruby
116
116
  require 'graphite-api'
117
117
 
118
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
118
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
119
119
 
120
120
  client.webServer.web01.loadAvg 10.7
121
121
  # => webServer.web01.loadAvg 10.7 time.now.to_i
@@ -128,7 +128,7 @@ Built-in timers support
128
128
  ```ruby
129
129
  require 'graphite-api'
130
130
 
131
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
131
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
132
132
 
133
133
  # lets send the metric every 120 seconds
134
134
  client.every(120) do |c|
@@ -141,7 +141,7 @@ Built-in extension for time declarations stuff, like 2.minutes, 3.hours etc...
141
141
  require 'graphite-api'
142
142
  require 'graphite-api/core_ext/numeric'
143
143
 
144
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
144
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
145
145
 
146
146
  client.every 10.seconds do |c|
147
147
  c.metrics("webServer.web01.uptime" => `uptime`.split.first.to_i)
@@ -157,7 +157,7 @@ Make your own custom metrics daemons, using `client#join`
157
157
  require 'graphite-api'
158
158
  require 'graphite-api/core_ext/numeric'
159
159
 
160
- client = GraphiteAPI::Client.new( graphite: 'graphite:2003' )
160
+ client = GraphiteAPI.new( graphite: 'graphite:2003' )
161
161
 
162
162
  client.every 26.minutes do |c|
163
163
  c.metrics("webServer.shuki.stats" => 10)
@@ -185,8 +185,6 @@ GraphiteAPI::Logger.init(
185
185
  )
186
186
  ```
187
187
 
188
- > more examples can be found [here](https://github.com/kontera-technologies/graphite-api/tree/master/examples).
189
-
190
188
  ## GraphiteAPI-Middleware Usage
191
189
  * After installing GraphiteAPI gem, the `graphite-middleware` command should be available.
192
190
 
@@ -239,15 +237,12 @@ example.middleware.value2 99 1334929231
239
237
 
240
238
  ```ruby
241
239
  require 'graphite-api'
242
- client = GraphiteAPI::Client.new(:graphite => 'graphite-middleware-node:2005')
240
+ client = GraphiteAPI.new(:graphite => 'graphite-middleware-node:2005')
243
241
  client.example.middleware.value 10.2
244
242
  client.example.middleware.value2 27
245
243
  client.bla.bla.value2 27
246
244
  ```
247
245
 
248
- > more examples can be found [here](https://github.com/kontera-technologies/graphite-api/tree/master/examples).
249
-
250
-
251
246
  ## Example Setup
252
247
  <br/>
253
248
  <img src="https://raw.github.com/kontera-technologies/graphite-api/master/examples/middleware_t1.png" align="center">
data/Rakefile CHANGED
@@ -2,9 +2,62 @@ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
2
  Dir.chdir File.dirname __FILE__
3
3
 
4
4
  require 'graphite-api'
5
+ require "rake/testtask"
6
+ require 'rubygems/package_task'
5
7
 
6
8
  def message msg
7
9
  puts "*** #{msg} ***"
8
10
  end
9
11
 
10
- Dir['tasks/**/*.rake'].each { |rake| load rake }
12
+ task(:test => :functional) { ENV['with_coverage'] = "true" }
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << "tests"
16
+ t.pattern = "tests/**/*_test.rb"
17
+ end
18
+
19
+ task :functional do
20
+ some_failed = false
21
+
22
+ next unless ENV['SKIP_FUNC'].nil?
23
+
24
+ unless RUBY_COPYRIGHT.end_with?("Matsumoto")
25
+ puts("Functional tests are enabled only on MRI...")
26
+ next
27
+ end
28
+
29
+ message "Executing GraphiteAPI Functional Tests"
30
+ message "( You can skip them by passing SKIP_FUNC=true )"
31
+
32
+ Dir[File.expand_path("../tests/functional/*",__FILE__)].each do |file|
33
+ next unless file.end_with?(".rb")
34
+ now = Time.now.to_i
35
+ name = File.basename(file)
36
+ message "Executing #{name}"
37
+ Process.waitpid(Process.spawn("ruby", File.expand_path(file)))
38
+ took = "took #{Time.now.to_i - now} seconds"
39
+ if $?.success?
40
+ message "[PASS] #{name}, #{took}"
41
+ else
42
+ message "[FAIL] #{name}, #{took}"
43
+ some_failed = true
44
+ end
45
+ end
46
+ message "Done Executing GraphiteAPI Functional Tests"
47
+ abort "Some functional tests failed..." if some_failed
48
+ end
49
+
50
+ task :default => :test
51
+
52
+ task :gem => [:test,:clobber_package]
53
+
54
+ GraphiteAPI::GemSpec = eval File.read 'graphite-api.gemspec'
55
+
56
+ Gem::PackageTask.new(GraphiteAPI::GemSpec) do |p|
57
+ p.gem_spec = GraphiteAPI::GemSpec
58
+ end
59
+
60
+ task :install => [:gem] do
61
+ sh "gem install pkg/graphite-api"
62
+ Rake::Task['clobber_package'].execute
63
+ end
@@ -1,13 +1,13 @@
1
1
  require 'zscheduler'
2
2
 
3
3
  module GraphiteAPI
4
- ROOT = File.expand_path(File.dirname(__FILE__))
4
+ ROOT = File.expand_path File.dirname __FILE__
5
+
5
6
  require "#{ROOT}/graphite-api/version"
6
7
 
7
8
  autoload :Version, "#{ROOT}/graphite-api/version"
8
9
  autoload :Client, "#{ROOT}/graphite-api/client"
9
10
  autoload :Cache, "#{ROOT}/graphite-api/cache"
10
- autoload :Reactor, "#{ROOT}/graphite-api/reactor"
11
11
  autoload :Connector, "#{ROOT}/graphite-api/connector"
12
12
  autoload :Middleware, "#{ROOT}/graphite-api/middleware"
13
13
  autoload :Runner, "#{ROOT}/graphite-api/runner"
@@ -20,4 +20,8 @@ module GraphiteAPI
20
20
  GraphiteAPI::VERSION
21
21
  end
22
22
 
23
+ def self.new options
24
+ Client.new options
25
+ end
26
+
23
27
  end
@@ -31,10 +31,7 @@ module GraphiteAPI
31
31
  @options = options
32
32
  @queue = Queue.new
33
33
  @streamer = Hash.new {|h,k| h[k] = ""}
34
-
35
- if options[:cache]
36
- @cache = Cache::Memory.new options
37
- end
34
+ @cache = Cache::Memory.new options if options[:cache]
38
35
  end
39
36
 
40
37
  private_reader :queue, :options, :streamer, :cache
@@ -91,7 +88,7 @@ module GraphiteAPI
91
88
 
92
89
  def inspect
93
90
  "#<GraphiteAPI::Buffer:%s @quque#size=%s @streamer=%s>" %
94
- [object_id,queue.size,streamer]
91
+ [ object_id, queue.size, streamer]
95
92
  end
96
93
 
97
94
  private
@@ -1,63 +1,17 @@
1
- # -----------------------------------------------------
2
- # Graphite Client
3
- # Send metrics to graphite (or to some kind of middleware/proxy)
4
- # -----------------------------------------------------
5
- # Usage
6
- #
7
- # client = GraphiteAPI::Client.new(
8
- # :graphite => "graphite.example.com:2003",
9
- # :prefix => ["example","prefix"], # add example.prefix to each key
10
- # :slice => 60.seconds # results are aggregated in 60 seconds slices
11
- # :interval => 60.seconds # send to graphite every 60 seconds
12
- # )
13
- #
14
- # client.webServer.web01.loadAvg 10.7
15
- # # => example.prefix.webServer.web01.loadAvg 10.7 time.now.to_i
16
-
17
- # client.metrics "webServer.web01.loadAvg" => 10.7
18
- # # => example.prefix.webServer.web01.loadAvg 10.7 time.now.to_i
19
- #
20
- # client.metrics({
21
- # "webServer.web01.loadAvg" => 10.7,
22
- # "webServer.web01.memUsage" => 40
23
- # },Time.at(1326067060))
24
- # # => example.prefix.webServer.web01.loadAvg 10.7 1326067060
25
- # # => example.prefix.webServer.web01.memUsage 40 1326067060
26
- #
27
- # # Timers
28
- # client.every 10.seconds do |c|
29
- # c.webServer.web01.uptime `uptime`.split.first.to_i
30
- # # => example.prefix.webServer.web01.uptime 40 1326067060
31
- # end
32
- #
33
- # client.every 52.minutes do |c|
34
- # c.abcd.efghi.jklmnop.qrst 12
35
- # # => example.prefix.abcd.efghi.jklmnop.qrst 12 1326067060
36
- # end
37
- #
38
- # client.join # wait...
39
- # -----------------------------------------------------
40
-
41
1
  require File.expand_path '../utils', __FILE__
42
2
 
43
3
  module GraphiteAPI
44
4
  class Client
45
5
  include Utils
46
6
 
47
- private_reader :options, :buffer, :connectors, :direct_send
7
+ private_reader :options, :buffer, :connectors
48
8
 
49
9
  def initialize opt
50
10
  @options = build_options validate opt.clone
51
11
  @buffer = GraphiteAPI::Buffer.new options
52
12
  @connectors = GraphiteAPI::Connector::Group.new options
53
- @direct_send = @options[:interval] == 0
54
-
55
- if direct_send
56
- options[:slice] = 1
57
- else
58
- every(options.fetch(:interval),&method(:send_metrics))
59
- end
60
-
13
+
14
+ every options[:interval], &method(:send_metrics) if !options[:direct]
61
15
  end
62
16
 
63
17
  def_delegator Zscheduler, :loop, :join
@@ -68,8 +22,9 @@ module GraphiteAPI
68
22
  end
69
23
 
70
24
  def metrics metric, time = Time.now
25
+ return if metric.empty?
71
26
  buffer.push :metric => metric, :time => time
72
- send_metrics if direct_send
27
+ send_metrics if options[:direct]
73
28
  end
74
29
 
75
30
  alias_method :add_metrics, :metrics
@@ -104,26 +59,21 @@ module GraphiteAPI
104
59
  protected
105
60
 
106
61
  class Proxy
107
- include Utils
108
-
109
62
  def initialize client
110
- @client = client
111
- @keys = []
63
+ @client, @keys = client, []
112
64
  end
113
65
 
114
- private_reader :client, :keys
115
-
116
66
  def method_missing m, *args, &block
117
- keys.push m
118
- if keys.size > 10 # too deep
119
- super
67
+ if @keys.push(m).size > 10
68
+ super # too deep
120
69
  elsif args.any?
121
- client.metrics(Hash[keys.join("."),args.first],*args[1..-1])
70
+ @client.metrics Hash[
71
+ @keys.join('.'), args.first
72
+ ], *args[1..-1]
122
73
  else
123
74
  self
124
75
  end
125
76
  end
126
-
127
77
  end
128
78
 
129
79
  def validate options
@@ -136,6 +86,8 @@ module GraphiteAPI
136
86
  default_options.tap do |options_hash|
137
87
  options_hash[:backends].push expand_host opt.delete :graphite
138
88
  options_hash.merge! opt
89
+ options_hash[:direct] = options_hash[:interval] == 0
90
+ options_hash[:slice] = 1 if options_hash[:direct]
139
91
  end
140
92
  end
141
93
 
@@ -13,34 +13,23 @@ require 'socket'
13
13
  module GraphiteAPI
14
14
  class Connector
15
15
  class Group
16
- include Utils
17
-
18
- private_reader :options, :connectors
19
-
20
16
  def initialize options
21
- @options = options
22
17
  @connectors = options[:backends].map { |o| Connector.new(*o) }
23
18
  end
24
19
 
25
20
  def publish messages
26
- debug [:connector_group,:publish,messages.size, @connectors]
27
- Array(messages).each { |msg| connectors.map {|c| c.puts msg} }
21
+ Logger.debug [:connector_group, :publish, messages.size, @connectors]
22
+ Array(messages).each { |msg| @connectors.map {|c| c.puts msg} }
28
23
  end
29
-
30
24
  end
31
-
32
- include Utils
33
25
 
34
26
  def initialize host, port
35
- @host = host
36
- @port = port
27
+ @host, @port = host, port
37
28
  end
38
29
 
39
- private_reader :host, :port
40
-
41
30
  def puts message
42
31
  begin
43
- debug [:connector,:puts,[host,port].join(":"),message]
32
+ Logger.debug [:connector,:puts,[@host, @port].join(":"),message]
44
33
  socket.puts message + "\n"
45
34
  rescue Errno::EPIPE, Errno::EINVAL
46
35
  @socket = nil
@@ -56,8 +45,8 @@ module GraphiteAPI
56
45
 
57
46
  def socket
58
47
  if @socket.nil? || @socket.closed?
59
- debug [:connector,[host,port]]
60
- @socket = ::TCPSocket.new host, port
48
+ Logger.debug [:connector,[@host,@port]]
49
+ @socket = ::TCPSocket.new @host, @port
61
50
  end
62
51
  @socket
63
52
  end
@@ -66,7 +66,7 @@ module GraphiteAPI
66
66
  end
67
67
 
68
68
  # Send metrics to graphite every X seconds
69
- Zscheduler.every(options[:interval],:on_shutdown => true) do
69
+ Zscheduler.every(options[:interval], :on_shutdown => true) do
70
70
  group.publish buffer.pull :string if buffer.new_records?
71
71
  end
72
72
 
@@ -11,12 +11,12 @@ module GraphiteAPI
11
11
 
12
12
  def run
13
13
  Logger.init Hash[[:std,:level].zip options.values_at(:log_file, :log_level) ]
14
- options[:daemonize] ? daemonize(options[:pid]) { run! } : run!
14
+ options[:daemonize] ? daemonize(options[:pid], &method(:run!)) : run!
15
15
  end
16
16
 
17
17
  private
18
18
 
19
- def daemonize pid
19
+ def daemonize pid, &block
20
20
  block_given? or raise ArgumentError.new "the block is missing..."
21
21
 
22
22
  fork do
@@ -27,7 +27,7 @@ module GraphiteAPI
27
27
  STDOUT.reopen('/dev/null','a')
28
28
  STDERR.reopen('/dev/null','a')
29
29
  File.open(pid,'w') { |f| f.write(Process.pid) } rescue nil
30
- yield
30
+ block.call
31
31
  end
32
32
  end
33
33
 
@@ -1,3 +1,3 @@
1
1
  module GraphiteAPI
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  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.1.0
4
+ version: 0.1.1
5
5
  prerelease:
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-05-09 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -66,8 +66,6 @@ files:
66
66
  - lib/graphite-api/utils.rb
67
67
  - lib/graphite-api/version.rb
68
68
  - lib/graphite-api.rb
69
- - tasks/build.rake
70
- - tasks/tests.rake
71
69
  homepage: http://www.kontera.com
72
70
  licenses: []
73
71
  post_install_message:
@@ -1,32 +0,0 @@
1
- require 'rubygems/package_task'
2
-
3
- GraphiteAPI::GemSpec = Gem::Specification.new do |s|
4
- s.name = "graphite-api"
5
- s.version = GraphiteAPI.version
6
- s.platform = Gem::Platform::RUBY
7
- s.summary = "Graphite Ruby Client"
8
- s.description = "Graphite API - A Simple ruby client, aggregator daemon and API tools"
9
- s.author = "Eran Barak Levi"
10
- s.email = 'eran@kontera.com'
11
- s.homepage = 'http://www.kontera.com'
12
- s.executables = %w(graphite-middleware)
13
- s.required_ruby_version = '>= 1.8.7'
14
- s.rubyforge_project = "graphite-api"
15
- s.files = %w(README.md Rakefile) + Dir.glob("{bin,lib,test,tasks}/**/*")
16
- s.require_path = "lib"
17
- s.bindir = "bin"
18
-
19
- s.add_dependency 'eventmachine','>= 0.3.3'
20
- s.add_dependency 'zscheduler', '>= 0.0.3'
21
- end
22
-
23
- task :gem => [:test,:clobber_package]
24
-
25
- Gem::PackageTask.new(GraphiteAPI::GemSpec) do |p|
26
- p.gem_spec = GraphiteAPI::GemSpec
27
- end
28
-
29
- task :install => [:gem] do
30
- sh "gem install pkg/graphite-api"
31
- Rake::Task['clobber_package'].execute
32
- end
@@ -1,42 +0,0 @@
1
- require "rake/testtask"
2
-
3
- task(:test => :functional) { ENV['with_coverage'] = "true" }
4
-
5
- Rake::TestTask.new(:test) do |t|
6
- t.libs << "tests"
7
- t.pattern = "tests/**/*_test.rb"
8
- end
9
-
10
-
11
- task :functional do
12
- some_failed = false
13
-
14
- next unless ENV['SKIP_FUNC'].nil?
15
-
16
- unless RUBY_COPYRIGHT.end_with?("Matsumoto")
17
- puts("Functional tests are enabled only on MRI...")
18
- next
19
- end
20
-
21
- message "Executing GraphiteAPI Functional Tests"
22
- message "( You can skip them by passing SKIP_FUNC=true )"
23
-
24
- Dir[File.expand_path("../../tests/functional/*",__FILE__)].each do |file|
25
- next unless file.end_with?(".rb")
26
- now = Time.now.to_i
27
- name = File.basename(file)
28
- message "Executing #{name}"
29
- Process.waitpid(Process.spawn("ruby", File.expand_path(file)))
30
- took = "took #{Time.now.to_i - now} seconds"
31
- if $?.success?
32
- message "[PASS] #{name}, #{took}"
33
- else
34
- message "[FAIL] #{name}, #{took}"
35
- some_failed = true
36
- end
37
- end
38
- message "Done Executing GraphiteAPI Functional Tests"
39
- abort "Some functional tests failed..." if some_failed
40
- end
41
-
42
- task :default => :test