Class: StatfulClient
- Inherits:
-
Object
- Object
- StatfulClient
- Defined in:
- lib/client.rb
Overview
Statful Client Instance
Defined Under Namespace
Classes: MyHash
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
Current client config.
Instance Method Summary collapse
-
#counter(name, value, options = {}) ⇒ Object
Sends a counter.
-
#flush_metrics ⇒ Object
Flush metrics buffer.
-
#gauge(name, value, options = {}) ⇒ Object
Sends a gauge.
-
#initialize(config = {}) ⇒ Object
constructor
Initialize the client.
- #new ⇒ Object
-
#put(metric, tags, value, agg = [], agg_freq = 10, sample_rate = nil, namespace = 'application', timestamp = nil) ⇒ Object
Adds a new metric to the buffer.
-
#timer(name, value, options = {}) ⇒ Object
Sends a timer.
Constructor Details
#initialize(config = {}) ⇒ Object
Initialize the client
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 |
# File 'lib/client.rb', line 31 def initialize(config = {}) user_config = MyHash[config].symbolize_keys if !user_config.has_key?(:transport) || !%w(udp http).include?(user_config[:transport]) raise ArgumentError.new('Transport is missing or invalid') end if user_config[:transport] == 'http' raise ArgumentError.new('Token is missing') if user_config[:token].nil? end if user_config.has_key?(:sample_rate) && !user_config[:sample_rate].between?(1, 100) raise ArgumentError.new('Sample rate is not within range (1-100)') end default_config = { :host => 'api.statful.com', :port => 443, :transport => 'http', :tags => {}, :sample_rate => 100, :namespace => 'application', :flush_size => 5 } @config = default_config.merge(user_config) @logger = @config[:logger] @buffer = [] @http = Net::HTTP.new(@config[:host], @config[:port]) self end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Current client config
8 9 10 |
# File 'lib/client.rb', line 8 def config @config end |
Instance Method Details
#counter(name, value, options = {}) ⇒ Object
Sends a counter
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/client.rb', line 100 def counter(name, value, = {}) = @config[:tags] = .merge([:tags]) unless [:tags].nil? aggregations = %w(sum count) aggregations.concat([:agg]) unless [:agg].nil? opts = { :agg_freq => 10, :namespace => 'application' }.merge(MyHash[].symbolize_keys) opts[:tags] = opts[:agg] = aggregations _put("counter.#{name}", opts[:tags], value.to_i, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace]) end |
#flush_metrics ⇒ Object
Flush metrics buffer
147 148 149 |
# File 'lib/client.rb', line 147 def flush_metrics flush end |
#gauge(name, value, options = {}) ⇒ Object
Sends a gauge
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/client.rb', line 127 def gauge(name, value, = {}) = @config[:tags] = .merge([:tags]) unless [:tags].nil? aggregations = %w(last) aggregations.concat([:agg]) unless [:agg].nil? opts = { :agg_freq => 10, :namespace => 'application' }.merge(MyHash[].symbolize_keys) opts[:tags] = opts[:agg] = aggregations _put("gauge.#{name}", opts[:tags], value, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace]) end |
#new ⇒ Object
11 12 13 |
# File 'lib/client.rb', line 11 def new self end |
#put(metric, tags, value, agg = [], agg_freq = 10, sample_rate = nil, namespace = 'application', timestamp = nil) ⇒ Object
Adds a new metric to the buffer
162 163 164 165 166 167 |
# File 'lib/client.rb', line 162 def put(metric, , value, agg = [], agg_freq = 10, sample_rate = nil, namespace = 'application', = nil) = @config[:tags] = .merge() unless .nil? _put(metric, , value, agg, agg_freq, sample_rate, namespace, ) end |
#timer(name, value, options = {}) ⇒ Object
Sends a timer
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/client.rb', line 73 def timer(name, value, = {}) = @config[:tags].merge({:unit => 'ms'}) = .merge([:tags]) unless [:tags].nil? aggregations = %w(avg p90 count) aggregations.concat([:agg]) unless [:agg].nil? opts = { :agg_freq => 10, :namespace => 'application' }.merge(MyHash[].symbolize_keys) opts[:tags] = opts[:agg] = aggregations _put("timer.#{name}", opts[:tags], value, opts[:agg], opts[:agg_freq], @config[:sample_rate], opts[:namespace]) end |