mini_graphite 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 96afb08d730f3a00ba47725ce3b39eae492368e4
4
+ data.tar.gz: 1b2a2350de858f406a671e2e4e2390e6f66b001e
5
+ SHA512:
6
+ metadata.gz: 09f08e5c5fd678fb01bd59c7faf730022fc9aaed46d9f2cb70eb890b72dd993de8978e80591b566b8e37e9a2af8f8f96ed3af6184621672f62c3ede42078f62f
7
+ data.tar.gz: bfcc21cfeca12874cf435d9491bf6434e1613324392e69abe2692a84e625288f1fa97a173c9639c09e843cc928eef1889ee0693a11e0cb4c42962d8abbd4cf30
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in daliagraphite.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sevastianos Komianos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MiniGraphite
2
+
3
+ Simple wrapper for Graphite and Statsd
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mini_graphite'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mini_graphite
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/mini_graphite/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "."
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ t.verbose = true
11
+ end
data/ci_script.sh ADDED
@@ -0,0 +1,13 @@
1
+ echo "Build Tag: $BUILD_TAG"
2
+ echo $PATH
3
+ whoami
4
+ pwd
5
+ ruby -v
6
+ date
7
+
8
+ echo "---START---"
9
+
10
+ bundle --no-color
11
+ bundle exec rake
12
+
13
+ echo "---END---"
@@ -0,0 +1,61 @@
1
+ require_relative "mini_graphite/version"
2
+ require_relative "mini_graphite/logger"
3
+
4
+ require "socket"
5
+
6
+ module Dalia
7
+ module MiniGraphite
8
+ DEFAULTS = {
9
+ :graphite_host => "graphite.host.com",
10
+ :graphite_port => 2003,
11
+ :statsd_host => "statsd.host.com",
12
+ :statsd_port => 8125,
13
+ :mock_mode => false,
14
+ :debug_mode => false
15
+ }
16
+
17
+ def self.config(opts = {})
18
+ @opts = DEFAULTS.merge(opts)
19
+ @logger = Dalia::MiniGraphite::Logger.new(opts[:debug_mode])
20
+ logger.debug("Initalized with opts")
21
+ logger.debug(opts.inspect)
22
+ end
23
+
24
+ def self.datapoint(key, value = 1, timestamp = Time.now)
25
+ signal = "#{key} #{value} #{timestamp.to_i}"
26
+ logger.debug("Sending datapoint: '#{signal}'")
27
+
28
+ send_tcp(signal) if !opts[:mock_mode]
29
+ end
30
+
31
+ def self.counter(key, value = 1)
32
+ signal = "#{key}:#{value}|c"
33
+ logger.debug("Sending counter: '#{signal}'")
34
+
35
+ send_udp(signal) if !opts[:mock_mode]
36
+ end
37
+
38
+ private
39
+
40
+ def self.opts
41
+ @opts
42
+ end
43
+
44
+ def self.logger
45
+ @logger
46
+ end
47
+
48
+ def self.send_tcp(message)
49
+ socket = TCPSocket.new(opts[:graphite_host], opts[:graphite_port])
50
+ socket.print("#{message}\n")
51
+ socket.close
52
+ end
53
+
54
+ def self.send_udp(message)
55
+ socket = UDPSocket.new
56
+ socket.send(message, 0, opts[:statsd_host], opts[:statsd_port])
57
+ socket.close
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,28 @@
1
+ module Dalia
2
+
3
+ module MiniGraphite
4
+
5
+ class Logger
6
+ attr_reader :debug_mode
7
+
8
+ def initialize(debug_mode = true)
9
+ @debug_mode = debug_mode
10
+ end
11
+
12
+ def debug(message)
13
+ return unless debug_mode
14
+
15
+ result = "Dalia::MiniGraphite [#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}]: #{message}"
16
+
17
+ if defined? ::Rails
18
+ ::Rails.logger.info result
19
+ else
20
+ Kernel.puts result
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,5 @@
1
+ module Dalia
2
+ module MiniGraphite
3
+ VERSION = "0.0.3"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mini_graphite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mini_graphite"
8
+ spec.version = Dalia::MiniGraphite::VERSION
9
+ spec.authors = ["Sevastianos Komianos"]
10
+ spec.email = ["sebastian.komianos@daliaresearch.com"]
11
+ spec.summary = "Simple wrapper for Graphite and Statsd"
12
+ spec.description = "Simple wrapper for Graphite and Statsd"
13
+ spec.homepage = "https://github.com/DaliaResearch/MiniGraphite"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "mocha"
25
+ end
@@ -0,0 +1,26 @@
1
+ require "minitest/autorun"
2
+ require "mocha/setup"
3
+ require_relative "../lib/mini_graphite"
4
+
5
+ class MiniGraphiteTest < MiniTest::Unit::TestCase
6
+
7
+ def test_debug
8
+ Kernel.expects(:puts).with(regexp_matches(/MESSAGE/))
9
+ logger = Dalia::MiniGraphite::Logger.new
10
+ logger.debug("MESSAGE")
11
+ end
12
+
13
+ def test_debug_when_not_debug_mode
14
+ Kernel.expects(:puts).never
15
+ logger = Dalia::MiniGraphite::Logger.new(false)
16
+ logger.debug("MESSAGE")
17
+ end
18
+
19
+ def test_debug_when_rails_actived
20
+ Kernel.const_set("Rails", mock(:logger => mock(:info)))
21
+ logger = Dalia::MiniGraphite::Logger.new
22
+ logger.debug("MESSAGE")
23
+ Kernel.send(:remove_const, :Rails)
24
+ end
25
+
26
+ end
@@ -0,0 +1,62 @@
1
+ require "minitest/autorun"
2
+ require "mocha/setup"
3
+ require_relative "../lib/mini_graphite"
4
+
5
+ class MiniGraphiteTest < MiniTest::Unit::TestCase
6
+
7
+ def test_datapoint
8
+ Dalia::MiniGraphite.config({ :graphite_host => "graphite.host.com", :graphite_port => 2003 })
9
+
10
+ socket_mock = mock()
11
+ TCPSocket.expects(:new).with("graphite.host.com", 2003).returns(socket_mock)
12
+ socket_mock.expects(:print).with("test.age 31 1357117860\n")
13
+ socket_mock.expects(:close)
14
+
15
+ Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
16
+ end
17
+
18
+ def test_counter
19
+ Dalia::MiniGraphite.config({ :statsd_host => "statsd.host.com", :statsd_port => 8125 })
20
+
21
+ socket_mock = mock()
22
+ UDPSocket.expects(:new).returns(socket_mock)
23
+ socket_mock.expects(:send).with("height:231|c", 0, "statsd.host.com", 8125 )
24
+ socket_mock.expects(:close)
25
+
26
+ Dalia::MiniGraphite.counter("height", 231)
27
+ end
28
+
29
+ def test_on_config_should_debug
30
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).at_least_once
31
+ Dalia::MiniGraphite.config()
32
+ end
33
+
34
+ def test_on_counter_should_debug
35
+ Dalia::MiniGraphite.expects(:send_udp)
36
+ Dalia::MiniGraphite.config()
37
+
38
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending counter: 'test.age:31|c'")
39
+ Dalia::MiniGraphite.counter("test.age", 31)
40
+ end
41
+
42
+ def test_on_datapoint_should_debug
43
+ Dalia::MiniGraphite.expects(:send_tcp)
44
+ Dalia::MiniGraphite.config()
45
+
46
+ Dalia::MiniGraphite::Logger.any_instance.expects(:debug).with("Sending datapoint: 'test.age 31 1357117860'")
47
+ Dalia::MiniGraphite.datapoint("test.age", 31, Time.new(2013,1,2,10,11))
48
+ end
49
+
50
+ def test_on_datapoint_not_send_tcp_if_mock_mode
51
+ Dalia::MiniGraphite.config(:mock_mode => true)
52
+ TCPSocket.expects(:new).never
53
+ Dalia::MiniGraphite.datapoint("test.age")
54
+ end
55
+
56
+ def test_on_counter_not_send_udp_if_mock_mode
57
+ Dalia::MiniGraphite.config(:mock_mode => true)
58
+ UDPSocket.expects(:new).never
59
+ Dalia::MiniGraphite.counter("test.age")
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mini_graphite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Sevastianos Komianos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.5
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple wrapper for Graphite and Statsd
70
+ email:
71
+ - sebastian.komianos@daliaresearch.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-version
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - ci_script.sh
83
+ - lib/mini_graphite.rb
84
+ - lib/mini_graphite/logger.rb
85
+ - lib/mini_graphite/version.rb
86
+ - minigraphite.gemspec
87
+ - test/logger_test.rb
88
+ - test/minigraphite_test.rb
89
+ homepage: https://github.com/DaliaResearch/MiniGraphite
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simple wrapper for Graphite and Statsd
113
+ test_files:
114
+ - test/logger_test.rb
115
+ - test/minigraphite_test.rb