recognizer 0.0.7 → 0.0.8

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.org CHANGED
@@ -5,7 +5,7 @@
5
5
  * Install
6
6
  : gem install recognizer
7
7
  * Configure
8
- Example: config.json
8
+ Example: =config.json=
9
9
  : {
10
10
  : "librato": {
11
11
  : "email": "email@example.com",
@@ -14,13 +14,29 @@
14
14
  : },
15
15
  : "amqp": {
16
16
  : "host": "hostname",
17
- : "pass": "password",
18
- : "user": "username"
17
+ : "user": "username",
18
+ : "pass": "password"
19
19
  : }
20
20
  : }
21
21
  * Usage
22
22
  : Usage: recognizer (options)
23
23
  : -c, --config CONFIG The config file path
24
24
  : -h, --help Show this message
25
+ * More
26
+ Example metric path: =production.i-424242.cpu.user=
27
+
28
+ Get the source from the metric path using a regular expression
29
+ : {
30
+ : "librato": {
31
+ : "source": "/i-.*/"
32
+ Or using an index
33
+ : {
34
+ : "librato": {
35
+ : "source": 1
36
+ Or define a static source
37
+ : {
38
+ : "librato": {
39
+ : "source": "custom"
40
+ By default, Recognizer uses the source: =recognizer=
25
41
  * License
26
42
  Recognizer is released under the [[https://github.com/portertech/recognizer/raw/master/MIT-LICENSE.txt][MIT license]].
@@ -8,25 +8,37 @@ module Recognizer
8
8
  unless thread_queue && options.is_a?(Hash)
9
9
  raise "You must provide a thread queue and options"
10
10
  end
11
- amqp = Bunny.new(options[:amqp])
11
+
12
+ options[:amqp] ||= Hash.new
13
+ options[:amqp][:exchange] ||= Hash.new
14
+
15
+ exchange_name = options[:amqp][:exchange][:name] || "graphite"
16
+ durable = options[:amqp][:exchange][:durable] || false
17
+ routing_key = options[:amqp][:exchange][:routing_key] || "#"
18
+ exchange_type = options[:amqp][:exchange][:type] || :topic
19
+
20
+ amqp = Bunny.new(options[:amqp].reject { |key, value| key == :exchange })
12
21
  amqp.start
22
+
23
+ exchange = amqp.exchange(exchange_name, :type => exchange_type.to_sym, :durable => durable)
13
24
  queue = amqp.queue("recognizer")
14
- exchange = amqp.exchange("graphite", :type => :topic, :durable => true)
15
- queue.bind(exchange, :key => "*")
25
+ queue.bind(exchange, :key => routing_key)
26
+
16
27
  Thread.abort_on_exception = true
28
+
17
29
  consumer = Thread.new do
18
30
  puts "Awaiting the metrics with impatience ..."
19
31
  queue.subscribe do |message|
20
- payload = message[:payload]
21
- routing_key = message[:routing_key]
32
+ payload = message[:payload]
33
+ msg_routing_key = message[:routing_key] || message[:delivery_details][:routing_key]
22
34
  lines = payload.split("\n")
23
35
  lines.each do |line|
24
36
  line = line.strip
25
- case line.split(" ").count
37
+ case line.split("\s").count
26
38
  when 3
27
39
  thread_queue.push(line)
28
40
  when 2
29
- thread_queue.push("#{routing_key} #{line}")
41
+ thread_queue.push("#{msg_routing_key} #{line}")
30
42
  end
31
43
  end
32
44
  end
@@ -13,14 +13,17 @@ module Recognizer
13
13
  unless options[:librato][:email] && options[:librato][:api_key]
14
14
  raise "You must provide a Librato Metrics account email and API key"
15
15
  end
16
+
16
17
  ::Librato::Metrics.authenticate(options[:librato][:email], options[:librato][:api_key])
17
18
  librato = ::Librato::Metrics::Queue.new
19
+
18
20
  mutex = Mutex.new
19
21
  Thread.abort_on_exception = true
22
+
20
23
  Thread.new do
21
24
  loop do
22
25
  sleep(options[:librato][:flush_interval] || 10)
23
- unless librato.queued.empty?
26
+ unless librato.empty?
24
27
  puts "Attempting to flush metrics to Librato"
25
28
  mutex.synchronize do
26
29
  librato.submit
@@ -29,17 +32,35 @@ module Recognizer
29
32
  end
30
33
  end
31
34
  end
35
+
36
+ get_source = case options[:librato][:source]
37
+ when String
38
+ if options[:librato][:source].match("^/.*/$")
39
+ @source_pattern = Regexp.new(options[:librato][:source].delete("/"))
40
+ Proc.new { |path| (matched = path.grep(@source_pattern).first) ? matched : "recognizer" }
41
+ else
42
+ Proc.new { options[:librato][:source] }
43
+ end
44
+ when Integer
45
+ Proc.new { |path| path.slice(options[:librato][:source]) }
46
+ else
47
+ Proc.new { "recognizer" }
48
+ end
49
+
32
50
  Thread.new do
33
51
  loop do
34
52
  graphite_formated = thread_queue.pop
35
53
  begin
36
- metric, value, timestamp = graphite_formated.split(" ").inject([]) do |result, part|
37
- result << (result.empty? ? part.to_sym : Float(part).pretty)
54
+ path, value, timestamp = graphite_formated.split(" ").inject([]) do |result, part|
55
+ result << (result.empty? ? part.split(".") : Float(part).pretty)
38
56
  result
39
57
  end
40
- puts "Adding metric to queue: #{graphite_formated}"
58
+ source = get_source.call(path)
59
+ path.delete(source)
60
+ metric = {path.join(".") => {:value => value, :measure_time => timestamp, :source => source}}
41
61
  mutex.synchronize do
42
- librato.add(metric => {:value => value, :measure_time => timestamp, :source => "recognizer"})
62
+ puts "Adding metric to queue: #{metric.inspect}"
63
+ librato.add(metric)
43
64
  end
44
65
  rescue ArgumentError
45
66
  puts "Invalid metric: #{graphite_formated}"
@@ -1,3 +1,3 @@
1
1
  module Recognizer
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/recognizer.gemspec CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.executables = Dir.glob("bin/**/*").map { |file| File.basename(file) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency("librato-metrics", "0.2.3")
21
- s.add_dependency("bunny", "0.7.8")
20
+ s.add_dependency("librato-metrics", "0.3.1")
21
+ s.add_dependency("bunny", "0.7.9")
22
22
  s.add_dependency("mixlib-cli", ">= 1.1.0")
23
23
  s.add_dependency("json")
24
24
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recognizer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sean Porter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-10 00:00:00 -08:00
18
+ date: 2012-02-24 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -29,9 +29,9 @@ dependencies:
29
29
  hash: 17
30
30
  segments:
31
31
  - 0
32
- - 2
33
32
  - 3
34
- version: 0.2.3
33
+ - 1
34
+ version: 0.3.1
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
@@ -42,12 +42,12 @@ dependencies:
42
42
  requirements:
43
43
  - - "="
44
44
  - !ruby/object:Gem::Version
45
- hash: 19
45
+ hash: 17
46
46
  segments:
47
47
  - 0
48
48
  - 7
49
- - 8
50
- version: 0.7.8
49
+ - 9
50
+ version: 0.7.9
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
53
  - !ruby/object:Gem::Dependency