hosted_graphite 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3fba3a1689d415b15ce74911d5b98cd725bc1a6
4
- data.tar.gz: 58dd78d4c3a2344105d32b6b594612daae6355e2
3
+ metadata.gz: 8feed7b2061113ad3af0e7defd664a1a73beb2f0
4
+ data.tar.gz: 68e0bbcafce9ab5d6dcfa3718b708ec330d562a1
5
5
  SHA512:
6
- metadata.gz: 595e7e91127cb4623e98a963ce6cc62f51abaa014f5d459adfa8ea2bce64c510678873369b58d8cdf624924b01d67a204f3f9f43109037d78671b241cc2e3f58
7
- data.tar.gz: 274c7b5e8b1b4b307645b182903406a16aa5f58d982ad8fb97809b0f418e630bb489794b2809002353fcbb3681576378e3e880d2c4a73fb7665bf435781f5c06
6
+ metadata.gz: 432f36fad8a68e71932ba818fbdb107bbad05ed66b37c8f9c45a90f3b5e1957a412aa2c901269ff430d8a6baa9310915f5d5f7e3f5d1c881008dca5df31f380e
7
+ data.tar.gz: 3599c2698e689e9ef331510032cce5d6de8a0cfe99cc944cdcceefb5d2e57ec236b8c19d443319ffb1086e12d8f0698440e2c29311bf2fecc3c39aba8bbfffbe
data/.gitignore CHANGED
@@ -13,3 +13,5 @@
13
13
  *.a
14
14
  mkmf.log
15
15
  /.idea
16
+ .ruby-version
17
+ .ruby-gemset
data/README.md CHANGED
@@ -84,6 +84,20 @@ HostedGraphite.gauge 'bar', 100
84
84
  HostedGraphite.time('newsletter.monthly') { @newsletter.deliver_now }
