dawanda-statsd-client 0.1.5 → 0.1.6
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.
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/bin/statsd-send +0 -3
- data/dawanda-statsd-client.gemspec +1 -1
- data/lib/statsd/client.rb +37 -11
- data/spec/spec_helper.rb +1 -1
- metadata +3 -3
data/Rakefile
CHANGED
@@ -12,11 +12,11 @@ require 'rake'
|
|
12
12
|
require 'jeweler'
|
13
13
|
Jeweler::Tasks.new do |gem|
|
14
14
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
-
gem.name = "statsd-client"
|
16
|
-
gem.homepage = "http://github.com/
|
15
|
+
gem.name = "dawanda-statsd-client"
|
16
|
+
gem.homepage = "http://github.com/dawanda/statsd-client"
|
17
17
|
gem.license = "MIT"
|
18
18
|
gem.summary = "Client for statds"
|
19
|
-
gem.description = %Q{
|
19
|
+
gem.description = %Q{A Ruby client for statsd}
|
20
20
|
gem.email = "tom@tomtaylor.co.uk"
|
21
21
|
gem.authors = ["Tom Taylor"]
|
22
22
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/bin/statsd-send
CHANGED
data/lib/statsd/client.rb
CHANGED
@@ -5,14 +5,26 @@ require 'yaml'
|
|
5
5
|
class Statsd
|
6
6
|
class << self
|
7
7
|
def increment(metric, options={})
|
8
|
-
|
9
|
-
|
8
|
+
if options.is_a?(Fixnum)
|
9
|
+
value = options
|
10
|
+
sample_rate = 1
|
11
|
+
else
|
12
|
+
value = (options[:by] || 1)
|
13
|
+
sample_rate = (options[:sample_rate] || 1)
|
14
|
+
end
|
15
|
+
client.update_stats(metric, value*factor, sample_rate)
|
10
16
|
end
|
11
17
|
alias_method :inc, :increment
|
12
18
|
|
13
19
|
def decrement(metric, options={})
|
14
|
-
|
15
|
-
|
20
|
+
if options.is_a?(Fixnum)
|
21
|
+
value = options
|
22
|
+
sample_rate = 1
|
23
|
+
else
|
24
|
+
value = (options[:by] || 1)
|
25
|
+
sample_rate = (options[:sample_rate] || 1)
|
26
|
+
end
|
27
|
+
client.update_stats(metric, value*factor*(-1), sample_rate)
|
16
28
|
end
|
17
29
|
alias_method :dec, :decrement
|
18
30
|
|
@@ -23,7 +35,7 @@ class Statsd
|
|
23
35
|
|
24
36
|
def client
|
25
37
|
return Statsd::DummyClient if deactivated?
|
26
|
-
@client ||= Statsd::Client.new(host, port)
|
38
|
+
@client ||= Statsd::Client.new(host, port, tcp?)
|
27
39
|
end
|
28
40
|
|
29
41
|
def config
|
@@ -42,6 +54,10 @@ class Statsd
|
|
42
54
|
def port
|
43
55
|
config['port'] || 8125
|
44
56
|
end
|
57
|
+
|
58
|
+
def tcp?
|
59
|
+
config['tcp']
|
60
|
+
end
|
45
61
|
|
46
62
|
# statds reports with default configs 1/10 of actual value
|
47
63
|
def factor
|
@@ -64,14 +80,15 @@ class Statsd
|
|
64
80
|
|
65
81
|
class Client
|
66
82
|
VERSION = File.read( File.join(File.dirname(__FILE__),'..', '..', 'VERSION') ).strip
|
67
|
-
attr_reader :host, :port
|
83
|
+
attr_reader :host, :port, :tcp
|
68
84
|
|
69
85
|
# Initializes a Statsd client.
|
70
86
|
#
|
71
87
|
# @param [String] host
|
72
88
|
# @param [Integer] port
|
73
|
-
|
74
|
-
|
89
|
+
# @param [Boolean] tcp
|
90
|
+
def initialize(host = 'localhost', port = 8125, tcp = false)
|
91
|
+
@host, @port, @tcp = host, port, tcp
|
75
92
|
end
|
76
93
|
|
77
94
|
# Sends timing statistics.
|
@@ -123,6 +140,7 @@ class Statsd
|
|
123
140
|
private
|
124
141
|
|
125
142
|
def send(data, sample_rate = 1)
|
143
|
+
#puts "sending #{data} with sample #{sample_rate}"
|
126
144
|
sampled_data = {}
|
127
145
|
|
128
146
|
if sample_rate < 1
|
@@ -135,12 +153,20 @@ class Statsd
|
|
135
153
|
sampled_data = data
|
136
154
|
end
|
137
155
|
|
138
|
-
|
139
|
-
|
156
|
+
if self.tcp
|
157
|
+
socket = TCPSocket.new( self.host, self.port)
|
158
|
+
else
|
159
|
+
socket = UDPSocket.new
|
160
|
+
end
|
161
|
+
|
140
162
|
begin
|
141
163
|
sampled_data.each do |k,v|
|
142
164
|
message = [k,v].join(':')
|
143
|
-
|
165
|
+
if self.tcp
|
166
|
+
socket.send(message, 0)
|
167
|
+
else
|
168
|
+
socket.send(message, 0, self.host, self.port)
|
169
|
+
end
|
144
170
|
end
|
145
171
|
rescue Exception => e
|
146
172
|
puts "Unexpected error: #{e}"
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
require 'rspec'
|
4
|
-
require 'statsd
|
4
|
+
require 'statsd/client'
|
5
5
|
|
6
6
|
# Requires supporting files with custom matchers and macros, etc,
|
7
7
|
# in ./support/ and its subdirectories.
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dawanda-statsd-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tom Taylor
|