NullStatsd
NullStatsd is a Null Object Pattern
implementation of a Statsd client, allowing for
conveniently stubbed Statsd objects in your development and testing environments.
Installation
Add this line to your application's Gemfile:
gem 'null_statsd'
And then execute:
$ bundle
Or install it yourself as:
$ gem install null_statsd
Usage
Create a wrapper around your Statsd implementation:
module MyStatsd
def self.new
if ENV["STATSD_HOST"] && ENV["STATSD_PORT"]
Statsd.new(ENV["STATSD_HOST"], ENV["STATSD_PORT"])
else
NullStatsd::Statsd.new(host: "fake.host", port: 1234, logger: Logger.new($stderr))
end
end
end
if Rails.env.production? || Rails.env.staging?
$statsd = Datadog::Statsd.new(ENV["DD_HOST"], ENV["DD_PORT"])
else
$statsd = NullStatsd::Statsd.new(host: "fa.ke", port: 42, logger: Rails.logger)
end
Create an instance and use it as normal:
MyStatsd.new.increment(...)
$statsd.increment(...)
Notice that your statsd
endpoint is not receiving data, but your logs are.
[NullStatsD host:42] Incrementing media.book.consumed with opts {"genre":"science_fiction"}
[NullStatsD host:42] Decrementing media.book.on_hand
[NullStatsD host:42] Recording timing info in book.checkout -> 0.512917 sec
Supported Calls
statsd = NullStatsd::Statsd.new(host: "a.co" port: 42, logger: Logger.new($stdout))
statsd.increment "media.book.consumed", genre: "horror"
statsd.decrement "media.book.on_hand", genre: "science fiction"
statsd.count "responses", 3
statsd.gauge "media.book.return_time", 12, measurement: "days"
statsd.histogram "media.book.lent.hour", 42
statsd.timing "book checkout", 94, tags: "speedy"
statsd.set "media.book.lent", 10_000_000
statsd.service_check "door.locked", "ok"
statsd.event "Leak", "The library roof has a leak on the west end. Please take care"
statsd.time("media.movie.consume") do
Movie.new().watch
end
statsd.close
statsd.batch do |s|
s.increment "foo.bar"
s.increment "baz"
end
Testing
rake spec
License
This gem is available as open source under the terms of the MIT License.
Contributing
Bug reports and pull requests are welcome on GitHub.