Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b533e6ee74
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 101 lines (82 sloc) 2.477 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
require 'socket'
require 'yaml'

# ruby_example.rb

# Ian Sefferman <iseff@iseff.com>
# http://www.iseff.com
 
# If this is running in a Rails environment, will pick up config/statsd.yml.
# config/statsd.yml should look like:
# production:
# host: statsd.domain.com
# port: 8125
# development:
# host: localhost
# port: 8125

# If this file is not running in Rails environment, will pick up ./statsd.yml
# ./statsd.yml should look like:
# host: localhost
# port: 8125

# If neither of these files are present, it will default to localhost:8125

# Sends statistics to the stats daemon over UDP
class Statsd
  
  def self.timing(stats, time, sample_rate=1)
    Statsd.update_stats(stats, time, sample_rate, 'ms')
  end

  def self.increment(stats, sample_rate=1)
    Statsd.update_stats(stats, 1, sample_rate)
  end

  def self.decrement(stats, sample_rate=1)
    Statsd.update_stats(stats, -1, sample_rate)
  end

  def self.gauges(stats, value, sample_rate=1)
    Statsd.update_stats(stats, value, sample_rate, 'g')
  end

  def self.update_stats(stats, delta=1, sample_rate=1, metric='c')
    stats = [stats].flatten

    data = {}
    stats.each do |stat|
      data[stat] = "%s|%s" % [delta, metric]
    end

    Statsd.send(data, sample_rate)
  end

  def self.send(data, sample_rate=1)
    begin
      host = config["host"] || "localhost"
      port = config["port"] || "8125"
      
      sampled_data = {}
      if sample_rate < 1
        if rand <= sample_rate
          data.each_pair do |stat, val|
            sampled_data[stat] = "%s|@%s" % [val, sample_rate]
          end
        end
      else
        sampled_data = data
      end
      
      udp = UDPSocket.new
      sampled_data.each_pair do |stat, val|
        send_data = "%s:%s" % [stat, val]
        udp.send send_data, 0, host, port
      end
    rescue => e
      puts e.message
    end
  end

  def self.config
    return @@config if self.class_variable_defined?(:@@config)
    begin
      config_path = File.join(File.dirname(__FILE__), "statsd.yml")
      # for Rails environments, check Rails.root/config/statsd.yml
      if defined? Rails
        config_path = File.join(Rails.root, "config", "statsd.yml")
        @@config = open(config_path) { |f| YAML.load(f) }
        @@config = @@config[Rails.env]
      else
        @@config = open(config_path) { |f| YAML.load(f) }
      end
    rescue => e
      puts "config: #{e.message}"
      @@config = {}
    end

    @@config
  end
end
Something went wrong with that request. Please try again.