Repository: reinh/statsd
Branch: master
Commit: ab577846791b
Files: 14
Total size: 41.8 KB
Directory structure:
gitextract_ist25ggt/
├── .document
├── .github/
│ └── workflows/
│ └── ci.yml
├── .gitignore
├── Gemfile
├── LICENSE.txt
├── README.rdoc
├── Rakefile
├── lib/
│ ├── statsd/
│ │ └── monotonic_time.rb
│ ├── statsd-ruby.rb
│ └── statsd.rb
├── spec/
│ ├── helper.rb
│ ├── statsd_admin_spec.rb
│ └── statsd_spec.rb
└── statsd-ruby.gemspec
================================================
FILE CONTENTS
================================================
================================================
FILE: .document
================================================
lib/**/*.rb
bin/*
-
features/**/*.feature
LICENSE.txt
================================================
FILE: .github/workflows/ci.yml
================================================
name: Ruby
on: [push, pull_request]
jobs:
build:
strategy:
matrix:
os:
- ubuntu
- macos
ruby:
- 2.4
- 2.5
- 2.6
- 2.7
- '3.0'
- 3.1
- 3.2
# TODO: jruby, rbx
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: 2.3.26
- name: Build and test with Rake
run: |
bundle install
bundle exec rake
================================================
FILE: .gitignore
================================================
# simplecov generated
coverage
# rdoc generated
rdoc
# yard generated
doc
.yardoc
# bundler
.bundle
Gemfile.lock
# jeweler generated
pkg
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
# * Create a file at ~/.gitignore
# * Include files you want ignored
# * Run: git config --global core.excludesfile ~/.gitignore
#
# After doing this, these files will be ignored in all your git projects,
# saving you from having to 'pollute' every project you touch with them
#
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
#
# For MacOS:
#
#.DS_Store
#
# For TextMate
#*.tmproj
#tmtags
#
# For emacs:
#*~
#\#*
#.\#*
#
# For vim:
#*.swp
================================================
FILE: Gemfile
================================================
source 'https://rubygems.org'
gemspec
================================================
FILE: LICENSE.txt
================================================
Copyright (c) 2011, 2012, 2013 Rein Henrichs
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
================================================
FILE: README.rdoc
================================================
= statsd-ruby CI: {<img src="https://github.com/reinh/statsd/workflows/Ruby/badge.svg" />}[https://github.com/reinh/statsd/actions?query=workflow%3ARuby]
A Ruby client for {StatsD}[https://github.com/etsy/statsd]
= Installing
Bundler:
gem "statsd-ruby"
= Basic Usage
# Set up a global Statsd client for a server on localhost:9125
$statsd = Statsd.new 'localhost', 9125
# Set up a global Statsd client for a server on IPv6 port 9125
$statsd = Statsd.new '::1', 9125
# Send some stats
$statsd.increment 'garets'
$statsd.timing 'glork', 320
$statsd.gauge 'bork', 100
# Use {#time} to time the execution of a block
$statsd.time('account.activate') { @account.activate! }
# Create a namespaced statsd client and increment 'account.activate'
statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
statsd.increment 'activate'
= Testing
Run the specs with <tt>rake spec</tt>
= Performance
* A short note about DNS: If you use a dns name for the host option, then you will want to use a local caching dns service for optimal performance (e.g. nscd).
= Extensions / Libraries / Extra Docs
* See the wiki[https://github.com/reinh/statsd/wiki]
== Contributing to statsd
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
* Start a feature/bugfix branch
* Commit and push until you are happy with your contribution
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
* 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.
== Contributors
* Rein Henrichs
* Alex Williams
* Andrew Meyer
* Chris Gaffney
* Cody Cutrer
* Corey Donohoe
* Dotan Nahum
* Erez Rabih
* Eric Chapweske
* Gabriel Burt
* Hannes Georg
* James Tucker
* Jeremy Kemper
* John Nunemaker
* Lann Martin
* Mahesh Murthy
* Manu J
* Matt Sanford
* Nate Bird
* Noah Lorang
* Oscar Del Ben
* Peter Mounce
* Ray Krueger
* Reed Lipman
* rick
* Ryan Tomayko
* Schuyler Erle
* Thomas Whaples
* Trae Robrock
== Copyright
Copyright (c) 2011, 2012, 2013 Rein Henrichs. See LICENSE.txt for further details.
================================================
FILE: Rakefile
================================================
require 'bundler/setup'
require 'bundler/gem_tasks'
task :default => :spec
require 'rake/testtask'
Rake::TestTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.verbose = true
spec.warning = true
end
require 'yard'
YARD::Rake::YardocTask.new
================================================
FILE: lib/statsd/monotonic_time.rb
================================================
class Statsd
# = MonotonicTime: a helper for getting monotonic time
#
# @example
# MonotonicTime.time_in_ms #=> 287138801.144576
# MonotonicTime guarantees that the time is strictly linearly
# increasing (unlike realtime).
# @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
module MonotonicTime
class << self
# @return [Integer] current monotonic time in milliseconds
def time_in_ms
time_in_nanoseconds / (10.0 ** 6)
end
private
if defined?(Process::CLOCK_MONOTONIC)
def time_in_nanoseconds
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
end
elsif RUBY_ENGINE == 'jruby'
def time_in_nanoseconds
java.lang.System.nanoTime
end
else
def time_in_nanoseconds
t = Time.now
t.to_i * (10 ** 9) + t.nsec
end
end
end
end
end
================================================
FILE: lib/statsd-ruby.rb
================================================
require 'statsd'
================================================
FILE: lib/statsd.rb
================================================
# frozen_string_literal: true
require 'socket'
require 'forwardable'
require 'json'
require 'statsd/monotonic_time'
# = Statsd: A Statsd client (https://github.com/etsy/statsd)
#
# @example Set up a global Statsd client for a server on localhost:8125
# $statsd = Statsd.new 'localhost', 8125
# @example Set up a global Statsd client for a server on IPv6 port 8125
# $statsd = Statsd.new '::1', 8125
# @example Send some stats
# $statsd.increment 'garets'
# $statsd.timing 'glork', 320
# $statsd.gauge 'bork', 100
# @example Use {#time} to time the execution of a block
# $statsd.time('account.activate') { @account.activate! }
# @example Create a namespaced statsd client and increment 'account.activate'
# statsd = Statsd.new('localhost').tap{|sd| sd.namespace = 'account'}
# statsd.increment 'activate'
#
# Statsd instances are thread safe for general usage, by utilizing the thread
# safe nature of UDP sends. The attributes are stateful, and are not
# mutexed, it is expected that users will not change these at runtime in
# threaded environments. If users require such use cases, it is recommend that
# users either mutex around their Statsd object, or create separate objects for
# each namespace / host+port combination.
class Statsd
# = Batch: A batching statsd proxy
#
# @example Batch a set of instruments using Batch and manual flush:
# $statsd = Statsd.new 'localhost', 8125
# batch = Statsd::Batch.new($statsd)
# batch.increment 'garets'
# batch.timing 'glork', 320
# batch.gauge 'bork', 100
# batch.flush
#
# Batch is a subclass of Statsd, but with a constructor that proxies to a
# normal Statsd instance. It has it's own batch_size and namespace parameters
# (that inherit defaults from the supplied Statsd instance). It is recommended
# that some care is taken if setting very large batch sizes. If the batch size
# exceeds the allowed packet size for UDP on your network, communication
# troubles may occur and data will be lost.
class Batch < Statsd
extend Forwardable
def_delegators :@statsd,
:namespace, :namespace=,
:host, :host=,
:port, :port=,
:prefix,
:postfix,
:delimiter, :delimiter=
attr_accessor :batch_size, :batch_byte_size, :flush_interval
# @param [Statsd] statsd requires a configured Statsd instance
def initialize(statsd)
@statsd = statsd
@batch_size = statsd.batch_size
@batch_byte_size = statsd.batch_byte_size
@flush_interval = statsd.flush_interval
@backlog = []
@backlog_bytesize = 0
@last_flush = Time.now
end
# @yield [Batch] yields itself
#
# A convenience method to ensure that data is not lost in the event of an
# exception being thrown. Batches will be transmitted on the parent socket
# as soon as the batch is full, and when the block finishes.
def easy
yield self
ensure
flush
end
def flush
unless @backlog.empty?
@statsd.send_to_socket @backlog.join("\n")
@backlog.clear
@backlog_bytesize = 0
@last_flush = Time.now
end
end
protected
def send_to_socket(message)
# this message wouldn't fit; flush the queue. note that we don't have
# to do this for message based flushing, because we're incrementing by
# one, so the post-queue check will always catch it
if (@batch_byte_size && @backlog_bytesize + message.bytesize + 1 > @batch_byte_size) ||
(@flush_interval && last_flush_seconds_ago >= @flush_interval)
flush
end
@backlog << message
@backlog_bytesize += message.bytesize
# skip the interleaved newline for the first item
@backlog_bytesize += 1 if @backlog.length != 1
# if we're precisely full now, flush
if (@batch_size && @backlog.size == @batch_size) ||
(@batch_byte_size && @backlog_bytesize == @batch_byte_size)
flush
end
end
def last_flush_seconds_ago
Time.now - @last_flush
end
end
class Admin
# StatsD host. Defaults to 127.0.0.1.
attr_reader :host
# StatsD admin port. Defaults to 8126.
attr_reader :port
class << self
# Set to a standard logger instance to enable debug logging.
attr_accessor :logger
end
# @attribute [w] host.
# Users should call connect after changing this.
def host=(host)
@host = host || '127.0.0.1'
end
# @attribute [w] port.
# Users should call connect after changing this.
def port=(port)
@port = port || 8126
end
# @param [String] host your statsd host
# @param [Integer] port your statsd port
def initialize(host = '127.0.0.1', port = 8126)
@host = host || '127.0.0.1'
@port = port || 8126
# protects @socket transactions
@socket = nil
@s_mu = Mutex.new
connect
end
# Reads all gauges from StatsD.
def gauges
read_metric :gauges
end
# Reads all timers from StatsD.
def timers
read_metric :timers
end
# Reads all counters from StatsD.
def counters
read_metric :counters
end
# @param[String] item
# Deletes one or more gauges. Wildcards are allowed.
def delgauges item
delete_metric :gauges, item
end
# @param[String] item
# Deletes one or more timers. Wildcards are allowed.
def deltimers item
delete_metric :timers, item
end
# @param[String] item
# Deletes one or more counters. Wildcards are allowed.
def delcounters item
delete_metric :counters, item
end
def stats
result = @s_mu.synchronize do
# the format of "stats" isn't JSON, who knows why
send_to_socket "stats"
read_from_socket
end
items = {}
result.split("\n").each do |line|
key, val = line.chomp.split(": ")
items[key] = val.to_i
end
items
end
# Reconnects the socket, for when the statsd address may have changed. Users
# do not normally need to call this, but calling it may be appropriate when
# reconfiguring a process (e.g. from HUP)
def connect
@s_mu.synchronize do
begin
@socket.flush rescue nil
@socket.close if @socket
rescue
# Ignore socket errors on close.
end
@socket = TCPSocket.new(host, port)
end
end
private
def read_metric name
result = @s_mu.synchronize do
send_to_socket name
read_from_socket
end
# for some reason, the reply looks like JSON, but isn't, quite
JSON.parse result.gsub("'", "\"")
end
def delete_metric name, item
result = @s_mu.synchronize do
send_to_socket "del#{name} #{item}"
read_from_socket
end
deleted = []
result.split("\n").each do |line|
deleted << line.chomp.split(": ")[-1]
end
deleted
end
def send_to_socket(message)
self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
@socket.write(message.to_s + "\n")
rescue => boom
self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
nil
end
def read_from_socket
buffer = ""
loop do
line = @socket.readline
break if line == "END\n"
buffer += line
end
@socket.readline # clear the closing newline out of the socket
buffer
end
end
# A namespace to prepend to all statsd calls.
attr_reader :namespace
# StatsD host. Defaults to 127.0.0.1.
attr_reader :host
# StatsD port. Defaults to 8125.
attr_reader :port
# StatsD namespace prefix, generated from #namespace
attr_reader :prefix
# The default batch size for new batches. Set to nil to use batch_byte_size (default: 10)
attr_accessor :batch_size
# The default batch size, in bytes, for new batches (default: default nil; use batch_size)
attr_accessor :batch_byte_size
# The flush interval, in seconds, for new batches (default: nil)
attr_accessor :flush_interval
# a postfix to append to all metrics
attr_reader :postfix
# The replacement of :: on ruby module names when transformed to statsd metric names
attr_reader :delimiter
class << self
# Set to a standard logger instance to enable debug logging.
attr_accessor :logger
end
# @param [String] host your statsd host
# @param [Integer] port your statsd port
# @param [Symbol] protocol :tcp for TCP, :udp or any other value for UDP
def initialize(host = '127.0.0.1', port = 8125, protocol = :udp)
@host = host || '127.0.0.1'
@port = port || 8125
self.delimiter = "."
@prefix = nil
@batch_size = 10
@batch_byte_size = nil
@flush_interval = nil
@postfix = nil
@socket = nil
@protocol = protocol || :udp
@s_mu = Mutex.new
connect
end
# @attribute [w] namespace
# Writes are not thread safe.
def namespace=(namespace)
@namespace = namespace
@prefix = "#{namespace}."
end
# @attribute [w] postfix
# A value to be appended to the stat name after a '.'. If the value is
# blank then the postfix will be reset to nil (rather than to '.').
def postfix=(pf)
case pf
when nil, false, '' then @postfix = nil
else @postfix = ".#{pf}"
end
end
# @attribute [w] host
# Writes are not thread safe.
# Users should call hup after making changes.
def host=(host)
@host = host || '127.0.0.1'
end
# @attribute [w] port
# Writes are not thread safe.
# Users should call hup after making changes.
def port=(port)
@port = port || 8125
end
# @attribute [w] stat_delimiter
# Allows for custom delimiter replacement for :: when Ruby modules are transformed to statsd metric name
def delimiter=(delimiter)
@delimiter = delimiter || "."
end
# Sends an increment (count = 1) for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Numeric] sample_rate sample rate, 1 for always
# @see #count
def increment(stat, sample_rate=1)
count stat, 1, sample_rate
end
# Sends a decrement (count = -1) for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Numeric] sample_rate sample rate, 1 for always
# @see #count
def decrement(stat, sample_rate=1)
count stat, -1, sample_rate
end
# Sends an arbitrary count for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Integer] count count
# @param [Numeric] sample_rate sample rate, 1 for always
def count(stat, count, sample_rate=1)
send_stats stat, count, :c, sample_rate
end
# Sends an arbitary gauge value for the given stat to the statsd server.
#
# This is useful for recording things like available disk space,
# memory usage, and the like, which have different semantics than
# counters.
#
# @param [String] stat stat name.
# @param [Numeric] value gauge value.
# @param [Numeric] sample_rate sample rate, 1 for always
# @example Report the current user count:
# $statsd.gauge('user.count', User.count)
def gauge(stat, value, sample_rate=1)
send_stats stat, value, :g, sample_rate
end
# Sends an arbitary set value for the given stat to the statsd server.
#
# This is for recording counts of unique events, which are useful to
# see on graphs to correlate to other values. For example, a deployment
# might get recorded as a set, and be drawn as annotations on a CPU history
# graph.
#
# @param [String] stat stat name.
# @param [Numeric] value event value.
# @param [Numeric] sample_rate sample rate, 1 for always
# @example Report a deployment happening:
# $statsd.set('deployment', DEPLOYMENT_EVENT_CODE)
def set(stat, value, sample_rate=1)
send_stats stat, value, :s, sample_rate
end
# Sends a timing (in ms) for the given stat to the statsd server. The
# sample_rate determines what percentage of the time this report is sent. The
# statsd server then uses the sample_rate to correctly track the average
# timing for the stat.
#
# @param [String] stat stat name
# @param [Integer] ms timing in milliseconds
# @param [Numeric] sample_rate sample rate, 1 for always
def timing(stat, ms, sample_rate=1)
send_stats stat, ms, :ms, sample_rate
end
# Reports execution time of the provided block using {#timing}.
#
# @param [String] stat stat name
# @param [Numeric] sample_rate sample rate, 1 for always
# @yield The operation to be timed
# @see #timing
# @example Report the time (in ms) taken to activate an account
# $statsd.time('account.activate') { @account.activate! }
def time(stat, sample_rate=1)
start = MonotonicTime.time_in_ms
result = yield
ensure
timing(stat, (MonotonicTime.time_in_ms - start).round, sample_rate)
result
end
# Creates and yields a Batch that can be used to batch instrument reports into
# larger packets. Batches are sent either when the packet is "full" (defined
# by batch_size), or when the block completes, whichever is the sooner.
#
# @yield [Batch] a statsd subclass that collects and batches instruments
# @example Batch two instument operations:
# $statsd.batch do |batch|
# batch.increment 'sys.requests'
# batch.gauge('user.count', User.count)
# end
def batch(&block)
Batch.new(self).easy(&block)
end
# Reconnects the socket, useful if the address of the statsd has changed. This
# method is not thread safe from a perspective of stat submission. It is safe
# from resource leaks. Users do not normally need to call this, but calling it
# may be appropriate when reconfiguring a process (e.g. from HUP).
def connect
@s_mu.synchronize do
begin
@socket.close if @socket
rescue
# Errors are ignored on reconnects.
end
case @protocol
when :tcp
@socket = TCPSocket.new @host, @port
else
@socket = UDPSocket.new Addrinfo.ip(@host).afamily
@socket.connect host, port
end
end
end
protected
def send_to_socket(message)
self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
retries = 0
n = 0
while true
# send(2) is atomic, however, in stream cases (TCP) the socket is left
# in an inconsistent state if a partial message is written. If that case
# occurs, the socket is closed down and we retry on a new socket.
message = @protocol == :tcp ? message + "\n" : message
n = socket.write(message) rescue (err = $!; 0)
if n == message.length
break
end
connect
retries += 1
raise (err || "statsd: Failed to send after #{retries} attempts") if retries >= 5
end
n
rescue => boom
self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
nil
end
private
def send_stats(stat, delta, type, sample_rate=1)
if sample_rate == 1 or rand < sample_rate
# Replace Ruby module scoping with '.' and reserved chars (: | @) with underscores.
stat = stat.to_s.gsub('::', delimiter).tr(':|@', '_')
rate = "|@#{sample_rate}" unless sample_rate == 1
send_to_socket "#{prefix}#{stat}#{postfix}:#{delta}|#{type}#{rate}"
end
end
def socket
# Subtle: If the socket is half-way through initialization in connect, it
# cannot be used yet.
@s_mu.synchronize { @socket } || raise(ThreadError, "socket missing")
end
end
================================================
FILE: spec/helper.rb
================================================
require 'bundler/setup'
require 'simplecov'
SimpleCov.start
require 'minitest/autorun'
require 'statsd'
require 'logger'
require 'timeout'
class FakeUDPSocket
def initialize
@buffer = []
end
def write(message)
@buffer.push [message]
message.length
end
def recv
@buffer.shift
end
def clear
@buffer = []
end
def to_s
inspect
end
def inspect
"<#{self.class.name}: #{@buffer.inspect}>"
end
end
class FakeTCPSocket < FakeUDPSocket
alias_method :readline, :recv
def write(message)
@buffer.push message
end
end
================================================
FILE: spec/statsd_admin_spec.rb
================================================
require 'helper'
describe Statsd::Admin do
before do
class Statsd::Admin
o, $VERBOSE = $VERBOSE, nil
alias connect_old connect
def connect
$connect_count ||= 0
$connect_count += 1
end
$VERBOSE = o
end
@admin = Statsd::Admin.new('localhost', 1234)
@socket = @admin.instance_variable_set(:@socket, FakeTCPSocket.new)
end
after do
class Statsd::Admin
o, $VERBOSE = $VERBOSE, nil
alias connect connect_old
$VERBOSE = o
end
end
describe "#initialize" do
it "should set the host and port" do
_(@admin.host).must_equal 'localhost'
_(@admin.port).must_equal 1234
end
it "should default the host to 127.0.0.1 and port to 8126" do
statsd = Statsd::Admin.new
_(statsd.host).must_equal '127.0.0.1'
_(statsd.port).must_equal 8126
end
end
describe "#host and #port" do
it "should set host and port" do
@admin.host = '1.2.3.4'
@admin.port = 5678
_(@admin.host).must_equal '1.2.3.4'
_(@admin.port).must_equal 5678
end
it "should not resolve hostnames to IPs" do
@admin.host = 'localhost'
_(@admin.host).must_equal 'localhost'
end
it "should set nil host to default" do
@admin.host = nil
_(@admin.host).must_equal '127.0.0.1'
end
it "should set nil port to default" do
@admin.port = nil
_(@admin.port).must_equal 8126
end
end
%w(gauges counters timers).each do |action|
describe "##{action}" do
it "should send a command and return a Hash" do
["{'foo.bar': 0,\n",
"'foo.baz': 1,\n",
"'foo.quux': 2 }\n",
"END\n","\n"].each do |line|
@socket.write line
end
result = @admin.send action.to_sym
_(result).must_be_kind_of Hash
_(result.size).must_equal 3
_(@socket.readline).must_equal "#{action}\n"
end
end
describe "#del#{action}" do
it "should send a command and return an Array" do
["deleted: foo.bar\n",
"deleted: foo.baz\n",
"deleted: foo.quux\n",
"END\n", "\n"].each do |line|
@socket.write line
end
result = @admin.send "del#{action}", "foo.*"
_(result).must_be_kind_of Array
_(result.size).must_equal 3
_(@socket.readline).must_equal "del#{action} foo.*\n"
end
end
end
describe "#stats" do
it "should send a command and return a Hash" do
["whatever: 0\n", "END\n", "\n"].each do |line|
@socket.write line
end
result = @admin.stats
_(result).must_be_kind_of Hash
_(result["whatever"]).must_equal 0
_(@socket.readline).must_equal "stats\n"
end
end
describe "#connect" do
it "should reconnect" do
c = $connect_count
@admin.connect
_(($connect_count - c)).must_equal 1
end
end
end
================================================
FILE: spec/statsd_spec.rb
================================================
require 'helper'
describe Statsd do
before do
class Statsd
o, $VERBOSE = $VERBOSE, nil
alias connect_old connect
def connect
$connect_count ||= 1
$connect_count += 1
end
$VERBOSE = o
end
@statsd = Statsd.new('localhost', 1234)
@socket = @statsd.instance_variable_set(:@socket, FakeUDPSocket.new)
end
after do
class Statsd
o, $VERBOSE = $VERBOSE, nil
alias connect connect_old
$VERBOSE = o
end
end
describe "#initialize" do
it "should set the host and port" do
_(@statsd.host).must_equal 'localhost'
_(@statsd.port).must_equal 1234
end
it "should default the host to 127.0.0.1 and port to 8125" do
statsd = Statsd.new
_(statsd.host).must_equal '127.0.0.1'
_(statsd.port).must_equal 8125
end
it "should set delimiter to period by default" do
_(@statsd.delimiter).must_equal "."
end
end
describe "#host and #port" do
it "should set host and port" do
@statsd.host = '1.2.3.4'
@statsd.port = 5678
_(@statsd.host).must_equal '1.2.3.4'
_(@statsd.port).must_equal 5678
end
it "should not resolve hostnames to IPs" do
@statsd.host = 'localhost'
_(@statsd.host).must_equal 'localhost'
end
it "should set nil host to default" do
@statsd.host = nil
_(@statsd.host).must_equal '127.0.0.1'
end
it "should set nil port to default" do
@statsd.port = nil
_(@statsd.port).must_equal 8125
end
it "should allow an IPv6 address" do
@statsd.host = '::1'
_(@statsd.host).must_equal '::1'
end
end
describe "#delimiter" do
it "should set delimiter" do
@statsd.delimiter = "-"
_(@statsd.delimiter).must_equal "-"
end
it "should set default to period if not given a value" do
@statsd.delimiter = nil
_(@statsd.delimiter).must_equal "."
end
end
describe "#increment" do
it "should format the message according to the statsd spec" do
@statsd.increment('foobar')
_(@socket.recv).must_equal ['foobar:1|c']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.increment('foobar', 0.5)
_(@socket.recv).must_equal ['foobar:1|c|@0.5']
end
end
end
describe "#decrement" do
it "should format the message according to the statsd spec" do
@statsd.decrement('foobar')
_(@socket.recv).must_equal ['foobar:-1|c']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.decrement('foobar', 0.5)
_(@socket.recv).must_equal ['foobar:-1|c|@0.5']
end
end
end
describe "#gauge" do
it "should send a message with a 'g' type, per the nearbuy fork" do
@statsd.gauge('begrutten-suffusion', 536)
_(@socket.recv).must_equal ['begrutten-suffusion:536|g']
@statsd.gauge('begrutten-suffusion', -107.3)
_(@socket.recv).must_equal ['begrutten-suffusion:-107.3|g']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.gauge('begrutten-suffusion', 536, 0.1)
_(@socket.recv).must_equal ['begrutten-suffusion:536|g|@0.1']
end
end
end
describe "#timing" do
it "should format the message according to the statsd spec" do
@statsd.timing('foobar', 500)
_(@socket.recv).must_equal ['foobar:500|ms']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.timing('foobar', 500, 0.5)
_(@socket.recv).must_equal ['foobar:500|ms|@0.5']
end
end
end
describe "#set" do
it "should format the message according to the statsd spec" do
@statsd.set('foobar', 765)
_(@socket.recv).must_equal ['foobar:765|s']
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.set('foobar', 500, 0.5)
_(@socket.recv).must_equal ['foobar:500|s|@0.5']
end
end
end
describe "#time" do
it "should format the message according to the statsd spec" do
@statsd.time('foobar') { 'test' }
_(@socket.recv).must_equal ['foobar:0|ms']
end
it "should return the result of the block" do
result = @statsd.time('foobar') { 'test' }
_(result).must_equal 'test'
end
describe "when given a block with an explicit return" do
it "should format the message according to the statsd spec" do
lambda { @statsd.time('foobar') { return 'test' } }.call
_(@socket.recv).must_equal ['foobar:0|ms']
end
it "should return the result of the block" do
result = lambda { @statsd.time('foobar') { return 'test' } }.call
_(result).must_equal 'test'
end
end
describe "with a sample rate" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should format the message according to the statsd spec" do
@statsd.time('foobar', 0.5) { 'test' }
_(@socket.recv).must_equal ['foobar:0|ms|@0.5']
end
end
end
describe "#sampled" do
describe "when the sample rate is 1" do
before { class << @statsd; def rand; raise end; end }
it "should send" do
@statsd.timing('foobar', 500, 1)
_(@socket.recv).must_equal ['foobar:500|ms']
end
end
describe "when the sample rate is greater than a random value [0,1]" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should send" do
@statsd.timing('foobar', 500, 0.5)
_(@socket.recv).must_equal ['foobar:500|ms|@0.5']
end
end
describe "when the sample rate is less than a random value [0,1]" do
before { class << @statsd; def rand; 1; end; end } # ensure no delivery
it "should not send" do
assert_nil @statsd.timing('foobar', 500, 0.5)
end
end
describe "when the sample rate is equal to a random value [0,1]" do
before { class << @statsd; def rand; 0; end; end } # ensure delivery
it "should send" do
@statsd.timing('foobar', 500, 0.5)
_(@socket.recv).must_equal ['foobar:500|ms|@0.5']
end
end
end
describe "with namespace" do
before { @statsd.namespace = 'service' }
it "should add namespace to increment" do
@statsd.increment('foobar')
_(@socket.recv).must_equal ['service.foobar:1|c']
end
it "should add namespace to decrement" do
@statsd.decrement('foobar')
_(@socket.recv).must_equal ['service.foobar:-1|c']
end
it "should add namespace to timing" do
@statsd.timing('foobar', 500)
_(@socket.recv).must_equal ['service.foobar:500|ms']
end
it "should add namespace to gauge" do
@statsd.gauge('foobar', 500)
_(@socket.recv).must_equal ['service.foobar:500|g']
end
end
describe "with postfix" do
before { @statsd.postfix = 'ip-23-45-56-78' }
it "should add postfix to increment" do
@statsd.increment('foobar')
_(@socket.recv).must_equal ['foobar.ip-23-45-56-78:1|c']
end
it "should add postfix to decrement" do
@statsd.decrement('foobar')
_(@socket.recv).must_equal ['foobar.ip-23-45-56-78:-1|c']
end
it "should add namespace to timing" do
@statsd.timing('foobar', 500)
_(@socket.recv).must_equal ['foobar.ip-23-45-56-78:500|ms']
end
it "should add namespace to gauge" do
@statsd.gauge('foobar', 500)
_(@socket.recv).must_equal ['foobar.ip-23-45-56-78:500|g']
end
end
describe '#postfix=' do
describe "when nil, false, or empty" do
it "should set postfix to nil" do
[nil, false, ''].each do |value|
@statsd.postfix = 'a postfix'
@statsd.postfix = value
assert_nil @statsd.postfix
end
end
end
end
describe "with logging" do
require 'stringio'
before { Statsd.logger = Logger.new(@log = StringIO.new)}
it "should write to the log in debug" do
Statsd.logger.level = Logger::DEBUG
@statsd.increment('foobar')
_(@log.string).must_match "Statsd: foobar:1|c"
end
it "should not write to the log unless debug" do
Statsd.logger.level = Logger::INFO
@statsd.increment('foobar')
_(@log.string).must_be_empty
end
end
describe "stat names" do
it "should accept anything as stat" do
@statsd.increment(Object, 1)
end
it "should replace ruby constant delimeter with graphite package name" do
class Statsd::SomeClass; end
@statsd.increment(Statsd::SomeClass, 1)
_(@socket.recv).must_equal ['Statsd.SomeClass:1|c']
end
describe "custom delimiter" do
before do
@statsd.delimiter = "-"
end
it "should replace ruby constant delimiter with custom delimiter" do
class Statsd::SomeOtherClass; end
@statsd.increment(Statsd::SomeOtherClass, 1)
_(@socket.recv).must_equal ['Statsd-SomeOtherClass:1|c']
end
end
it "should replace statsd reserved chars in the stat name" do
@statsd.increment('ray@hostname.blah|blah.blah:blah', 1)
_(@socket.recv).must_equal ['ray_hostname.blah_blah.blah_blah:1|c']
end
end
describe "handling socket errors" do
before do
require 'stringio'
Statsd.logger = Logger.new(@log = StringIO.new)
@socket.instance_variable_set(:@err_count, 0)
@socket.instance_eval { def write(*) @err_count+=1; raise SocketError end }
end
it "should ignore socket errors" do
assert_nil @statsd.increment('foobar')
end
it "should log socket errors" do
@statsd.increment('foobar')
_(@log.string).must_match 'Statsd: SocketError'
end
it "should retry and reconnect on socket errors" do
$connect_count = 0
@statsd.increment('foobar')
_(@socket.instance_variable_get(:@err_count)).must_equal 5
_($connect_count).must_equal 5
end
end
describe "batching" do
it "should have a default batch size of 10" do
_(@statsd.batch_size).must_equal 10
end
it "should have a default batch byte size of nil" do
assert_nil @statsd.batch_byte_size
end
it "should have a default flush interval of nil" do
assert_nil @statsd.flush_interval
end
it "should have a modifiable batch size" do
@statsd.batch_size = 7
_(@statsd.batch_size).must_equal 7
@statsd.batch do |b|
_(b.batch_size).must_equal 7
end
@statsd.batch_size = nil
@statsd.batch_byte_size = 1472
@statsd.batch do |b|
assert_nil b.batch_size
_(b.batch_byte_size).must_equal 1472
end
end
it 'should have a modifiable flush interval' do
@statsd.flush_interval = 1
_(@statsd.flush_interval).must_equal 1
@statsd.batch do |b|
_(b.flush_interval).must_equal 1
end
end
it "should flush the batch at the batch size or at the end of the block" do
@statsd.batch do |b|
b.batch_size = 3
# The first three should flush, the next two will be flushed when the
# block is done.
5.times { b.increment('foobar') }
_(@socket.recv).must_equal [(["foobar:1|c"] * 3).join("\n")]
end
_(@socket.recv).must_equal [(["foobar:1|c"] * 2).join("\n")]
end
it "should flush based on batch byte size" do
@statsd.batch do |b|
b.batch_size = nil
b.batch_byte_size = 22
# The first two should flush, the last will be flushed when the
# block is done.
3.times { b.increment('foobar') }
_(@socket.recv).must_equal [(["foobar:1|c"] * 2).join("\n")]
end
_(@socket.recv).must_equal ["foobar:1|c"]
end
it "should flush immediately when the queue is exactly a batch size" do
@statsd.batch do |b|
b.batch_size = nil
b.batch_byte_size = 21
# The first two should flush together
2.times { b.increment('foobar') }
_(@socket.recv).must_equal [(["foobar:1|c"] * 2).join("\n")]
end
end
it "should flush when the interval has passed" do
@statsd.batch do |b|
b.batch_size = nil
b.flush_interval = 0.01
# The first two should flush, the last will be flushed when the
# block is done.
2.times { b.increment('foobar') }
sleep(0.03)
b.increment('foobar')
_(@socket.recv).must_equal [(["foobar:1|c"] * 2).join("\n")]
end
_(@socket.recv).must_equal ["foobar:1|c"]
end
it "should not flush to the socket if the backlog is empty" do
batch = Statsd::Batch.new(@statsd)
batch.flush
_(@socket.recv).must_be :nil?
batch.increment 'foobar'
batch.flush
_(@socket.recv).must_equal %w[foobar:1|c]
end
it "should support setting namespace for the underlying instance" do
batch = Statsd::Batch.new(@statsd)
batch.namespace = 'ns'
_(@statsd.namespace).must_equal 'ns'
end
it "should support setting host for the underlying instance" do
batch = Statsd::Batch.new(@statsd)
batch.host = '1.2.3.4'
_(@statsd.host).must_equal '1.2.3.4'
end
it "should support setting port for the underlying instance" do
batch = Statsd::Batch.new(@statsd)
batch.port = 42
_(@statsd.port).must_equal 42
end
end
describe "#connect" do
it "should reconnect" do
c = $connect_count
@statsd.connect
_(($connect_count - c)).must_equal 1
end
end
end
describe Statsd do
describe "with a real UDP socket" do
it "should actually send stuff over the socket" do
family = Addrinfo.udp(UDPSocket.getaddress('localhost'), 0).afamily
begin
socket = UDPSocket.new family
host, port = 'localhost', 0
socket.bind(host, port)
port = socket.addr[1]
statsd = Statsd.new(host, port)
statsd.increment('foobar')
message = socket.recvfrom(16).first
_(message).must_equal 'foobar:1|c'
ensure
socket.close
end
end
it "should send stuff over an IPv4 socket" do
begin
socket = UDPSocket.new Socket::AF_INET
host, port = '127.0.0.1', 0
socket.bind(host, port)
port = socket.addr[1]
statsd = Statsd.new(host, port)
statsd.increment('foobar')
message = socket.recvfrom(16).first
_(message).must_equal 'foobar:1|c'
ensure
socket.close
end
end
it "should send stuff over an IPv6 socket" do
begin
socket = UDPSocket.new Socket::AF_INET6
host, port = '::1', 0
socket.bind(host, port)
port = socket.addr[1]
statsd = Statsd.new(host, port)
statsd.increment('foobar')
message = socket.recvfrom(16).first
_(message).must_equal 'foobar:1|c'
ensure
socket.close
end
end
end
describe "supports TCP sockets" do
it "should connect to and send stats over TCPv4" do
begin
host, port = '127.0.0.1', 0
server = TCPServer.new host, port
port = server.addr[1]
socket = nil
Thread.new { socket = server.accept }
statsd = Statsd.new(host, port, :tcp)
statsd.increment('foobar')
Timeout.timeout(5) do
Thread.pass while socket == nil
end
message = socket.recvfrom(16).first
_(message).must_equal "foobar:1|c\n"
ensure
socket.close if socket
server.close
end
end
it "should connect to and send stats over TCPv6" do
begin
host, port = '::1', 0
server = TCPServer.new host, port
port = server.addr[1]
socket = nil
Thread.new { socket = server.accept }
statsd = Statsd.new(host, port, :tcp)
statsd.increment('foobar')
Timeout.timeout(5) do
Thread.pass while socket == nil
end
message = socket.recvfrom(16).first
_(message).must_equal "foobar:1|c\n"
ensure
socket.close if socket
server.close
end
end
end
end
================================================
FILE: statsd-ruby.gemspec
================================================
# -*- encoding: utf-8 -*-
Gem::Specification.new("statsd-ruby", "1.5.0") do |s|
s.authors = `git log --format='%aN' | sort -u`.split("\n")
s.email = "reinh@reinh.com"
s.summary = "A Ruby StatsD client"
s.description = "A Ruby StatsD client (https://github.com/etsy/statsd)"
s.homepage = "https://github.com/reinh/statsd"
s.licenses = %w[MIT]
s.extra_rdoc_files = %w[LICENSE.txt README.rdoc]
if $0 =~ /gem/ # If running under rubygems (building), otherwise, just leave
s.files = `git ls-files`.split($\)
s.test_files = s.files.grep(%r{^(test|spec|features)/})
end
s.add_development_dependency "minitest", ">= 5.6.0"
s.add_development_dependency "yard"
s.add_development_dependency "simplecov", ">= 0.6.4"
s.add_development_dependency "rake"
end
gitextract_ist25ggt/ ├── .document ├── .github/ │ └── workflows/ │ └── ci.yml ├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.rdoc ├── Rakefile ├── lib/ │ ├── statsd/ │ │ └── monotonic_time.rb │ ├── statsd-ruby.rb │ └── statsd.rb ├── spec/ │ ├── helper.rb │ ├── statsd_admin_spec.rb │ └── statsd_spec.rb └── statsd-ruby.gemspec
SYMBOL INDEX (75 symbols across 5 files)
FILE: lib/statsd.rb
class Statsd (line 31) | class Statsd
class Batch (line 49) | class Batch < Statsd
method initialize (line 63) | def initialize(statsd)
method easy (line 78) | def easy
method flush (line 84) | def flush
method send_to_socket (line 95) | def send_to_socket(message)
method last_flush_seconds_ago (line 114) | def last_flush_seconds_ago
class Admin (line 120) | class Admin
method host= (line 134) | def host=(host)
method port= (line 140) | def port=(port)
method initialize (line 146) | def initialize(host = '127.0.0.1', port = 8126)
method gauges (line 156) | def gauges
method timers (line 161) | def timers
method counters (line 166) | def counters
method delgauges (line 172) | def delgauges item
method deltimers (line 178) | def deltimers item
method delcounters (line 184) | def delcounters item
method stats (line 188) | def stats
method connect (line 205) | def connect
method read_metric (line 219) | def read_metric name
method delete_metric (line 228) | def delete_metric name, item
method send_to_socket (line 240) | def send_to_socket(message)
method read_from_socket (line 249) | def read_from_socket
method initialize (line 296) | def initialize(host = '127.0.0.1', port = 8125, protocol = :udp)
method namespace= (line 313) | def namespace=(namespace)
method postfix= (line 321) | def postfix=(pf)
method host= (line 331) | def host=(host)
method port= (line 338) | def port=(port)
method delimiter= (line 344) | def delimiter=(delimiter)
method increment (line 353) | def increment(stat, sample_rate=1)
method decrement (line 362) | def decrement(stat, sample_rate=1)
method count (line 371) | def count(stat, count, sample_rate=1)
method gauge (line 386) | def gauge(stat, value, sample_rate=1)
method set (line 402) | def set(stat, value, sample_rate=1)
method timing (line 414) | def timing(stat, ms, sample_rate=1)
method time (line 426) | def time(stat, sample_rate=1)
method batch (line 444) | def batch(&block)
method connect (line 452) | def connect
method send_to_socket (line 472) | def send_to_socket(message)
method send_stats (line 499) | def send_stats(stat, delta, type, sample_rate=1)
method socket (line 508) | def socket
FILE: lib/statsd/monotonic_time.rb
class Statsd (line 1) | class Statsd
type MonotonicTime (line 10) | module MonotonicTime
function time_in_ms (line 13) | def time_in_ms
function time_in_nanoseconds (line 20) | def time_in_nanoseconds
function time_in_nanoseconds (line 24) | def time_in_nanoseconds
function time_in_nanoseconds (line 28) | def time_in_nanoseconds
FILE: spec/helper.rb
class FakeUDPSocket (line 11) | class FakeUDPSocket
method initialize (line 12) | def initialize
method write (line 16) | def write(message)
method recv (line 21) | def recv
method clear (line 25) | def clear
method to_s (line 29) | def to_s
method inspect (line 33) | def inspect
class FakeTCPSocket (line 38) | class FakeTCPSocket < FakeUDPSocket
method write (line 40) | def write(message)
FILE: spec/statsd_admin_spec.rb
class Statsd::Admin (line 6) | class Statsd::Admin
method connect (line 9) | def connect
class Statsd::Admin (line 20) | class Statsd::Admin
method connect (line 9) | def connect
FILE: spec/statsd_spec.rb
class Statsd (line 5) | class Statsd
method connect (line 8) | def connect
class Statsd (line 20) | class Statsd
method connect (line 8) | def connect
function rand (line 92) | def rand; 0; end
function rand (line 107) | def rand; 0; end
function rand (line 124) | def rand; 0; end
function rand (line 139) | def rand; 0; end
function rand (line 154) | def rand; 0; end
function rand (line 186) | def rand; 0; end
function rand (line 197) | def rand; raise end
function rand (line 205) | def rand; 0; end
function rand (line 213) | def rand; 1; end
function rand (line 220) | def rand; 0; end
class Statsd::SomeClass (line 315) | class Statsd::SomeClass; end
class Statsd::SomeOtherClass (line 327) | class Statsd::SomeOtherClass; end
function write (line 345) | def write(*) @err_count+=1; raise SocketError end
Condensed preview — 14 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (45K chars).
[
{
"path": ".document",
"chars": 55,
"preview": "lib/**/*.rb\nbin/*\n- \nfeatures/**/*.feature\nLICENSE.txt\n"
},
{
"path": ".github/workflows/ci.yml",
"chars": 601,
"preview": "name: Ruby\n\non: [push, pull_request]\n\njobs:\n build:\n strategy:\n matrix:\n os:\n - ubuntu\n "
},
{
"path": ".gitignore",
"chars": 779,
"preview": "# simplecov generated\ncoverage\n\n# rdoc generated\nrdoc\n\n# yard generated\ndoc\n.yardoc\n\n# bundler\n.bundle\nGemfile.lock\n\n# j"
},
{
"path": "Gemfile",
"chars": 38,
"preview": "source 'https://rubygems.org'\ngemspec\n"
},
{
"path": "LICENSE.txt",
"chars": 1069,
"preview": "Copyright (c) 2011, 2012, 2013 Rein Henrichs\n\nPermission is hereby granted, free of charge, to any person obtaining\na co"
},
{
"path": "README.rdoc",
"chars": 2402,
"preview": "= statsd-ruby CI: {<img src=\"https://github.com/reinh/statsd/workflows/Ruby/badge.svg\" />}[https://github.com/reinh/stat"
},
{
"path": "Rakefile",
"chars": 296,
"preview": "require 'bundler/setup'\nrequire 'bundler/gem_tasks'\n\ntask :default => :spec\n\nrequire 'rake/testtask'\nRake::TestTask.new("
},
{
"path": "lib/statsd/monotonic_time.rb",
"chars": 936,
"preview": "class Statsd\n # = MonotonicTime: a helper for getting monotonic time\n #\n # @example\n # MonotonicTime.time_in_ms #="
},
{
"path": "lib/statsd-ruby.rb",
"chars": 17,
"preview": "require 'statsd'\n"
},
{
"path": "lib/statsd.rb",
"chars": 15562,
"preview": "# frozen_string_literal: true\n\nrequire 'socket'\nrequire 'forwardable'\nrequire 'json'\n\nrequire 'statsd/monotonic_time'\n\n#"
},
{
"path": "spec/helper.rb",
"chars": 575,
"preview": "require 'bundler/setup'\n\nrequire 'simplecov'\nSimpleCov.start\n\nrequire 'minitest/autorun'\nrequire 'statsd'\nrequire 'logge"
},
{
"path": "spec/statsd_admin_spec.rb",
"chars": 2926,
"preview": "require 'helper'\n\ndescribe Statsd::Admin do\n\n before do\n class Statsd::Admin\n o, $VERBOSE = $VERBOSE, nil\n "
},
{
"path": "spec/statsd_spec.rb",
"chars": 16746,
"preview": "require 'helper'\n\ndescribe Statsd do\n before do\n class Statsd\n o, $VERBOSE = $VERBOSE, nil\n alias connect_"
},
{
"path": "statsd-ruby.gemspec",
"chars": 797,
"preview": "# -*- encoding: utf-8 -*-\n\nGem::Specification.new(\"statsd-ruby\", \"1.5.0\") do |s|\n s.authors = `git log --format='%aN' "
}
]
About this extraction
This page contains the full source code of the reinh/statsd GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 14 files (41.8 KB), approximately 12.4k tokens, and a symbol index with 75 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.