NullStatsd is a Statsd implementation which utilizes the Null Object Pattern, allowing for a fully stubbed Statsd object in your development and testing environments.
Add this line to your application's Gemfile:
gem 'null_statsd'
And then execute:
$ bundle
Or install it yourself as:
$ gem install null_statsd
Create a thin wrapper around your Statsd implementation:
class MyStatsd
def self.new
if ENV["STATSD_URL"] # OR if Rails.development || Rails.staging ...
Statsd.new(statsd_host, statsd_port, *additional_params)
else
NullStatsd::Statsd.new(host: statsd_host, port: statsd_port, logger: Rails.logger)
end
end
end
Create an instance and use it as normal:
MyStatsd.new.increment(...)
Notice that your statsd
endpoint is not receiving data. Also notice that your logs are receiving data.
[NullStatsD :-] Incrementing media.book.consumed with opts genre:science fiction
[NullStatsD :-] Decrementing media.book.on_hand
[NullStatsD :-] Recording timing info in book checkout -> 0.512917 sec
instance = NullStatsd::Statsd.new(host: "https://fakestatsd.com", port: 4242, logger: $stdout
instance.increment "media.book.consumed", genre: "horror"
[NullStatsD :-] Incrementing media.book.consumed with opts genre:horror
instance.decrement "media.book.on_hand", genre: "science fiction"
[NullStatsD :-] Decrementing media.book.on_hand with opts genre:science fiction
instance.count "responses", 3
[NullStatsD :-] Increasing responses by 3
instance.guage "media.book.return_time", 12, measurement: "days"
[NullStatsD :-] Setting guage media.book.return_time to 12 with opts measurement:days
instance.histogram "media.book.lent.hour", 42
[NullStatsD :-] Logging histogram media.book.lent.hour -> 42
instance.timing "book checkout", 94, tags: "speedy"
[NullStatsD :-] Timing book checkout at 94 ms with opts tags:speedy
instance.set "media.book.lent", 10_000_000
[NullStatsD :-] Setting media.book.lent to 10000000
instance.service_check "door.locked", "ok"
[NullStatsD :-] Service check door.locked: ok
instance.event "Leak", "The library roof has a leak on the west end. Please take care"
[NullStatsD :-] Event Leak: The library roof has a leak on the west end. Please take care
instance.time("media.movie.consume") do
Movie.new().watch
end
[NullStatsD :-] Recording timing info in media.movie.consumed -> 12323 sec
instance.close
[NullStatsD :-] Close called
rake spec
The gem is available as open source under the terms of the MIT License.
Library created by UserTesting.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Bug reports and pull requests are welcome on GitHub at https://github.com/usertesting/null_statsd