graphite-api 0.3.1 → 0.3.2

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: 971a57f1632c01fa383fcafd604fb0c020e55039
4
- data.tar.gz: b49356560ef58767756cb38c5110685307ec5113
3
+ metadata.gz: 4ca6b15c3e2131c39c0ebee88a04cda7dd4995dc
4
+ data.tar.gz: c7e5f47fc0fcf7f16e345847178288133188df6b
5
5
  SHA512:
6
- metadata.gz: 0a31a71042953ac8b4cee6c23f29b934f6202b2e0f609a6b80b99d20b122683461f2a6e2bdb8ae25fb8fb07ebe5d6239499390c8beee2f329003656e8cdbc29d
7
- data.tar.gz: 4e4d60ccf3f79cf80af1efad3018731f99e252c02ec0e281e6ec6414ee53c7e9b4fe66a8e19ad5f906a33ca7d9b25ec897d0c5299afd794ff5115a8ea97f92ac
6
+ metadata.gz: e15fe6ed1788991886a59eef139c100d6cc36c161384241a90b049715cf55d95aa315afdf493c9bb48ceeaa518f491174d0ae76c0cce1915e61634b6b54c1d22
7
+ data.tar.gz: b8d341c9549391e8e8a1af4a045444a0c84f6e82dd47fa161c892e2b636431ff059c49f21c2cb0b3a3d8fb31790d45683567561b10867929575397603777d114
data/README.md CHANGED
@@ -49,22 +49,22 @@ require 'graphite-api'
49
49
 
50
50
  options = {
51
51
  # Required: valid URI {udp,tcp}://host:port/?timeout=seconds
52
- graphite: "udp://graphite.example.com:2003",
53
-
52
+ graphite: "udp://graphite.example.com:2003",
53
+
54
54
  # Optional: add example.prefix to each key
55
55
  prefix: ["example","prefix"],
56
-
56
+
57
57
  # Optional: results are aggregated in 60 seconds slices ( default is 60 )
58
58
  slice: 60,
59
-
59
+
60
60
  # Optional: send to graphite every 60 seconds ( default is 0 - direct send )
61
61
  interval: 60,
62
-
62
+
63
63
  # Optional: set the max age in seconds for records reanimation ( default is 12 hours )
64
64
  cache: 4 * 60 * 60
65
65
  }
66
66
 
67
- client = GraphiteAPI.new options
67
+ client = GraphiteAPI.new options
68
68
  ```
69
69
 
70
70
  TCP Client
@@ -108,6 +108,28 @@ client.metrics({
108
108
  # => webServer.web01.memUsage 40 1326067060
109
109
  ```
110
110
 
111
+ Verifying connectivity
112
+ ```ruby
113
+ require 'graphite-api'
114
+
115
+ client = GraphiteAPI.new graphite: 'udp://UNKNOWN-HOST:1234'
116
+ client.check!
117
+
118
+ SocketError: udp://UNKNOWN-HOST:1234: getaddrinfo: nodename nor servname provided, or not known
119
+ from graphite-api/lib/graphite-api/connector.rb:71:in `connect'
120
+ from graphite-api/lib/graphite-api/connector.rb:71:in `block in init_udp'
121
+ from graphite-api/lib/graphite-api/connector.rb:71:in `tap'
122
+ from graphite-api/lib/graphite-api/connector.rb:71:in `init_udp'
123
+ from graphite-api/lib/graphite-api/connector.rb:37:in `socket'
124
+ from graphite-api/lib/graphite-api/connector.rb:30:in `check!'
125
+ from graphite-api/lib/graphite-api/connector.rb:88:in `block in check!'
126
+ from graphite-api/lib/graphite-api/connector.rb:86:in `each'
127
+ from github/graphite-api/lib/graphite-api/connector.rb:86:in `check!'
128
+ from graphite-api/lib/graphite-api/client.rb:22:in `check!'
129
+ from (irb):4
130
+ from ~/.rvm/rubies/ruby-2.3.3/bin/irb:11:in `<main>'
131
+ ```
132
+
111
133
  Increment records
112
134
  ```ruby
