Simple wrapper for Graphite and Statsd
Check the test
folder for examples, if you need more explanations please contact us.
Add this line to your application's Gemfile:
gem 'mini_graphite'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mini_graphite
Dalia::MiniGraphite.config({
:graphite_host => "my.graphite.server.com",
:graphite_port => 2003, # default 2003
:statsd_host => "my.statsd.server.com",
:statsd_port => 8125, # default 8125
:mock_mode => false, # default false
:debug_mode => true # default false
})
Also support multiple servers:
Dalia::MiniGraphite.config({
:graphite_host => ["my.graphite1.server.com", "my.graphite2.server.com"],
:graphite_port => 2003, # default 2003
:statsd_host => ["my.statsd1.server.com", "my.statsd2.server.com", "my.statsd3.server.com"],
:statsd_port => 8125, # default 8125
:mock_mode => false, # default false
:debug_mode => true # default false
})
Dalia::MiniGraphite.datapoint("my.key", 120, Time.now) # to Graphite
Dalia::MiniGraphite.counter("my.key", 120) # to StatSD with `c` modifyer
Dalia::MiniGraphite.time("my.key", 120) # to StatSD with `ms` modifyer
# Dalia::MiniGraphite.benchmark_wrapper(key_prefix, result_send_method)
result =
Dalia::MiniGraphite.benchmark_wrapper("key_prefix", :length) do
sleep(1)
"RESULT"
end
puts result # => RESULT
This will send several signals:
- key_prefix.ini:1|c # At the begining of the block
- key_prefix.result:6|c # At the end of the block, send the block
result.send(result_send_method)
. Only ifresult_send_method
paramater is present. - key_prefix.time:1000|c # At the end of the block, with the Benchmark.realtime result of the execution
- key_prefix.end:1|c # At the end of the block
- key_prefix.time_stats:1000|ms # At the end of the block, with the Benchmark.realtime result of the execution
# Dalia::MiniGraphite::MethodWrapper.mini_graphite_benchmark_method(method_name, key_prefix)
class MyClass
# Trigger the method wrapper in an instance method
extend Dalia::MiniGraphite::MethodWrapper
def my_instance_method
end
mini_graphite_benchmark_method(:my_instance_method, "key_name")
# Trigger the method wrapper in a class method
def self.my_class_method
end
class << self
extend Dalia::MiniGraphite::MethodWrapper
mini_graphite_benchmark_method(:my_class_method, "key_name")
end
end
This will send several signals:
- key_prefix.time:1000|c # At the end of the method, with the Benchmark.realtime result of the execution
- key_prefix.end:1|c # At the end of the method
The rack middleware must be added to the Rack chain in config.ru passing along with it a block with config options:
-
set_graphite_key "graphite_key", /route_regexp/ # The regular expression will be used to match the Sinatra route and the corresponding graphite_key will be used to build the Graphite metrics:
-
"[graphite_key].count" # The counter of the times a particular route is requested.
-
"[graphite_key].duration" # The duration in milliseconds of each request/response cycle.
-
"[graphite_key].duration_stats" # The duration in milliseconds of each request/response cycle in a "ms" metric
If the requested url doesn't match any configured regular expression the Graphite metrics will not be sent.
Example:
# config.ru
use Dalia::MiniGraphite::RoutesReporter do
set_graphite_key "app.my_app.production.routes.active_surveys", /active_surveys\/this_should_be_a_password/
set_graphite_key "app.my_app.production.routes.get_surveys", /\:offer_click_id\/\:panel_user_id\/\:panel_user_id_kind/
set_graphite_key "app.my_app.production.routes.error", /error/
set_graphite_key "app.my_app.production.routes.homepage", /\/$/
end
run MyApp::App
- Fork it ( http://github.com//mini_graphite/fork )
- 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 new Pull Request