hosted_graphite 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6aa577490387b6dee333ad6d6a3525dd195a25e
4
- data.tar.gz: 647e214a6af4dc12fde2888e3b4b5a6c6b072491
3
+ metadata.gz: bf11312fbd83ce1b48007df57abe2e2c2521327b
4
+ data.tar.gz: e0fc96df3a589328f8bc9cb912944df5458cd584
5
5
  SHA512:
6
- metadata.gz: 35148133080ec01eaea23c52bcced766ca13684499fa4a704afc27c9ba06688917cb2f1f7294dd1eda9068d822aab39111b15dbf00081abdf66f33082fc92013
7
- data.tar.gz: 253428cb080c00480df01735607076f6f0d3813ccef212447c69c61e13ff51dc0fb24125987cbbfd42fb35b5465789fff42a0aed3ffea2048a858158baa77d9a
6
+ metadata.gz: 995e3f7630a2c77f77811faf2462981e0380f62b42b8d1e221cbb2db928057ca2bfb92dc37d3a4d6bd9ea5eb3994222ca5bd017c5bc13bcb23b5442d18ef6fa6
7
+ data.tar.gz: d14de66879dc278375af325ceade1c2be7a192c6e57008729221e1eb735d1522692105618e04f60bbba2d7755dd8cded1eca2e17f092d3509a98c894b0a4fdf9
data/Gemfile CHANGED
@@ -4,5 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  platforms :mri_20, :mri_21 do
7
- gem 'byebug'
7
+ gem 'pry-byebug'
8
8
  end
9
+
10
+ gem 'statsd-ruby'
data/README.md CHANGED
@@ -14,38 +14,74 @@ gem 'hosted_graphite'
14
14
 
15
15
  And then execute:
16
16
 
17
- $ bundle
17
+ ```bash
18
+ $ bundle
19
+ ```
18
20
 
19
21
  Or install it yourself as:
20
22
 
21
- $ gem install hosted_graphite
23
+ ```bash
24
+ $ gem install hosted_graphite
25
+ ```
22
26
 
23
27
  ## Usage
24
28
 
25
- Set ```ENV['HOSTEDGRAPHITE_APIKEY']``` to your api key.
29
+ Set ```ENV['HOSTEDGRAPHITE_APIKEY']``` to your hosted graphite API key.
26
30
  Your API key can be found on your [account home](https://www.hostedgraphite.com/accounts/profile/) page.
27
31
 
28
- You can also set it programatically with:
32
+ You can also set the hosted graphite API key programatically with:
33
+
34
+ ```bash
35
+ HostedGraphite.api_key = 'YOUR API KEY'
36
+ ```
37
+
38
+ #### Sending a metric via UDP
39
+ ```ruby
40
+ HostedGraphite.protocol = HostedGraphite::UDP
41
+ HostedGraphite.send_metric('foo.udp', 1.2)
42
+ ```
43
+
44
+ #### Sending a metric via TCP
45
+ ```ruby
46
+ HostedGraphite.protocol = HostedGraphite::TCP
47
+ HostedGraphite.send_metric('foo.tcp', 1.2)
48
+ ```
49
+
50
+ #### Sending a metric via HTTP
51
+ ```ruby
52
+ HostedGraphite.protocol = HostedGraphite::HTTP
53
+ HostedGraphite.send_metric('foo.http', 1.2)
54
+ ```
55
+
56
+ ### HostedStatsD
57
+
58
+ Enable Statsd on your [account](https://www.hostedgraphite.com/app/data-sources)
59
+
60
+ Ensure these line are present in your application's Gemfile:
61
+
62
+ ```ruby
63
+ gem 'hosted_graphite'
64
+ gem 'statsd-ruby'
65
+ ```
29
66
 
30
- HostedGraphite.api_key = 'YOUR API KEY'
67
+ And then require
31
68
 
32
- ### Sending a metric via UDP
33
- ```ruby
34
- HostedGraphite.protocol = HostedGraphite::UDP
35
- HostedGraphite.send_metric('foo.udp', 1.2)
36
- ```
69
+ ```ruby
70
+ require 'hosted_graphite/statsd'
71
+ ```
37
72
 
38
- ### Sending a metric via TCP
39
- ```ruby
40
- HostedGraphite.protocol = HostedGraphite::TCP
41
- HostedGraphite.send_metric('foo.tcp', 1.2)
42
- ```
73
+ #### Basic usage
43
74
 
44
- ### Sending a metric via HTTP
45
- ```ruby
46
- HostedGraphite.protocol = HostedGraphite::HTTP
47
- HostedGraphite.send_metric('foo.http', 1.2)
48
- ```
75
+ ```ruby
76
+ # Send some stats
77
+ HostedGraphite.increment 'page_views'
78
+ HostedGraphite.decrement 'likes_count'
79
+ HostedGraphite.timing 'foo', 320
80
+ HostedGraphite.gauge 'bar', 100
81
+
82
+ # Use {#time} to time the execution of a block
83
+ HostedGraphite.time('newsletter.monthly') { @newletter.deliver_now }
84
+ ```
49
85
 
50
86
  ## Contributing
51
87
 
data/Rakefile CHANGED
@@ -5,7 +5,6 @@ require 'bundler/gem_tasks'
5
5
  task default: :test
6
6
 
7
7
  Rake::TestTask.new(:test) do |t|
8
- t.libs << 'lib'
9
8
  t.pattern = 'test/test_*.rb'
10
9
  t.verbose = true
11
10
  end
@@ -1,4 +1,3 @@
1
- require 'net/http'
2
1
  # ANALYSE, is this implementation thread safe ?
3
2
  module HostedGraphite
4
3
  class HTTP < Protocol
@@ -1,3 +1,5 @@
1
+ require 'socket'
2
+ require 'net/http'
1
3
  module HostedGraphite
2
4
  class Protocol
3
5
  HOST = 'carbon.hostedgraphite.com'.freeze
@@ -22,9 +24,5 @@ module HostedGraphite
22
24
  def build_message(name, value)
23
25
  [name, value].join(' ')
24
26
  end
25
-
26
- def addr_info
27
- Addrinfo.tcp(HOST, PORT)
28
- end
29
27
  end
30
28
  end
@@ -0,0 +1,58 @@
1
+ gem 'statsd-ruby'
2
+ require 'hosted_graphite'
3
+ require 'statsd'
4
+
5
+ module HostedGraphite
6
+ class StatsD < Statsd
7
+ HOST = 'statsd.hostedgraphite.com'.freeze
8
+ PORT = 8125.freeze
9
+
10
+ def initialize
11
+ raise MissingAPIKey unless HostedGraphite.api_key
12
+ super(HOST, PORT)
13
+ @namespace = HostedGraphite.api_key
14
+ @prefix = "#{@namespace}."
15
+ end
16
+
17
+ private
18
+ def socket
19
+ Thread.current[:hostedstatsd_socket] ||= UDPSocket.new addr_family
20
+ end
21
+
22
+ def addr_family
23
+ Addrinfo.udp(@host, @port).afamily
24
+ end
25
+ end
26
+
27
+ @@statsd = StatsD.new
28
+
29
+ class << self
30
+ def increment(stat, sample_rate=1)
31
+ @@statsd.count stat, 1, sample_rate
32
+ end
33
+
34
+ def decrement(stat, sample_rate=1)
35
+ @@statsd.count stat, -1, sample_rate
36
+ end
37
+
38
+ def count(stat, count, sample_rate=1)
39
+ @@statsd.send_stats stat, count, :c, sample_rate
40
+ end
41
+
42
+ def gauge(stat, value, sample_rate=1)
43
+ @@statsd.send_stats stat, value, :g, sample_rate
44
+ end
45
+
46
+ def set(stat, value, sample_rate=1)
47
+ @@statsd.send_stats stat, value, :s, sample_rate
48
+ end
49
+
50
+ def timing(stat, ms, sample_rate=1)
51
+ @@statsd.send_stats stat, ms, :ms, sample_rate
52
+ end
53
+
54
+ def time(stat, sample_rate=1)
55
+ @@statsd.time(stat, sample_rate)
56
+ end
57
+ end
58
+ end
@@ -1,8 +1,6 @@
1
- require 'socket'
2
1
  module HostedGraphite
3
2
  class TCP < Protocol
4
3
  private
5
-
6
4
  def build_message(name, value)
7
5
  message = [name, value].join(' ') + "\n"
8
6
  [@api_key, message].join('.')
@@ -17,5 +15,9 @@ module HostedGraphite
17
15
  Thread.current[:hostedgraphite_tcpsocket] ||= TCPSocket.new addr_info.ip_address,
18
16
  addr_info.ip_port
19
17
  end
18
+
19
+ def addr_info
20
+ Addrinfo.tcp(HOST, PORT)
21
+ end
20
22
  end
21
23
  end
@@ -1,4 +1,3 @@
1
- require 'socket'
2
1
  module HostedGraphite
3
2
  class UDP < Protocol
4
3
  private
@@ -19,5 +18,9 @@ module HostedGraphite
19
18
  s
20
19
  end
21
20
  end
21
+
22
+ def addr_info
23
+ Addrinfo.udp(HOST, PORT)
24
+ end
22
25
  end
23
26
  end
@@ -1,3 +1,3 @@
1
1
  module HostedGraphite
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -22,8 +22,8 @@ module HostedGraphite
22
22
  @@api_key
23
23
  end
24
24
 
25
- def self.api_key=(api_key)
26
- @@api_key = api_key
25
+ def self.api_key=(key)
26
+ @@api_key = key
27
27
  end
28
28
 
29
29
  def self.protocol
data/test/helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  begin
2
2
  require 'byebug'
3
+ require 'pry'
3
4
  rescue LoadError
4
5
  end
6
+ require 'securerandom'
5
7
  require 'minitest/autorun'
6
8
  require 'hosted_graphite'
@@ -0,0 +1,37 @@
1
+ require_relative 'helper'
2
+
3
+ class StatsDTest < Minitest::Test
4
+ def setup
5
+ @previous_api_key = HostedGraphite.api_key
6
+ HostedGraphite.api_key = SecureRandom.uuid
7
+ require 'hosted_graphite/statsd'
8
+ end
9
+
10
+ def teardown
11
+ HostedGraphite.api_key = @previous_api_key
12
+ end
13
+
14
+ def test_respond_to_time
15
+ assert_respond_to HostedGraphite, :time
16
+ end
17
+
18
+ def test_respond_to_timing
19
+ assert_respond_to HostedGraphite, :timing
20
+ end
21
+
22
+ def test_respond_to_set
23
+ assert_respond_to HostedGraphite, :set
24
+ end
25
+
26
+ def test_respond_to_gauge
27
+ assert_respond_to HostedGraphite, :gauge
28
+ end
29
+
30
+ def test_respond_to_decrement
31
+ assert_respond_to HostedGraphite, :decrement
32
+ end
33
+
34
+ def test_respond_to_increment
35
+ assert_respond_to HostedGraphite, :increment
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hosted_graphite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-14 00:00:00.000000000 Z
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,6 +69,7 @@ files:
69
69
  - lib/hosted_graphite.rb
70
70
  - lib/hosted_graphite/http.rb
71
71
  - lib/hosted_graphite/protocol.rb
72
+ - lib/hosted_graphite/statsd.rb
72
73
  - lib/hosted_graphite/tcp.rb
73
74
  - lib/hosted_graphite/udp.rb
74
75
  - lib/hosted_graphite/version.rb
@@ -76,6 +77,7 @@ files:
76
77
  - test/helper.rb
77
78
  - test/test_http_protocol.rb
78
79
  - test/test_missing_apikey.rb
80
+ - test/test_stastd.rb
79
81
  - test/test_tcp_protocol.rb
80
82
  - test/test_udp_protocol.rb
81
83
  homepage: https://github.com/seuros/hosted_graphite
@@ -107,6 +109,7 @@ test_files:
107
109
  - test/helper.rb
108
110
  - test/test_http_protocol.rb
109
111
  - test/test_missing_apikey.rb
112
+ - test/test_stastd.rb
110
113
  - test/test_tcp_protocol.rb
111
114
  - test/test_udp_protocol.rb
112
115
  has_rdoc: