Permalink
Browse files

Set DISABLE_NEW_RELIC_GRAPE at runtime to disable instrumentation.

  • Loading branch information...
dblock committed Dec 27, 2012
1 parent 2e6d9f9 commit b244fecb763510ac110024461f38067a9863df5d
Showing with 67 additions and 13 deletions.
  1. +2 −1 CHANGELOG.md
  2. +8 −3 README.md
  3. +19 −9 lib/newrelic-grape/instrument.rb
  4. +38 −0 spec/newrelic-grape/instrumenter_spec.rb
View
@@ -1,7 +1,8 @@
Next Release
============
-* Changed namespace from `Newrelic` to `NewRelic` to be consistent with the RPM gem - [@dblock](https://github.com/dblock)
+* Changed namespace from `Newrelic` to `NewRelic` to be consistent with the RPM gem - [@dblock](https://github.com/dblock).
+* Set `DISABLE_NEW_RELIC_GRAPE` at runtime to disable instrumentation - [@dblock](https://github.com/dblock).
1.0.0 (12/18/2012)
==================
View
@@ -20,13 +20,18 @@ Or install it yourself as:
That's it.
+## Disabling Instrumentation
+
+Set `disable_grape` in `newrelic.yml` or `ENV['DISABLE_NEW_RELIC_GRAPE']` to disable instrumentation.
+
## 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 new Pull Request
+3. Update `CHANGELOG.md` describing your changes
+4. Commit your changes (`git commit -am 'Add some feature'`)
+5. Push to the branch (`git push origin my-new-feature`)
+6. Create new Pull Request
[0]: https://github.com/intridea/grape
[1]: http://artsy.github.com/blog/2012/11/29/measuring-performance-in-grape-apis-with-new-relic/
@@ -6,14 +6,17 @@ class Grape < ::Grape::Middleware::Base
def call!(env)
@env = env
-
- @newrelic_request = ::Rack::Request.new(env)
- trace_options = {
- :category => :rack,
- :path => "#{request_method} #{request_path}",
- :request => @newrelic_request
- }
- perform_action_with_newrelic_trace(trace_options) do
+ unless NewRelic::Agent::Instrumentation::Grape.disabled?
+ @newrelic_request = ::Rack::Request.new(env)
+ trace_options = {
+ :category => :rack,
+ :path => "#{request_method} #{request_path}",
+ :request => @newrelic_request
+ }
+ perform_action_with_newrelic_trace(trace_options) do
+ @app_response = @app.call(@env)
+ end
+ else
@app_response = @app.call(@env)
end
end
@@ -25,6 +28,13 @@ def request_path
def request_method
@newrelic_request.request_method
end
+
+ class << self
+ def disabled?
+ ::NewRelic::Control.instance['disable_grape'] || ENV['DISABLE_NEW_RELIC_GRAPE']
+ end
+ end
+
end
end
end
@@ -34,7 +44,7 @@ def request_method
@name = :grape
depends_on do
- defined?(::Grape) and not ::NewRelic::Control.instance['disable_grape']
+ defined?(::Grape) && ! NewRelic::Agent::Instrumentation::Grape.disabled?
end
executes do
@@ -21,6 +21,44 @@ def app
last_response.body.should == "Hello World"
end
+ it "not disabled?" do
+ NewRelic::Agent::Instrumentation::Grape.should_not be_disabled
+ end
+
+ context "disable_grape" do
+ before do
+ ::NewRelic::Control.stub(:instance).and_return({ 'disable_grape' => true })
+ end
+ it "disabled?" do
+ NewRelic::Agent::Instrumentation::Grape.should be_disabled
+ end
+ it "turns off instrumentation" do
+ NewRelic::Agent::Instrumentation::Grape.any_instance.should_not_receive(:perform_action_with_newrelic_trace).and_yield
+ get "/hello"
+ last_response.status.should == 200
+ last_response.body.should == "Hello World"
+ end
+ end
+
+ context "DISABLE_NEW_RELIC_GRAPE" do
+ before do
+ @disable_new_relic_grape = ENV['DISABLE_NEW_RELIC_GRAPE']
+ ENV['DISABLE_NEW_RELIC_GRAPE'] = '1'
+ end
+ it "disabled?" do
+ NewRelic::Agent::Instrumentation::Grape.should be_disabled
+ end
+ it "turns off instrumentation" do
+ NewRelic::Agent::Instrumentation::Grape.any_instance.should_not_receive(:perform_action_with_newrelic_trace).and_yield
+ get "/hello"
+ last_response.status.should == 200
+ last_response.body.should == "Hello World"
+ end
+ after do
+ ENV['DISABLE_NEW_RELIC_GRAPE'] = @disable_new_relic_grape
+ end
+ end
+
end

0 comments on commit b244fec

Please sign in to comment.