graphite-metric 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 1.9.2@graphite-metric
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in graphite_metric.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'minitest', :notify => false do
2
+ watch(%r|^test/(.*)_test\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ end
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ If you're sending data to Graphite from Ruby, GraphiteMetric will help you get
2
+ it in the right format. For now, it only supports the plaintext protocol, but
3
+ pickle should be relatively easy to add. Contributions are welcome.
4
+
5
+ At [GoSquared](http://www.gosquared.com/), we're using graphite extensively.
6
+ Some services use it directly via TCP, others via UDP (direct or via
7
+ aggregators such as statsd). When metrics are critical, we push them into our
8
+ RabbitMQ cluster. Carbon agents support AMQP natively, thus an efficient and
9
+ fault-tolerant setup is achievable with little effort.
10
+
11
+ ## USAGE
12
+
13
+ What is a `GraphiteMetric::Plaintext`?
14
+
15
+ GraphiteMetric::Plaintext.ancestors
16
+ => [GraphiteMetric::Plaintext,
17
+ Struct,
18
+ Enumerable,
19
+ Object,
20
+ TestHelpers,
21
+ MiniTest::Expectations,
22
+ PP::ObjectMixin,
23
+ Kernel,
24
+ BasicObject]
25
+
26
+ ### From values
27
+
28
+ graphite_metric = GraphiteMetric::Plaintext.new("visitors", 2)
29
+ => #<struct GraphiteMetric::Plaintext
30
+ key="visitors",
31
+ value=2,
32
+ timestamp=1331043095>
33
+
34
+ graphite_metric.key
35
+ => "visitors"
36
+ graphite_metric.value
37
+ => 2
38
+ graphite_metric.timestamp
39
+ => 1331043095
40
+
41
+ graphite_metric.to_s
42
+ => "visitors 2 1331043095"
43
+
44
+ "#{graphite_metric}"
45
+ => "visitors 2 1331043095"
46
+
47
+ `GraphiteMetric::Plaintext` is the same as `GMP`. The following examples will
48
+ use the abbreviated form.
49
+
50
+ ### From hash
51
+
52
+ gmp = GMP.from_hash(
53
+ :key => "visitors",
54
+ :value => 2,
55
+ :timestamp => Time.now.to_i
56
+ )
57
+ => #<struct GraphiteMetric::Plaintext
58
+ key="visitors",
59
+ value=2,
60
+ timestamp=1331039853>
61
+
62
+ "#{gmp}"
63
+ => "visitors 2 1331039853"
64
+
65
+ ### From array (of hashes)
66
+
67
+ gmps = GMP.from_array([
68
+ {
69
+ :key => "visitors",
70
+ :value => 1,
71
+ :timestamp => Time.now.to_i
72
+ },
73
+ {
74
+ :key => "visitors",
75
+ :value => 5
76
+ }
77
+ ]
78
+ => [#<struct GraphiteMetric::Plaintext
79
+ key="visitors",
80
+ value=1,
81
+ timestamp=1331039914>,
82
+ #<struct GraphiteMetric::Plaintext
83
+ key="visitors",
84
+ value=5,
85
+ timestamp=1331043514>]
86
+
87
+ gmps.map(&:to_s)
88
+ ["visitors 1 1331039914", "visitors 5 1331043514"]
89
+
90
+
91
+
92
+ ## COMPATIBILITY
93
+
94
+ Tested against Ruby 1.9.2. Used in production on both 1.9.2 & 1.9.3.
95
+
96
+
97
+
98
+ ## LICENSE
99
+
100
+ (The MIT license)
101
+
102
+ Copyright (c) Gerhard Lazu
103
+
104
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
105
+ this software and associated documentation files (the "Software"), to deal in
106
+ the Software without restriction, including without limitation the rights to
107
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
108
+ of the Software, and to permit persons to whom the Software is furnished to do
109
+ so, subject to the following conditions:
110
+
111
+ The above copyright notice and this permission notice shall be included in all
112
+ copies or substantial portions of the Software.
113
+
114
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
115
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
116
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
117
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
118
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
119
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
120
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/**/*_test.rb'
6
+ end
7
+
8
+ task :default => [:test]
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "graphite-metric/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "graphite-metric"
7
+ s.version = GraphiteMetric::VERSION
8
+ s.authors = ["Gerhard Lazu"]
9
+ s.email = ["gerhard@lazu.co.uk"]
10
+ s.homepage = ""
11
+ s.summary = %q{Generates strings that graphite understands}
12
+ s.description = %q{Converts hashes and arrays into graphite plaintext format}
13
+
14
+ #s.rubyforge_project = "graphite-metric"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'guard-minitest'
22
+ s.add_development_dependency 'minitest'
23
+ s.add_development_dependency 'pry'
24
+ s.add_development_dependency 'turn'
25
+ end
@@ -0,0 +1,16 @@
1
+ require 'graphite-metric/util'
2
+
3
+ module GraphiteMetric
4
+ Plaintext = Struct.new(:key, :value, :timestamp) do
5
+ extend Util
6
+
7
+ def initialize(*args)
8
+ super
9
+ self[:timestamp] ||= Time.now.utc.to_i
10
+ end
11
+
12
+ def to_s
13
+ [key, value, timestamp].join(" ")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module GraphiteMetric
2
+ module Util
3
+ extend self
4
+
5
+ def from_hash(hash)
6
+ metric = new
7
+ members.each { |member| metric[member] = hash[member] if hash.has_key?(member) }
8
+ metric
9
+ end
10
+
11
+ def from_array(array)
12
+ array.inject([]) do |result, hash|
13
+ result << from_hash(hash)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module GraphiteMetric
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'graphite-metric/version'
2
+ require 'graphite-metric/plaintext'
3
+
4
+ module GraphiteMetric
5
+ end
6
+
7
+ GMP = GraphiteMetric::Plaintext
@@ -0,0 +1,16 @@
1
+ require_relative '../test_helper'
2
+ require 'graphite-metric/plaintext'
3
+
4
+ include TestHelpers
5
+
6
+ module GraphiteMetric
7
+ describe Plaintext do
8
+ it "by default timestamp is current utc" do
9
+ Plaintext.new.timestamp.must_equal utc_now
10
+ end
11
+
12
+ it "uses the graphite plaintext format when converted to string" do
13
+ "#{Plaintext.new("visitors", 2)}".must_equal "visitors 2 #{utc_now}"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../test_helper'
2
+ require 'graphite-metric/plaintext'
3
+
4
+ include TestHelpers
5
+
6
+ module GraphiteMetric
7
+ describe Plaintext do
8
+ before do
9
+ @timestamp = Time.now.to_i - 3600
10
+ end
11
+
12
+ describe "from hash" do
13
+ before do
14
+ @hash = {
15
+ :key => "visitors",
16
+ :value => 2,
17
+ :timestamp => @timestamp
18
+ }
19
+ end
20
+
21
+ it "can be built from a hash" do
22
+ "#{Plaintext.from_hash(@hash)}".must_equal "visitors 2 #{@timestamp}"
23
+ end
24
+ end
25
+
26
+ describe "from array (of hashes)" do
27
+ before do
28
+ @array = [
29
+ {
30
+ :key => "visitors",
31
+ :value => 1,
32
+ :timestamp => @timestamp
33
+ },
34
+ {
35
+ :key => "visitors",
36
+ :value => 5
37
+ }
38
+ ]
39
+ @graphite_metrics = Plaintext.from_array(@array)
40
+ end
41
+
42
+ it "returns an array" do
43
+ @graphite_metrics.must_be_instance_of Array
44
+ @graphite_metrics.size.must_equal 2
45
+ end
46
+
47
+ it "each element is a GraphiteMetric::Plaintext" do
48
+ @graphite_metrics.each { |graphite_metric| graphite_metric.must_be_instance_of GraphiteMetric::Plaintext }
49
+ end
50
+
51
+ it "converts into graphite plaintext format" do
52
+ @graphite_metrics.map(&:to_s).must_equal([
53
+ "visitors 1 #{@timestamp}",
54
+ "visitors 5 #{utc_now}"
55
+ ])
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ require_relative('./test_helper')
2
+ require 'graphite-metric'
3
+
4
+ describe GraphiteMetric do
5
+ it "Plainext has an abbreviated form" do
6
+ GMP.new.must_be_instance_of GraphiteMetric::Plaintext
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ require 'pry'
4
+ require 'turn/autorun'
5
+
6
+ module TestHelpers
7
+ def utc_now
8
+ Time.now.utc.to_i
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphite-metric
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gerhard Lazu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard-minitest
16
+ requirement: &2158012620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2158012620
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &2158012200 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2158012200
36
+ - !ruby/object:Gem::Dependency
37
+ name: pry
38
+ requirement: &2158011780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2158011780
47
+ - !ruby/object:Gem::Dependency
48
+ name: turn
49
+ requirement: &2158011360 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2158011360
58
+ description: Converts hashes and arrays into graphite plaintext format
59
+ email:
60
+ - gerhard@lazu.co.uk
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rvmrc
67
+ - Gemfile
68
+ - Guardfile
69
+ - README.md
70
+ - Rakefile
71
+ - graphite-metric.gemspec
72
+ - lib/graphite-metric.rb
73
+ - lib/graphite-metric/plaintext.rb
74
+ - lib/graphite-metric/util.rb
75
+ - lib/graphite-metric/version.rb
76
+ - test/graphite-metric-test.rb
77
+ - test/graphite-metric/plaintext_test.rb
78
+ - test/graphite-metric/util_test.rb
79
+ - test/test_helper.rb
80
+ homepage: ''
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.10
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Generates strings that graphite understands
104
+ test_files:
105
+ - test/graphite-metric-test.rb
106
+ - test/graphite-metric/plaintext_test.rb
107
+ - test/graphite-metric/util_test.rb
108
+ - test/test_helper.rb