graphite-api 1.0.0 → 1.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
  SHA256:
3
- metadata.gz: 0f81b82a2dff25c88b118c8734da6711683cd3dbe4befa2d5faea3172d3c752b
4
- data.tar.gz: f0c6278c05daba7691b3a854953b2a6f5d8abb0338298fddc76afa2ae8892d24
3
+ metadata.gz: '0595ca2197bbdfe81fd61a7fc06f4949adea392b2c3ca7aee0d4c461eccb7479'
4
+ data.tar.gz: 4a8df14d1e1f7a7a39fde283eae647668f08c6ce4005a5dc4e435153cba417d0
5
5
  SHA512:
6
- metadata.gz: ea462f77942592a9b31067f091b417b3e6bc13a945f68e11c5d283744e7d585a3ee4136400175c5cd9778ccb012ccfca3bd3a9ed569eba3c7e83d861736dee89
7
- data.tar.gz: 951db12f2528aeff81202121809c09d96394e25c65a2ca14a79297c0f3789c3f8109f931c28575303ae05d875b0edc0b24d9d5d2851d87f1735dec7cf4322e01
6
+ metadata.gz: 5373a60c92806962795637d6ab23bedf523db35a02fcaf6ae8f99b699c8f0e97e3aed3d952e03b891db1dde3c9388a7224a9753871b0b05f76be39bbd13730c5
7
+ data.tar.gz: 8148064e7eeeaa35847b8f50dc153cb33aa13277f2cab8b25a8c6d3570eb1bd33c59845550d2d7c25cdd58465305f24ee2d0d5df0e1362943b86a9ac2a40a287
data/README.md CHANGED
@@ -175,6 +175,30 @@ client.every 52.minutes do |c|
175
175
  end
176
176
  ```
177
177
 
178
+ Stopping the client loop
179
+ ```ruby
180
+ require 'graphite-api'
181
+
182
+ client = GraphiteAPI.new( graphite: 'tcp://graphite:2003' )
183
+
184
+ # Lets send the metric every 2 minutes.
185
+ client.every(2.minutes) do |c|
186
+ c.metrics("webServer.web01.uptime" => `uptime`.split.first.to_i)
187
+ end
188
+
189
+ sleep 2.minutes # Client will send one message.
190
+
191
+ client.pause
192
+ sleep 6.minutes # Client will not send any messages.
193
+
194
+ client.resume
195
+ sleep 4.minutes # Client will send 2 messages.
196
+
197
+ client.cancel
198
+ client.resume
199
+ sleep 6.minutes # After cancel, client will not send any more messages.
200
+ ```
201
+
178
202
  Make your own custom metrics daemons, using `client#join`
