statsd-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/.gitignore +1 -0
  2. data/README.md +26 -0
  3. data/lib/statsd.rb +65 -0
  4. data/statsd-client.gemspec +20 -0
  5. metadata +70 -0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ statsd-client
2
+ =============
3
+
4
+ This is a simple client for [statsd](https://github.com/etsy/statsd). It's
5
+ roughly equivalent to the php and python examples included in the statsd
6
+ repo. I put it in a gem to make it easy to install, reuse, etc.
7
+
8
+ Example
9
+ -------
10
+
11
+ require 'rubygems'
12
+ require 'statsd'
13
+
14
+ Statsd.host = 'localhost'
15
+ Statsd.port = 8125
16
+
17
+ Statsd.increment('some_counter') # basic incrementing
18
+ Statsd.increment('system.nested_counter', 0.1) # incrementing with sampling (10%)
19
+
20
+ Statsd.decrement(:some_other_counter) # basic decrememting using a symbol
21
+ Statsd.decrement('system.nested_counter', 0.1) # decrementing with sampling (10%)
22
+
23
+ Statsd.timing('some_job_time', 20) # reporting job that took 20ms
24
+ Statsd.timing('some_job_time', 20, 0.05) # reporting job that took 20ms with sampling (5% sampling)
25
+
26
+
data/lib/statsd.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'socket'
2
+
3
+ class Statsd
4
+
5
+ Version = '0.0.1'
6
+
7
+ class << self
8
+
9
+ attr_accessor :host, :port
10
+
11
+ # +stat+ to log timing for
12
+ # +time+ is the time to log in ms
13
+ def timing(stat, time, sample_rate = 1)
14
+ send_stats "#{stat}:#{time}|ms", sample_rate
15
+ end
16
+
17
+ # +stats+ can be a string or an array of strings
18
+ def increment(stats, sample_rate = 1)
19
+ update_counter stats, 1, sample_rate
20
+ end
21
+
22
+ # +stats+ can be a string or an array of strings
23
+ def decrement(stats, sample_rate = 1)
24
+ update_counter stats, -1, sample_rate
25
+ end
26
+
27
+ # +stats+ can be a string or array of strings
28
+ def update_counter(stats, delta = 1, sample_rate = 1)
29
+ stats = Array(stats)
30
+ send_stats(stats.map { |s| "#{s}:#{delta}|c" }, sample_rate)
31
+ end
32
+
33
+ private
34
+
35
+ def send_stats(data, sample_rate = 1)
36
+ data = Array(data)
37
+ sampled_data = []
38
+
39
+ # Apply sample rate if less than one
40
+ if sample_rate < 1
41
+ data.each do |d|
42
+ if rand <= sample_rate
43
+ sampled_data << "#{d}@#{sample_rate}"
44
+ end
45
+ end
46
+ data = sampled_data
47
+ end
48
+
49
+ return if data.empty?
50
+
51
+ raise "host and port must be set" unless host && port
52
+
53
+ sock = UDPSocket.new
54
+ data.each do |d|
55
+ puts d
56
+ sock.send(d, 0, host, port)
57
+ end
58
+ true
59
+ end
60
+
61
+
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/statsd", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "statsd-client"
6
+ s.version = Statsd::Version
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ['Ben VandenBos']
9
+ s.email = ['bvandenbos@gmail.com']
10
+ s.homepage = "http://github.com/bvandenbos/statsd-client"
11
+ s.summary = "Ruby client for statsd."
12
+ s.description = "Ruby client for statsd."
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
18
+ s.require_path = 'lib'
19
+ end
20
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsd-client
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Ben VandenBos
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-23 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Ruby client for statsd.
22
+ email:
23
+ - bvandenbos@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - README.md
33
+ - lib/statsd.rb
34
+ - statsd-client.gemspec
35
+ has_rdoc: true
36
+ homepage: http://github.com/bvandenbos/statsd-client
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 3
60
+ - 6
61
+ version: 1.3.6
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Ruby client for statsd.
69
+ test_files: []
70
+