hosted_graphite 0.1.0 → 0.1.1

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: 8feed7b2061113ad3af0e7defd664a1a73beb2f0
4
- data.tar.gz: 68e0bbcafce9ab5d6dcfa3718b708ec330d562a1
3
+ metadata.gz: 5a96d4721686e9b29cded57b6624a05bec73484c
4
+ data.tar.gz: e2350801a97eb3b09b97e2ea36e8c97241fd4463
5
5
  SHA512:
6
- metadata.gz: 432f36fad8a68e71932ba818fbdb107bbad05ed66b37c8f9c45a90f3b5e1957a412aa2c901269ff430d8a6baa9310915f5d5f7e3f5d1c881008dca5df31f380e
7
- data.tar.gz: 3599c2698e689e9ef331510032cce5d6de8a0cfe99cc944cdcceefb5d2e57ec236b8c19d443319ffb1086e12d8f0698440e2c29311bf2fecc3c39aba8bbfffbe
6
+ metadata.gz: 9f6f7351e6d2be3135e9a6f88772e6e3c5d81d1fb71baffa8558f5b4404ea7d4a3592f380b38e93f505013272220f574d0e69ae33c5fecdb97fea05b67d2106d
7
+ data.tar.gz: 4c93fd58f0c53687a8165f337eb532c404c4c132b54c8862eae29dbabad6ca06b7ebdfb19f602281ccf09b6c4b23dffd263a01b221fad703513d708dfbfde91d
data/.travis.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.3.1
3
4
  - 2.2
4
5
  - 2.1
5
6
  - 2.0.0
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ 1.0.1 : Add sidekiq extension
data/README.md CHANGED
@@ -65,15 +65,10 @@ gem 'hosted_graphite'
65
65
  gem 'statsd-ruby'
66
66
  ```
67
67
 
68
- And then require
69
-
70
- ```ruby
71
- require 'hosted_graphite/statsd'
72
- ```
73
-
74
68
  #### Basic usage
75
69
 
76
70
  ```ruby
71
+ HostedGraphite.protocol = :statsd
77
72
  # Send some stats
78
73
  HostedGraphite.increment 'page_views'
79
74
  HostedGraphite.decrement 'likes_count'
@@ -91,9 +86,10 @@ HostedGraphite.time('newsletter.monthly') { @newsletter.deliver_now }
91
86
 
92
87
  Sidekiq.configure_server do |config|
93
88
  config.server_middleware do |chain|
94
- # chain.prepend Sidekiq::Middleware::Server::StatsdMetrics, namespace: 'my_app'
89
+ # my_app is an optional namespace
90
+ # chain.prepend HostedGraphite::Ext::Sidekiq::StatsdMetrics, 'my_app'
95
91
  # or
96
- # chain.prepend Sidekiq::Middleware::Server::Metrics
92
+ # chain.prepend HostedGraphite::Ext::Sidekiq::Metrics
97
93
  end
98
94
  end
99
95
  ```
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'hosted_graphite'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,17 +1,16 @@
1
1
  require 'forwardable'
2
2
  require 'hosted_graphite/version'
3
3
  require 'hosted_graphite/protocol'
4
- require 'hosted_graphite/udp'
5
- require 'hosted_graphite/tcp'
6
- require 'hosted_graphite/http'
4
+
7
5
 
8
6
  module HostedGraphite
9
7
  extend Forwardable
10
8
  extend self
9
+ attr_reader :namespace, :protocol
10
+ attr_writer :namespace, :api_key
11
11
 
12
12
  def_delegators :protocol, :send_metric
13
13
  module_function :send_metric
14
- @protocol = nil
15
14
 
16
15
  class MissingAPIKey < StandardError
17
16
  def initialize
@@ -23,21 +22,14 @@ module HostedGraphite
23
22
  @api_key ||= ENV['HOSTEDGRAPHITE_APIKEY']
24
23
  end
25
24
 
26
- def api_key=(key)
27
- @api_key = key
28
- end
25
+ def protocol=(protocol)
26
+ protocol = protocol.to_s.downcase
27
+ require "hosted_graphite/protocols/#{protocol}"
28
+ @protocol = @registred_protocols[protocol].new
29
+ end
29
30
 
30
- def protocol
31
- @protocol
32
- end
33
-
34
- def protocol=(protocol)
35
- case protocol
36
- when Class
37
- @protocol = protocol.new
38
- when String, Symbol
39
- protocol = protocol.to_s.upcase
40
- @protocol = const_get(protocol).new
41
- end
31
+ @registred_protocols = {}
32
+ private def register(name, klass)
33
+ @registred_protocols[name] = klass
42
34
  end
43
35
  end
@@ -1,18 +1,18 @@
1
- gem 'sidekiq', '>= 4.1.1'
1
+ gem 'sidekiq', '>= 4.1.2'
2
2
  require 'sidekiq'
3
3
 
4
4
  module HostedGraphite
5
5
  module Ext
6
6
  module Sidekiq
7
7
  class Metrics
8
- def initialize(namespace:)
8
+ def initialize(namespace=nil)
9
9
  @client = HostedGraphite
10
- @namespace = namespace
10
+ HostedGraphite.namespace = namespace
11
11
  end
12
12
 
13
13
  def call(worker, msg, _, &block)
14
14
  w = msg['wrapped'] || worker.class.to_s
15
- w = [@namespace,'jobs', w].compact.join('.')
15
+ w = ['jobs', w].compact.join('.')
16
16
  begin
17
17
  @client.increment("#{w}.performed")
18
18
  @client.time("#{w}.time", &block)
@@ -25,10 +25,10 @@ module HostedGraphite
25
25
  end
26
26
 
27
27
  class StatsdMetrics < Metrics
28
- def initialize(namespace:)
28
+ def initialize(namespace=nil)
29
29
  super
30
- require 'hosted_graphite/statsd'
31
- @client = StatsD.new
30
+ require 'hosted_graphite/protocols/statsd'
31
+ @client = STATSD.new
32
32
  end
33
33
  end
34
34
  end
@@ -16,4 +16,5 @@ module HostedGraphite
16
16
  @http.request(req)
17
17
  end
18
18
  end
19
+ register('http', HTTP)
19
20
  end
@@ -0,0 +1,24 @@
1
+ gem 'statsd-ruby'
2
+ require 'hosted_graphite'
3
+ require 'statsd'
4
+
5
+ module HostedGraphite
6
+ def_delegators :statsd, :increment, :decrement, :count, :gauge, :set, :timing, :time
7
+
8
+ class STATSD < Statsd
9
+ HOST = 'statsd.hostedgraphite.com'.freeze
10
+ PORT = 8125.freeze
11
+
12
+ def initialize
13
+ raise MissingAPIKey unless HostedGraphite.api_key
14
+ super(HOST, PORT)
15
+ self.namespace = [HostedGraphite.api_key,HostedGraphite.namespace].join('.')
16
+ end
17
+ end
18
+
19
+ def statsd
20
+ @statsd ||= STATSD.new
21
+ end
22
+
23
+ register('statsd', STATSD)
24
+ end
@@ -20,4 +20,5 @@ module HostedGraphite
20
20
  Addrinfo.tcp(HOST, PORT)
21
21
  end
22
22
  end
23
+ register('tcp', TCP)
23
24
  end
@@ -23,4 +23,6 @@ module HostedGraphite
23
23
  Addrinfo.udp(HOST, PORT)
24
24
  end
25
25
  end
26
+
27
+ register('udp', UDP)
26
28
  end
@@ -1,33 +1 @@
1
- gem 'statsd-ruby'
2
- require 'hosted_graphite'
3
- require 'statsd'
4
-
5
- module HostedGraphite
6
- def_delegators :statsd, :increment, :decrement, :count, :gauge, :set, :timing, :time
7
-
8
- class StatsD < Statsd
9
- HOST = 'statsd.hostedgraphite.com'.freeze
10
- PORT = 8125.freeze
11
-
12
- def initialize
13
- raise MissingAPIKey unless HostedGraphite.api_key
14
- super(HOST, PORT)
15
- @namespace = HostedGraphite.api_key
16
- @prefix = "#{@namespace}."
17
- end
18
-
19
- private
20
- def socket
21
- Thread.current[:hostedstatsd_socket] ||= UDPSocket.new addr_family
22
- end
23
-
24
- def addr_family
25
- Addrinfo.udp(@host, @port).afamily
26
- end
27
- end
28
-
29
- def statsd
30
- @statsd ||= StatsD.new
31
- end
32
-
33
- end
1
+ HostedGraphite.protocol = :stastd
@@ -1,5 +1,8 @@
1
1
  require 'hosted_graphite'
2
2
  require 'securerandom'
3
+ require 'hosted_graphite/protocols/http'
4
+ require 'hosted_graphite/protocols/tcp'
5
+ require 'hosted_graphite/protocols/udp'
3
6
  module HostedGraphite
4
7
  @api_key = SecureRandom.hex
5
8
  [TCP, UDP, HTTP].each do |protocol|
@@ -1,3 +1,3 @@
1
1
  module HostedGraphite
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/test/helper.rb CHANGED
@@ -7,3 +7,4 @@ require 'securerandom'
7
7
  require 'minitest/autorun'
8
8
  require 'hosted_graphite/testing'
9
9
 
10
+ HostedGraphite.api_key = SecureRandom.uuid
@@ -3,14 +3,7 @@ require_relative 'helper'
3
3
  class HTTPProtocolTest < Minitest::Test
4
4
  attr_reader :api_key
5
5
  def setup
6
- @previous_api_key = HostedGraphite.api_key
7
- @api_key = SecureRandom.uuid
8
- HostedGraphite.api_key = @api_key
9
- HostedGraphite.protocol = HostedGraphite::HTTP
10
- end
11
-
12
- def teardown
13
- HostedGraphite.api_key = @previous_api_key
6
+ HostedGraphite.protocol = :http
14
7
  end
15
8
 
16
9
  def test_http_protocol
data/test/test_stastd.rb CHANGED
@@ -2,13 +2,7 @@ require_relative 'helper'
2
2
 
3
3
  class StatsDTest < Minitest::Test
4
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
5
+ HostedGraphite.protocol = 'statsd'
12
6
  end
13
7
 
14
8
  def test_respond_to_time
@@ -4,22 +4,15 @@ class TCPProtocolTest < Minitest::Test
4
4
  attr_reader :api_key
5
5
 
6
6
  def setup
7
- @previous_api_key = HostedGraphite.api_key
8
- @api_key = SecureRandom.uuid
9
- HostedGraphite.api_key = @api_key
10
7
  HostedGraphite.protocol = 'tcp'
11
8
  end
12
9
 
13
- def teardown
14
- HostedGraphite.api_key = @previous_api_key
15
- end
16
-
17
10
  def test_tcp_protocol
18
11
  assert_instance_of HostedGraphite::TCP, HostedGraphite.protocol
19
12
  end
20
13
 
21
14
  def test_correct_query
22
15
  query = HostedGraphite.send_metric('foo', 1.2, 1421792423)
23
- assert_equal "#{api_key}.foo 1.2 1421792423\n", query
16
+ assert_equal "#{HostedGraphite.api_key}.foo 1.2 1421792423\n", query
24
17
  end
25
18
  end
@@ -3,22 +3,15 @@ require_relative 'helper'
3
3
  class UDPProtocolTest < Minitest::Test
4
4
  attr_reader :api_key
5
5
  def setup
6
- @previous_api_key = HostedGraphite.api_key
7
- @api_key = SecureRandom.uuid
8
- HostedGraphite.api_key = @api_key
9
6
  HostedGraphite.protocol = :udp
10
7
  end
11
8
 
12
- def teardown
13
- HostedGraphite.api_key = @previous_api_key
14
- end
15
-
16
9
  def test_udp_protocol
17
10
  assert_instance_of HostedGraphite::UDP, HostedGraphite.protocol
18
11
  end
19
12
 
20
13
  def test_correct_query
21
14
  query = HostedGraphite.send_metric('foo', 1.2, 1421792423)
22
- assert_equal "#{api_key}.foo 1.2 1421792423\n", query
15
+ assert_equal "#{HostedGraphite.api_key}.foo 1.2 1421792423\n", query
23
16
  end
24
17
  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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-20 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,19 +61,23 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".travis.yml"
64
+ - CHANGELOG.md
64
65
  - Gemfile
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
69
+ - bin/console
70
+ - bin/setup
68
71
  - hosted_graphite.gemspec
69
72
  - lib/hosted_graphite.rb
70
73
  - lib/hosted_graphite/ext/sidekiq.rb
71
- - lib/hosted_graphite/http.rb
72
74
  - lib/hosted_graphite/protocol.rb
75
+ - lib/hosted_graphite/protocols/http.rb
76
+ - lib/hosted_graphite/protocols/statsd.rb
77
+ - lib/hosted_graphite/protocols/tcp.rb
78
+ - lib/hosted_graphite/protocols/udp.rb
73
79
  - lib/hosted_graphite/statsd.rb
74
- - lib/hosted_graphite/tcp.rb
75
80
  - lib/hosted_graphite/testing.rb
76
- - lib/hosted_graphite/udp.rb
77
81
  - lib/hosted_graphite/version.rb
78
82
  - test/helper.rb
79
83
  - test/test_http_protocol.rb