179
203
  ```ruby
180
204
  require 'graphite-api'
@@ -1,4 +1,3 @@
1
- require 'zscheduler'
2
1
  require 'graphite-api/version'
3
2
  require 'graphite-api/client'
4
3
  require 'graphite-api/cache'
@@ -24,7 +24,10 @@ module GraphiteAPI
24
24
 
25
25
  IGNORE = ["\r"]
26
26
  END_OF_STREAM = "\n"
27
- VALID_MESSAGE = /^[\w|\.|-]+ \d+(?:\.|\d)* \d+$/
27
+
28
+ # Matches the metric name (string with dots and dashes), at least one space, the metric value (int or float),
29
+ # at least one space and finally the metric timestamp (int)
30
+ VALID_MESSAGE = /^[\w.-]+ +\d+(?:\.\d+)? +\d+$/
28
31
 
29
32
  AGGREGATORS = {
30
33
  sum: ->(*args) { args.reduce(0) { |sum, x| sum + x } },
@@ -32,11 +35,11 @@ module GraphiteAPI
32
35
  replace: ->(*args) { args.last },
33
36
  }
34
37
 
35
- def initialize options
38
+ def initialize options, timers=false
36
39
  @options = options
37
40
  @queue = Queue.new
38
41
  @streamer = Hash.new {|h,k| h[k] = ""}
39
- @cache = Cache::Memory.new(options) if options[:cache]
42
+ @cache = Cache::Memory.new(options, timers) if options[:cache]
40
43
  end
41
44
 
42
45
  attr_reader :queue, :options, :streamer, :cache
@@ -1,9 +1,12 @@
1
+ require 'timers'
2
+
1
3
  module GraphiteAPI
2
4
  module Cache
3
5
  class Memory
6
+ extend Forwardable
4
7
 
5
- def initialize options
6
- Zscheduler.every(120) { clean(options[:cache]) }
8
+ def initialize options, timers=false
9
+ timers.every(120) { clean(options[:cache]) } if timers
7
10
  end
8
11
 
9
12
  def get time, key
@@ -1,22 +1,31 @@
1
1
  require 'forwardable'
2
2
  require 'thread'
3
+ require 'timers'
3
4
 
4
5
  module GraphiteAPI
5
6
  class Client
6
7
  extend Forwardable
7
8
 
8
- def_delegator Zscheduler, :loop, :stop
9
+ def_delegator :timers, :cancel
10
+ def_delegator :timers, :pause
11
+ def_delegator :timers, :resume
12
+
13
+ def_delegator :buffer, :stream
9
14
 
10
15
  attr_reader :options, :buffer, :connectors, :mu
11
16
  private :options, :buffer, :connectors, :mu
12
17
 
13
18
  def initialize opt
14
19
  @options = build_options validate opt.clone
15
- @buffer = GraphiteAPI::Buffer.new options
20
+ @buffer = GraphiteAPI::Buffer.new options, timers
16
21
  @connectors = GraphiteAPI::Connector::Group.new options
17
22
  @mu = Mutex.new
18
23
 
19
- Zscheduler.every(options[:interval],&method(:send_metrics!)) unless options[:direct]
24
+ timers.every(options[:interval], true, &method(:send_metrics!)) unless options[:direct]
25
+ end
26
+
27
+ def timers
28
+ @timers ||= Timers::Group.new.tap {|t| Thread.new { loop { t.wait } } }
20
29
  end
21
30
 
22
31
  # throw exception on Socket error
@@ -25,7 +34,7 @@ module GraphiteAPI
25
34
  end
26
35
 
27
36
  def every interval, &block
28
- Zscheduler.every( interval ) { block.arity == 1 ? block.call(self) : block.call }
37
+ @timers.every(interval) { block.arity == 1 ? block.call(self) : block.call }
29
38
  end
30
39
 
31
40
  def metrics metric, time = nil, aggregation_method = nil
@@ -73,14 +82,14 @@ module GraphiteAPI
73
82
 
74
83
  def build_options opt
75
84
  self.class.default_options.tap do |options_hash|
76
- options_hash[:backends].push opt.delete :graphite
85
+ options_hash[:backends] = Array(opt.delete :graphite)
77
86
  options_hash.merge! opt
78
87
  options_hash[:direct] = options_hash[:interval] == 0
79
88
  options_hash[:slice] = 1 if options_hash[:direct]
80
89
  end
81
90
  end
82
91
 
83
- def send_metrics!
92
+ def send_metrics! *_
84
93
  mu.synchronize { connectors.publish buffer.pull :string if buffer.new_records? }
85
94
  end
86
95
 
@@ -1 +1,20 @@
1
- require 'zscheduler/core_ext/numeric'
1
+ class Numeric
2
+ {
3
+ :year => 365 * 24 * 3600,
4
+ :month => 30 * 24 * 3600,
5
+ :week => 7 * 24 * 3600,
6
+ :day => 24 * 3600,
7
+ :hour => 3600,
8
+ :minute => 60,
9
+ :second => 1
10
+ }.each do |m, val|
11
+ respond_to? m or define_method m do
12
+ self * val
13
+ end
14
+
15
+ "#{m}s".tap do |plural|
16
+ respond_to? plural or alias_method plural, m
17
+ end
18
+
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module GraphiteAPI
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,43 +1,141 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphite-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
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: 2019-03-19 00:00:00.000000000 Z
11
+ date: 2019-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: timers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: eventmachine
15
57
  requirement: !ruby/object:Gem::Requirement
16
58
  requirements:
17
59
  - - ">="
18
60
  - !ruby/object:Gem::Version
19
- version: 0.3.3
20
- type: :runtime
61
+ version: '0'
62
+ type: :development
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
24
66
  - - ">="
25
67
  - !ruby/object:Gem::Version
26
- version: 0.3.3
68
+ version: '0'
27
69
  - !ruby/object:Gem::Dependency
28
- name: zscheduler
70
+ name: mocha
29
71
  requirement: !ruby/object:Gem::Requirement
30
72
  requirements:
31
73
  - - ">="
32
74
  - !ruby/object:Gem::Version
33
- version: 0.0.7
34
- type: :runtime
75
+ version: '0'
76
+ type: :development
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
80
  - - ">="
39
81
  - !ruby/object:Gem::Version
40
- version: 0.0.7
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov-rcov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: gem-release
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.0'
41
139
  description: Graphite API - A Simple ruby client, aggregator daemon and API tools
42
140
  email: eran@kontera.com
43
141
  executables: []