85
85
  ```
86
86
 
87
+ ### Extensions
88
+ #### Sidekiq
89
+ ```ruby
90
+ require 'hosted_graphite/ext/sidekiq'
91
+
92
+ Sidekiq.configure_server do |config|
93
+ config.server_middleware do |chain|
94
+ # chain.prepend Sidekiq::Middleware::Server::StatsdMetrics, namespace: 'my_app'
95
+ # or
96
+ # chain.prepend Sidekiq::Middleware::Server::Metrics
97
+ end
98
+ end
99
+ ```
100
+
87
101
  ## Contributing
88
102
 
89
103
  1. Fork it ( https://github.com/seuros/hosted_graphite/fork )
@@ -7,10 +7,11 @@ require 'hosted_graphite/http'
7
7
 
8
8
  module HostedGraphite
9
9
  extend Forwardable
10
+ extend self
11
+
10
12
  def_delegators :protocol, :send_metric
11
13
  module_function :send_metric
12
- @@api_key = ENV['HOSTEDGRAPHITE_APIKEY']
13
- @@protocol = nil
14
+ @protocol = nil
14
15
 
15
16
  class MissingAPIKey < StandardError
16
17
  def initialize
@@ -18,27 +19,25 @@ module HostedGraphite
18
19
  end
19
20
  end
20
21
 
21
- class << self
22
- def api_key
23
- @@api_key
24
- end
22
+ def api_key
23
+ @api_key ||= ENV['HOSTEDGRAPHITE_APIKEY']
24
+ end
25
25
 
26
- def api_key=(key)
27
- @@api_key = key
28
- end
26
+ def api_key=(key)
27
+ @api_key = key
28
+ end
29
29
 
30
- def protocol
31
- @@protocol
32
- end
30
+ def protocol
31
+ @protocol
32
+ end
33
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
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
42
41
  end
43
42
  end
44
43
  end
@@ -0,0 +1,36 @@
1
+ gem 'sidekiq', '>= 4.1.1'
2
+ require 'sidekiq'
3
+
4
+ module HostedGraphite
5
+ module Ext
6
+ module Sidekiq
7
+ class Metrics
8
+ def initialize(namespace:)
9
+ @client = HostedGraphite
10
+ @namespace = namespace
11
+ end
12
+
13
+ def call(worker, msg, _, &block)
14
+ w = msg['wrapped'] || worker.class.to_s
15
+ w = [@namespace,'jobs', w].compact.join('.')
16
+ begin
17
+ @client.increment("#{w}.performed")
18
+ @client.time("#{w}.time", &block)
19
+ @client.increment("#{w}.succeed")
20
+ rescue Exception
21
+ @client.increment("#{w}.failed")
22
+ raise
23
+ end
24
+ end
25
+ end
26
+
27
+ class StatsdMetrics < Metrics
28
+ def initialize(namespace:)
29
+ super
30
+ require 'hosted_graphite/statsd'
31
+ @client = StatsD.new
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -11,7 +11,7 @@ module HostedGraphite
11
11
 
12
12
  def send_message(message)
13
13
  req = Net::HTTP::Post.new(@uri.request_uri)
14
- req.basic_auth @api_key, nil
14
+ req.basic_auth api_key, nil
15
15
  req.body = message
16
16
  @http.request(req)
17
17
  end
@@ -6,13 +6,15 @@ module HostedGraphite
6
6
  API_URI = 'https://hostedgraphite.com/api/v1/sink'.freeze
7
7
  PORT = 2003.freeze
8
8
 
9
- def initialize
10
- fail MissingAPIKey unless HostedGraphite.api_key
11
- @api_key = HostedGraphite.api_key
9
+ def api_key
10
+ HostedGraphite.api_key
12
11
  end
13
12
 
14
13
  def send_metric(name, value, timestamp = nil)
14
+ raise MissingAPIKey unless api_key
15
15
  send_message(build_message(name, value, timestamp))
16
+ rescue MissingAPIKey => e
17
+ raise e
16
18
  rescue => e
17
19
  # set HOSTEDGRAPHITE_DEBUG to to raise errors instead of silencing them.
18
20
  raise e if ENV['HOSTEDGRAPHITE_DEBUG']
@@ -3,6 +3,8 @@ require 'hosted_graphite'
3
3
  require 'statsd'
4
4
 
5
5
  module HostedGraphite
6
+ def_delegators :statsd, :increment, :decrement, :count, :gauge, :set, :timing, :time
7
+
6
8
  class StatsD < Statsd
7
9
  HOST = 'statsd.hostedgraphite.com'.freeze
8
10
  PORT = 8125.freeze
@@ -11,7 +13,7 @@ module HostedGraphite
11
13
  raise MissingAPIKey unless HostedGraphite.api_key
12
14
  super(HOST, PORT)
13
15
  @namespace = HostedGraphite.api_key
14
- @prefix = "#{@namespace}."
16
+ @prefix = "#{@namespace}."
15
17
  end
16
18
 
17
19
  private
@@ -24,35 +26,8 @@ module HostedGraphite
24
26
  end
25
27
  end
26
28
 
27
- @@statsd = StatsD.new
28
-
29
- class << self
30
- def increment(stat, sample_rate=1)
31
- @@statsd.increment stat, sample_rate
32
- end
33
-
34
- def decrement(stat, sample_rate=1)
35
- @@statsd.decrement stat, sample_rate
36
- end
37
-
38
- def count(stat, count, sample_rate=1)
39
- @@statsd.count stat, count, sample_rate
40
- end
41
-
42
- def gauge(stat, value, sample_rate=1)
43
- @@statsd.gauge stat, value, sample_rate
44
- end
45
-
46
- def set(stat, value, sample_rate=1)
47
- @@statsd.set stat, value, sample_rate
48
- end
49
-
50
- def timing(stat, ms, sample_rate=1)
51
- @@statsd.timing stat, ms, sample_rate
52
- end
53
-
54
- def time(stat, sample_rate=1, &blk)
55
- @@statsd.time stat, sample_rate, &blk
56
- end
29
+ def statsd
30
+ @statsd ||= StatsD.new
57
31
  end
32
+
58
33
  end
@@ -3,7 +3,7 @@ module HostedGraphite
3
3
  private
4
4
  def build_message(name, value, timestamp = nil)
5
5
  message = [name, value, timestamp].compact.join(' ') + "\n"
6
- [@api_key, message].join('.')
6
+ [api_key, message].join('.')
7
7
  end
8
8
 
9
9
  def send_message(message)
@@ -1,7 +1,7 @@
1
1
  require 'hosted_graphite'
2
2
  require 'securerandom'
3
3
  module HostedGraphite
4
- @@api_key = SecureRandom.hex
4
+ @api_key = SecureRandom.hex
5
5
  [TCP, UDP, HTTP].each do |protocol|
6
6
  protocol.class_eval do
7
7
 
@@ -3,7 +3,7 @@ module HostedGraphite
3
3
  private
4
4
  def build_message(name, value, timestamp = nil)
5
5
  message = [name, value, timestamp].compact.join(' ') + "\n"
6
- [@api_key, message].join('.')
6
+ [api_key, message].join('.')
7
7
  end
8
8
 
9
9
  def send_message(message)
@@ -1,3 +1,3 @@
1
1
  module HostedGraphite
2
- VERSION = '0.0.8'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -12,19 +12,19 @@ class MissingAPIKeyTest < Minitest::Test
12
12
 
13
13
  def test_udp_protocol
14
14
  assert_raises HostedGraphite::MissingAPIKey do
15
- HostedGraphite::UDP.new
15
+ HostedGraphite::UDP.new.send_metric('foo', 1)
16
16
  end
17
17
  end
18
18
 
19
19
  def test_tcp_protocol
20
20
  assert_raises HostedGraphite::MissingAPIKey do
21
- HostedGraphite::TCP.new
21
+ HostedGraphite::TCP.new.send_metric('foo', 1)
22
22
  end
23
23
  end
24
24
 
25
25
  def test_http_protocol
26
26
  assert_raises HostedGraphite::MissingAPIKey do
27
- HostedGraphite::HTTP.new
27
+ HostedGraphite::HTTP.new.send_metric('foo', 1)
28
28
  end
29
29
  end
30
30
  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.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-24 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - Rakefile
68
68
  - hosted_graphite.gemspec
69
69
  - lib/hosted_graphite.rb
70
+ - lib/hosted_graphite/ext/sidekiq.rb
70
71
  - lib/hosted_graphite/http.rb
71
72
  - lib/hosted_graphite/protocol.rb
72
73
  - lib/hosted_graphite/statsd.rb
@@ -100,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  version: '0'
101
102
  requirements: []
102
103
  rubyforge_project:
103
- rubygems_version: 2.2.2
104
+ rubygems_version: 2.5.1
104
105
  signing_key:
105
106
  specification_version: 4
106
107
  summary: A Ruby client for HostedGraphite
@@ -111,4 +112,3 @@ test_files:
111
112
  - test/test_stastd.rb
112
113
  - test/test_tcp_protocol.rb
113
114
  - test/test_udp_protocol.rb
114
- has_rdoc: