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



120
121
122
123
124
125
126
127
# File 'lib/statsd.rb', line 120

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.



103
104
105
# File 'lib/statsd.rb', line 103

def logger
  @logger
end

Instance Attribute Details

#hostObject

StatsD host. Defaults to 127.0.0.1.



96
97
98
# File 'lib/statsd.rb', line 96

def host
  @host
end

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

Users should call connect after changing this.



108
109
110
# File 'lib/statsd.rb', line 108

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

#portObject

StatsD admin port. Defaults to 8126.



99
100
101
# File 'lib/statsd.rb', line 99

def port
  @port
end

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

Users should call connect after changing this.



114
115
116
# File 'lib/statsd.rb', line 114

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)



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/statsd.rb', line 179

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.



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

def counters
  read_metric :counters
end

#delcounters(item) ⇒ Object

@param item

Deletes one or more counters. Wildcards are allowed.


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

def delcounters item
  delete_metric :counters, item
end

#delgauges(item) ⇒ Object

@param item

Deletes one or more gauges. Wildcards are allowed.


146
147
148
# File 'lib/statsd.rb', line 146

def delgauges item
  delete_metric :gauges, item
end

#deltimers(item) ⇒ Object

@param item

Deletes one or more timers. Wildcards are allowed.


152
153
154
# File 'lib/statsd.rb', line 152

def deltimers item
  delete_metric :timers, item
end

#gaugesObject

Reads all gauges from StatsD.



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

def gauges
  read_metric :gauges
end

#statsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/statsd.rb', line 162

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.



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

def timers
  read_metric :timers
end