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



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

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.



129
130
131
# File 'lib/statsd.rb', line 129

def logger
  @logger
end

Instance Attribute Details

#hostObject

StatsD host. Defaults to 127.0.0.1.



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

def host
  @host
end

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

Users should call connect after changing this.



134
135
136
# File 'lib/statsd.rb', line 134

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

#portObject

StatsD admin port. Defaults to 8126.



125
126
127
# File 'lib/statsd.rb', line 125

def port
  @port
end

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

Users should call connect after changing this.



140
141
142
# File 'lib/statsd.rb', line 140

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)



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

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.



166
167
168
# File 'lib/statsd.rb', line 166

def counters
  read_metric :counters
end

#delcounters(item) ⇒ Object

@param item

Deletes one or more counters. Wildcards are allowed.


184
185
186
# File 'lib/statsd.rb', line 184

def delcounters item
  delete_metric :counters, item
end

#delgauges(item) ⇒ Object

@param item

Deletes one or more gauges. Wildcards are allowed.


172
173
174
# File 'lib/statsd.rb', line 172

def delgauges item
  delete_metric :gauges, item
end

#deltimers(item) ⇒ Object

@param item

Deletes one or more timers. Wildcards are allowed.


178
179
180
# File 'lib/statsd.rb', line 178

def deltimers item
  delete_metric :timers, item
end

#gaugesObject

Reads all gauges from StatsD.



156
157
158
# File 'lib/statsd.rb', line 156

def gauges
  read_metric :gauges
end

#statsObject



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

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.



161
162
163
# File 'lib/statsd.rb', line 161

def timers
  read_metric :timers
end