113
135
  require 'graphite-api'
@@ -217,7 +239,7 @@ More Info @ https://github.com/kontera-technologies/graphite-api
217
239
  --log-file /tmp/graphite-middleware.out \
218
240
  --daemonize \
219
241
  --graphite graphite-server:2003 \
220
- --graphite graphite-backup-server:2003
242
+ --graphite graphite-backup-server:2003
221
243
  ```
222
244
 
223
245
  * Send metrics via **UDP/TCP sockets**
@@ -238,7 +260,7 @@ example.middleware.value2 99 1334929231
238
260
  ```ruby
239
261
  require 'graphite-api'
240
262
  client = GraphiteAPI.new(:graphite => 'tcp://graphite-middleware-node:2005')
241
- client.example.middleware.value 10.2
263
+ client.example.middleware.value 10.2
242
264
  client.example.middleware.value2 27
243
265
  client.bla.bla.value2 27
244
266
  ```
@@ -1,5 +1,5 @@
1
1
  require 'forwardable'
2
-
2
+
3
3
  module GraphiteAPI
4
4
  class Client
5
5
  extend Forwardable
@@ -13,15 +13,20 @@ module GraphiteAPI
13
13
  @options = build_options validate opt.clone
14
14
  @buffer = GraphiteAPI::Buffer.new options
15
15
  @connectors = GraphiteAPI::Connector::Group.new options
16
-
16
+
17
17
  Zscheduler.every(options[:interval]) { send_metrics } unless options[:direct]
18
18
  end
19
19
 
20
+ # throw exception on Socket error
21
+ def check!
22
+ connectors.check!
23
+ end
24
+
20
25
  def every interval, &block
21
26
  Zscheduler.every( interval ) { block.arity == 1 ? block.call(self) : block.call }
22
27
  end
23
28
 
24
- def metrics metric, time = Time.now
29
+ def metrics metric, time = Time.now
25
30
  return if metric.empty?
26
31
  buffer.push :metric => metric, :time => time
27
32
  send_metrics if options[:direct]
@@ -1,77 +1,75 @@
1
- # -----------------------------------------------------
2
- # TCP Socket connection
3
- # -----------------------------------------------------
4
- # Usage:
5
- # connector = GraphiteAPI::Connector.new("localhost",2003)
6
- # connector.puts("my.metric 1092 1232123231")
7
- #
8
- # Socket:
9
- # => my.metric 1092 1232123231\n
10
- # -----------------------------------------------------
11
1
  require 'socket'
12
2
  require 'uri'
13
3
 
14
4
  module GraphiteAPI
15
5
  class Connector
6
+ attr_reader :uri
16
7
 
17
- class UDPSocket
18
- def initialize uri
19
- @uri = uri
20
- @socket = ::UDPSocket.new
21
- end
8
+ def initialize uri
9
+ @uri = URI.parse uri
10
+ @uri = @uri.host ? @uri : URI.parse("udp://#{uri}")
11
+ end
22
12
 
23
- def puts message
24
- @socket.send message, 0, @uri.host, @uri.port
13
+ def puts message
14
+ counter = 0
15
+ begin
16
+ Logger.debug [:connector, :puts, @uri.to_s, message]
17
+ socket.puts message + "\n"
18
+ rescue Exception
19
+ @socket = nil
20
+ (counter += 1) <= 5 ? retry : raise
25
21
  end
22
+ end
26
23
 
27
- def closed?
28
- @socket.closed? rescue true
29
- end
24
+ def inspect
25
+ "#<#{self.class}:#{object_id}: #{@uri}>"
30
26
  end
31
27
 
32
- class TCPSocket
33
- def initialize uri
34
- @uri = uri
35
- end
28
+ # Manually init Socket, exception will be thrown on error
29
+ def check!
30
+ socket; nil
31
+ end
36
32
 
37
- def puts message
38
- socket.puts message
39
- end
33
+ private
40
34
 
41
- def closed?
42
- socket.closed? rescue true
35
+ def socket
36
+ if @socket.nil? || @socket.closed?
37
+ @socket = @uri.scheme.eql?("tcp") ? init_tcp : init_udp
43
38
  end
44
39
 
45
- def socket
46
- @socket ||= begin
47
- host, port = @uri.host, @uri.port
48
- timeout = Hash[URI.decode_www_form(@uri.query.to_s)].fetch("timeout", 1).to_f
49
- addr = Socket.getaddrinfo host, nil, :INET
50
- sockaddr = Socket.pack_sockaddr_in port, addr[0][3]
40
+ @socket
41
+ end
51
42
 
52
- sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
53
- sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
43
+ def init_tcp
44
+ host, port = @uri.host, @uri.port
45
+ timeout = Hash[URI.decode_www_form(@uri.query.to_s)].fetch("timeout", 1).to_f
46
+ addr = Socket.getaddrinfo host, nil, :INET
47
+ sockaddr = Socket.pack_sockaddr_in port, addr[0][3]
48
+
49
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
50
+ sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
51
+ begin
52
+ sock.connect_nonblock sockaddr
53
+ rescue IO::WaitWritable
54
+ if IO.select nil, [sock], nil, timeout
54
55
  begin
55
56
  sock.connect_nonblock sockaddr
56
- rescue IO::WaitWritable
57
- if IO.select nil, [sock], nil, timeout
58
- begin
59
- sock.connect_nonblock sockaddr
60
- rescue Errno::EISCONN # success
61
- rescue
62
- sock.close
63
- raise
64
- end
65
- else
66
- sock.close
67
- raise "Connection timeout"
68
- end
57
+ rescue Errno::EISCONN # success
58
+ rescue
59
+ sock.close
60
+ raise
69
61
  end
70
- sock
71
- end #begin
62
+ else
63
+ sock.close
64
+ raise "Connection timeout"
65
+ end
66
+ end
67
+ sock
68
+ end
72
69
 
73
- end # Socket
74
- end # TCPSocket
70
+ def init_udp
71
+ UDPSocket.new.tap {|x| x.connect @uri.host, @uri.port }
72
+ end
75
73
 
76
74
  class Group
77
75
  def initialize options
@@ -82,37 +80,18 @@ module GraphiteAPI
82
80
  Logger.debug [:connector_group, :publish, messages.size, @connectors]
83
81
  Array(messages).each { |msg| @connectors.map {|c| c.puts msg} }
84
82
  end
85
- end
86
-
87
- ##############################################################################
88
-
89
- def initialize uri
90
- @uri = URI.parse uri
91
- @uri = @uri.host ? @uri : URI.parse("udp://#{uri}")
92
- end
93
-
94
- def puts message
95
- counter = 0
96
- begin
97
- Logger.debug [:connector, :puts, @uri.to_s, message]
98
- socket.puts message + "\n"
99
- rescue Exception
100
- @socket = nil
101
- (counter += 1) <= 5 ? retry : raise
102
- end
103
- end
104
-
105
- def inspect
106
- "#<#{self.class}:#{object_id}: #{@uri}>"
107
- end
108
-
109
- private
110
83
 
111
- def socket
112
- if @socket.nil? || @socket.closed?
113
- @socket = @uri.scheme.eql?("tcp") ? TCPSocket.new(@uri) : UDPSocket.new(@uri)
84
+ # init all sockets in group.
85
+ # should throw exception on Socket errors.
86
+ def check!
87
+ @connectors.each do |c|
88
+ begin
89
+ c.check!
90
+ rescue Exception => e
91
+ raise e, "#{c.uri}: #{e.message}", e.backtrace
92
+ end
93
+ end
114
94
  end
115
- @socket
116
95
  end
117
96
 
118
97
  end
@@ -1,3 +1,3 @@
1
1
  module GraphiteAPI
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphite-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eran Barak Levi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-01 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine