Skip to content

Repository files navigation

CanvasStatsd

configurable statsd client proxy

Configuration

Set a few enviroment variables:

export CANVAS_STATSD_HOST=statsd.example.org
export CANVAS_STATSD_PORT=1234
export CANVAS_STATSD_NAMESPACE=my_app.prod
export CANVAS_STATSD_APPEND_HOSTNAME=false

Or pass a hash to CanvasStatsd.settings

settings={host: 'statsd.example.org'port: 1234namespace: 'my_app.prod'append_hostname: false}CanvasStatsd.settings=settings

Values passed to CanvasStatsd.settings will be merged into and take precedence over any existing ENV vars

Configuration Options

Only the host (or CANVAS_STATSD_HOST ENV var) is required, all other config is optional

host

Location of the statsd box you want to send stats to.

port

port of the statsd box you want to send stats to.

namespace

If a namespace is defined, it'll be prepended to the stat name. So the following:

settings={host: 'statsd.example.org'namespace: 'my_app.prod'}CanvasStatsd.settings=settingsCanvasStatsd::Statsd.timing('some.stat',300)

would use my_app.prod.some.stat as it's stat name.

append_hostname

The hostname of the server will be appended to the stat name, unless append_hostname: false is specified. So if the namespace is canvas and the hostname is app01, the final stat name of my_stat would be canvas.my_stat.app01 (assuming the default statsd/graphite configuration)

Usage

Outside of configuration, app code generally interacts with the CanvasStatsd::Statsd object, which is a proxy class to communicate messages to statsd.

Available statsd messages are described in:

So for instance:

ms=Benchmark.ms{ ..code.. }CanvasStatsd::Statsd.timing("my_stat",ms)

If statsd isn't configured and enabled, then calls to CanvasStatsd::Statsd.* will do nothing and return nil.

Default Metrics Tracking

CanvasStatsd ships with a several trackers that can capture several performance metrics. To enable these default metrics tracking in your rails app, you enable the ones you want, and then enable request tracking:

# config/initializers/canvas_statsd.rbCanvasStatsd::DefaultTracking.track_sqlCanvasStatsd::DefaultTracking.track_cacheCanvasStatsd::DefaultTracking.track_active_recordCanvasStatsd::RequestTracking.enable

This will track the following (as statsd timings) per request:

Metric TypeStatsd keyDescription
totalcontroller.action.totaltotal time spent on controller action*
dbcontroller.action.dbtime spent in the db*
viewcontroller.action.viewtime spent build views*
sql writecontroller.action.sql.writenumber of sql writes
sql readcontroller.action.sql.readnumber of sql reads
sql cachecontroller.action.sql.cachenumber of sql cache
active recordcontroller.action.active_recordnumber of ActiveRecord objects created **
cache readcontroller.action.cache.readnumber of cache reads

* as reported by ActiveSupport::Notifications

** as reported by aroi

If you'd like CanvasStatsd to log these metrics (as well as sending them to statsd), pass a logger object along like so:

# log default metrics to environment logs in RailsCanvasStatsd::RequestTracking.enablelogger: Rails.logger

Block tracking

You can easily track the performance of any block of code using all enabled metrics. Just be careful that your key isn't too dynamic, causing performance problems for your statsd server.

CanvasStatsd::DefaultTracking.track_sqlCanvasStatsd::DefaultTracking.track_cacheCanvasStatsd::DefaultTracking.track_active_recordCanvasStatsd::BlockTracking.track("my_important_job")dosleep(10)end

If you want to keep track of both exclusive and inclusive times for a re-entrant piece of code, you just need to tell CanvasStatsd which category to track along:

CanvasStatsd::BlockTracking.track("my_important_job",category: :my_stuff)dosleep(10)CanvasStatsd::BlockTracking.track("my_other_important_job",category: :my_stuff)dosleep(5)endend

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

configurable statsd client proxy

Resources

Stars

0 stars

Watchers

23 watching

Forks

Releases

Packages

Used by

Contributors

Languages