dogstatsd-ruby 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +36 -0
  3. data/lib/statsd.rb +171 -0
  4. metadata +114 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Rein Henrichs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ = dogstatsd-ruby {<img src="https://secure.travis-ci.org/DataDog/dogstatsd-ruby.png"/>}[http://travis-ci.org/DataDog/dogstatsd-ruby]
2
+
3
+ A Ruby client for DogStatsd, a fork of {Statsd}[https://github.com/etsy/statsd]
4
+ for Datadog. It will work fine for Statsd, but the extra Datadog features will
5
+ not work.
6
+
7
+ = Installing
8
+
9
+ Bundler:
10
+ gem 'dogstatsd-ruby', '~> 0.4.0', github: 'datadog/dogstatsd-ruby', require: 'statsd'
11
+
12
+ = Testing
13
+
14
+ Run the specs with <tt>rake spec</tt>
15
+
16
+ Run the specs and include live integration specs with <tt>LIVE=true rake spec</tt>. Note: This will test over a real UDP socket.
17
+
18
+ == Contributing to statsd
19
+
20
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
21
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
22
+ * Fork the project
23
+ * Start a feature/bugfix branch
24
+ * Commit and push until you are happy with your contribution
25
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
26
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
27
+
28
+ == Contributors
29
+
30
+ * Rein Henrichs
31
+ * Ray Krueger
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
36
+ further details.
@@ -0,0 +1,171 @@
1
+ require 'socket'
2
+
3
+ # = Statsd: A Statsd client (https://github.com/etsy/statsd)
4
+ #
5
+ # @example Set up a global Statsd client for a server on localhost:9125
6
+ # $statsd = Statsd.new 'localhost', 8125
7
+ # @example Send some stats
8
+ # $statsd.increment 'garets'
9
+ # $statsd.timing 'glork', 320
10
+ # $statsd.gauge 'bork', 100
11
+ # @example Use {#time} to time the execution of a block
12
+ # $statsd.time('account.activate') { @account.activate! }
13
+ # @example Create a namespaced statsd client and increment 'account.activate'
14
+ # statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
15
+ # statsd.increment 'activate'
16
+ class Statsd
17
+ # A namespace to prepend to all statsd calls.
18
+ attr_reader :namespace
19
+
20
+ # StatsD host. Defaults to 127.0.0.1.
21
+ attr_accessor :host
22
+
23
+ # StatsD port. Defaults to 8125.
24
+ attr_accessor :port
25
+
26
+ class << self
27
+ # Set to a standard logger instance to enable debug logging.
28
+ attr_accessor :logger
29
+ end
30
+
31
+ # Return the current version of the library.
32
+ def self.VERSION
33
+ "0.4.0"
34
+ end
35
+
36
+ # @param [String] host your statsd host
37
+ # @param [Integer] port your statsd port
38
+ def initialize(host = '127.0.0.1', port = 8125)
39
+ self.host, self.port = host, port
40
+ @prefix = nil
41
+ @socket = UDPSocket.new
42
+ end
43
+
44
+ def namespace=(namespace) #:nodoc:
45
+ @namespace = namespace
46
+ @prefix = "#{namespace}."
47
+ end
48
+
49
+ def host=(host) #:nodoc:
50
+ @host = host || '127.0.0.1'
51
+ end
52
+
53
+ def port=(port) #:nodoc:
54
+ @port = port || 8125
55
+ end
56
+
57
+ # Sends an increment (count = 1) for the given stat to the statsd server.
58
+ #
59
+ # @param [String] stat stat name
60
+ # @param [Hash] opts the options to create the metric with
61
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
62
+ # @option opts [Array<String>] :tags An array of tags
63
+ # @see #count
64
+ def increment(stat, opts={})
65
+ count stat, 1, opts
66
+ end
67
+
68
+ # Sends a decrement (count = -1) for the given stat to the statsd server.
69
+ #
70
+ # @param [String] stat stat name
71
+ # @param [Hash] opts the options to create the metric with
72
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
73
+ # @option opts [Array<String>] :tags An array of tags
74
+ # @see #count
75
+ def decrement(stat, opts={})
76
+ count stat, -1, opts
77
+ end
78
+
79
+ # Sends an arbitrary count for the given stat to the statsd server.
80
+ #
81
+ # @param [String] stat stat name
82
+ # @param [Integer] count count
83
+ # @param [Hash] opts the options to create the metric with
84
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
85
+ # @option opts [Array<String>] :tags An array of tags
86
+ def count(stat, count, opts={})
87
+ send_stats stat, count, :c, opts
88
+ end
89
+
90
+ # Sends an arbitary gauge value for the given stat to the statsd server.
91
+ #
92
+ # This is useful for recording things like available disk space,
93
+ # memory usage, and the like, which have different semantics than
94
+ # counters.
95
+ #
96
+ # @param [String] stat stat name.
97
+ # @param [Numeric] gauge value.
98
+ # @param [Hash] opts the options to create the metric with
99
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
100
+ # @option opts [Array<String>] :tags An array of tags
101
+ # @example Report the current user count:
102
+ # $statsd.gauge('user.count', User.count)
103
+ def gauge(stat, value, opts={})
104
+ send_stats stat, value, :g, opts
105
+ end
106
+
107
+ # Sends a value to be tracked as a histogram to the statsd server.
108
+ #
109
+ # @param [String] stat stat name.
110
+ # @param [Numeric] histogram value.
111
+ # @param [Hash] opts the options to create the metric with
112
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
113
+ # @option opts [Array<String>] :tags An array of tags
114
+ # @example Report the current user count:
115
+ # $statsd.histogram('user.count', User.count)
116
+ def histogram(stat, value, opts={})
117
+ send_stats stat, value, :h, opts
118
+ end
119
+
120
+ # Sends a timing (in ms) for the given stat to the statsd server. The
121
+ # sample_rate determines what percentage of the time this report is sent. The
122
+ # statsd server then uses the sample_rate to correctly track the average
123
+ # timing for the stat.
124
+ #
125
+ # @param [String] stat stat name
126
+ # @param [Integer] ms timing in milliseconds
127
+ # @param [Hash] opts the options to create the metric with
128
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
129
+ # @option opts [Array<String>] :tags An array of tags
130
+ def timing(stat, ms, opts={})
131
+ send_stats stat, ms, :ms, opts
132
+ end
133
+
134
+ # Reports execution time of the provided block using {#timing}.
135
+ #
136
+ # @param [String] stat stat name
137
+ # @param [Hash] opts the options to create the metric with
138
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
139
+ # @option opts [Array<String>] :tags An array of tags
140
+ # @yield The operation to be timed
141
+ # @see #timing
142
+ # @example Report the time (in ms) taken to activate an account
143
+ # $statsd.time('account.activate') { @account.activate! }
144
+ def time(stat, opts={})
145
+ start = Time.now
146
+ result = yield
147
+ timing(stat, ((Time.now - start) * 1000).round, opts)
148
+ result
149
+ end
150
+
151
+ private
152
+
153
+ def send_stats(stat, delta, type, opts={})
154
+ sample_rate = opts[:sample_rate] || 1
155
+ if sample_rate == 1 or rand < sample_rate
156
+ # Replace Ruby module scoping with '.' and reserved chars (: | @) with underscores.
157
+ stat = stat.to_s.gsub('::', '.').tr(':|@', '_')
158
+ rate = "|@#{sample_rate}" unless sample_rate == 1
159
+ tags = "|##{opts[:tags].join(",")}" if opts[:tags]
160
+ send_to_socket "#{@prefix}#{stat}:#{delta}|#{type}#{rate}#{tags}"
161
+ end
162
+ end
163
+
164
+ def send_to_socket(message)
165
+ self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
166
+ @socket.send(message, 0, @host, @port)
167
+ rescue => boom
168
+ self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
169
+ nil
170
+ end
171
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dogstatsd-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rein Henrichs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yard
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.6.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.6.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.8'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A Ruby DogStastd client
79
+ email: code@datadoghq.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ files:
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ - lib/statsd.rb
89
+ homepage: http://github.com/datadog/dogstatsd-ruby
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A Ruby DogStatsd client
114
+ test_files: []