Watchman

Watchman is your friend who monitors your processes so you don't have to.

Installation

gem "rt-watchman", :require => "watchman"

Usage

First, set up the host and the port of the metrics server:

Watchman.host = "localhost"
Watchman.port = 22345

To submit a value to statsd from your service use:

Watchman.submit(name, value, type)

Available types:

  • :gauge default
  • :timing
  • :count

Submitting a simple gauge value from your service would look like:

Watchman.submit("number.of.kittens", 30)

To benchmark a part of your service:

Watchman.benchmark("time.to.wake.up") do
  puts "Sleeping"
  sleep 10
  puts "Wake up"
end

To submit a time value in milliseconds use:

Watchman.submit("number.of.kittens", 30, :timing)

To submit a count value use:

# To increse:
Watchman.increment("number.of.kittens")

# or decrese:
Watchman.decrement("number.of.kittens")

Alternatively you can use:

# To increse:
Watchman.submit("number.of.kittens", 1, :count)

# or decrese:
Watchman.submit("number.of.kittens", -1, :count)

to achieve the equivalent effect, with the added possibility of tweaking the value.

Tags

If you want to use a variable that changes often, don't use this:

Watchman.submit("user.#{id}", 30)

Use tags. A list of tags is an optional last parameter of :submit, :benchmark, :increment and :decrement methods.

Watchman.submit("user", 30, tags: [id])

Tags list is limited to 3 values.

Global metric prefix

If you want to prepend all the metric names with a prefix, do the following:

Watchman.prefix = "production.server1"

Then, all your metrics will be saved with that prefix. For example:

Watchman.submit("high.score", 100) # => production.server1.high.score = 100

Test mode for Watchman

In tests you can set the following:

Watchman.test_mode = true

That way watchman will use a stubbed client, and won't send any data to the metric server.