Class: Statsd::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/statsd.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '127.0.0.1', port = 8126) ⇒ Admin

Returns a new instance of Admin.

Parameters:

  • host (String) (defaults to: '127.0.0.1')

    your statsd host

  • port (Integer) (defaults to: 8126)

    your statsd port



144
145
146
147
148
149
150
151
# File 'lib/statsd.rb', line 144

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

Class Attribute Details

.loggerObject

Set to a standard logger instance to enable debug logging.



127
128
129
# File 'lib/statsd.rb', line 127

def logger
  @logger
end

Instance Attribute Details

#hostObject

StatsD host. Defaults to 127.0.0.1.



120
121
122
# File 'lib/statsd.rb', line 120

def host
  @host
end

#host.=(value) ⇒ Object (writeonly)

Users should call connect after changing this.



132
133
134
# File 'lib/statsd.rb', line 132

def host=(host)
  @host = host || '127.0.0.1'
end

#portObject

StatsD admin port. Defaults to 8126.



123
124
125
# File 'lib/statsd.rb', line 123

def port
  @port
end

#port.=(value) ⇒ Object (writeonly)

Users should call connect after changing this.



138
139
140
# File 'lib/statsd.rb', line 138

def port=(port)
  @port = port || 8126
end

Instance Method Details

#connectObject

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)



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/statsd.rb', line 203

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

#countersObject

Reads all counters from StatsD.



164
165
166
# File 'lib/statsd.rb', line 164

def counters
  read_metric :counters
end

#delcounters(item) ⇒ Object

@param item

Deletes one or more counters. Wildcards are allowed.


182
183
184
# File 'lib/statsd.rb', line 182

def delcounters item
  delete_metric :counters, item
end

#delgauges(item) ⇒ Object

@param item

Deletes one or more gauges. Wildcards are allowed.


170
171
172
# File 'lib/statsd.rb', line 170

def delgauges item
  delete_metric :gauges, item
end

#deltimers(item) ⇒ Object

@param item

Deletes one or more timers. Wildcards are allowed.


176
177
178
# File 'lib/statsd.rb', line 176

def deltimers item
  delete_metric :timers, item
end

#gaugesObject

Reads all gauges from StatsD.



154
155
156
# File 'lib/statsd.rb', line 154

def gauges
  read_metric :gauges
end

#statsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/statsd.rb', line 186

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

#timersObject

Reads all timers from StatsD.



159
160
161
# File 'lib/statsd.rb', line 159

def timers
  read_metric :timers
end