simple_metrics_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_metrics_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Frederik Dietz
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.markdown ADDED
@@ -0,0 +1,29 @@
1
+ SimpleMetrics Ruby Client
2
+ =========================
3
+
4
+ SimpleMetrics makes it easy to collect and aggregate data (specifically counters, timers and events).
5
+
6
+ This is the Ruby client library gem.
7
+
8
+ Usage
9
+ ======
10
+
11
+ Initialize client:
12
+
13
+ client = SimpleMetrics::Client.new("localhost")
14
+
15
+ sends "com.example.test1:1|c" via UDP:
16
+
17
+ client.increment("com.example.test1")
18
+
19
+ sends "com.example.test1:5|c" (a counter with a relative value of 5):
20
+
21
+ client.count("com.example.test1", 5)
22
+
23
+ sends "com.example.test1:5|c|@0.1" with a sample rate of 10%:
24
+
25
+ client.count("com.example.test1", 5, 0.1)
26
+
27
+ sends "com.example.test1:5|g" (meaning gauge, an absolute value of 5):
28
+
29
+ client.gauge("com.example.test1", 5)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake'
4
+ require 'rspec/core/rake_task'
5
+
6
+ require 'simple_metrics-client'
7
+
8
+ RSpec::Core::RakeTask.new
9
+ task :default => :spec
10
+ task :test => :spec
11
+
12
+
@@ -0,0 +1 @@
1
+ require "simple_metrics/client"
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ require "socket"
3
+ require "logger"
4
+
5
+ module SimpleMetrics
6
+ class Client
7
+
8
+ attr_reader :host, :port
9
+
10
+ def self.logger=(logger)
11
+ @@logger = logger
12
+ end
13
+
14
+ def self.logger
15
+ @@logger ||= Logger.new($stdout)
16
+ end
17
+
18
+ def initialize(host, port = 8125)
19
+ @host, @port = host, port
20
+ end
21
+
22
+ # send relative value
23
+ def increment(stat, sample_rate = 1)
24
+ count(stat, 1, sample_rate)
25
+ end
26
+
27
+ # send relative value
28
+ def count(stat, count, sample_rate = 1)
29
+ send_data( stat, count, 'c', sample_rate)
30
+ end
31
+
32
+ # send absolute value
33
+ def gauge(stat, value)
34
+ send_data(stat, value, 'g')
35
+ end
36
+
37
+ private
38
+
39
+ def sampled(sample_rate, &block)
40
+ if sample_rate < 1
41
+ block.call if rand <= sample_rate
42
+ else
43
+ block.call
44
+ end
45
+ end
46
+
47
+ def send_data(stat, delta, type, sample_rate = 1)
48
+ sampled(sample_rate) do
49
+ data = "#{stat}:#{delta}|#{type}" # TODO: check stat is valid
50
+ data << "|@#{sample_rate}" if sample_rate < 1
51
+ send_to_socket(data)
52
+ end
53
+ end
54
+
55
+ def send_to_socket(data)
56
+ logger.debug "SimpleMetrics Client send: #{data}"
57
+ socket.send(data, 0, @host, @port)
58
+ rescue => e
59
+ logger.error "SimpleMetrics Client error: #{e}"
60
+ end
61
+
62
+ def socket
63
+ @socket ||= UDPSocket.new
64
+ end
65
+
66
+ def logger
67
+ self.class.logger
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleMetrics
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/simple_metrics/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Frederik Dietz"]
6
+ gem.email = ["fdietz@gmail.com"]
7
+ gem.description = %q{Simple Metrics Ruby Client}
8
+ gem.summary = %q{Simple Metrics Ruby Client}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "simple_metrics_client"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SimpleMetrics::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "rr"
21
+
22
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ module SimpleMetrics
5
+
6
+ describe Client do
7
+
8
+ before do
9
+ @client = SimpleMetrics::Client.new('localhost')
10
+ end
11
+
12
+ describe "#initialize" do
13
+ it "sets host and port" do
14
+ @client.host.should == 'localhost'
15
+ @client.port.should == 8125
16
+ end
17
+ end
18
+
19
+ describe "#increment" do
20
+ it "sends increment correctly" do
21
+ mock(@client).send_to_socket("test:1|c")
22
+ @client.increment("test")
23
+ end
24
+ end
25
+
26
+ describe "#count" do
27
+ it "sends count data correctly" do
28
+ mock(@client).send_to_socket("test:5|c")
29
+ @client.count("test", 5)
30
+ end
31
+ end
32
+
33
+ describe "#gauge" do
34
+ it "sends gauge data correctly" do
35
+ mock(@client).send_to_socket("test:10|g")
36
+ @client.gauge("test", 10)
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,5 @@
1
+ require 'simple_metrics-client'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rr
5
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_metrics_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Frederik Dietz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70294066694240 !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: *70294066694240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70294066693760 !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: *70294066693760
36
+ - !ruby/object:Gem::Dependency
37
+ name: rr
38
+ requirement: &70294066693320 !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: *70294066693320
47
+ description: Simple Metrics Ruby Client
48
+ email:
49
+ - fdietz@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.markdown
58
+ - Rakefile
59
+ - lib/simple_metrics-client.rb
60
+ - lib/simple_metrics/client.rb
61
+ - lib/simple_metrics/version.rb
62
+ - simple_metrics-client.gemspec
63
+ - spec/client_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: 4455586405516115580
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 4455586405516115580
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.17
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Simple Metrics Ruby Client
95
+ test_files:
96
+ - spec/client_spec.rb
97
+ - spec/spec_helper.rb