Skip to content

Instantly share code, notes, and snippets.

@ono
Last active December 11, 2015 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ono/4588061 to your computer and use it in GitHub Desktop.
Save ono/4588061 to your computer and use it in GitHub Desktop.
NewRelic instrumentation async Sinatra. But note that this doesn't instrument EM.callback etc.. You still need custom instrumentation for callbacks.
# NewRelic instrumentation for async_sinatra.
#
# The following code was reffered.
# https://github.com/newrelic/rpm/blob/51767a324046baff0ea4d347862cd4696d73374d/lib/new_relic/agent/instrumentation/sinatra.rb
require 'newrelic_rpm'
require 'new_relic/agent/instrumentation/controller_instrumentation'
module AsyncSinatraInstrumentation
def async_handle_exception_with_newrelic &blk
if @monitored
async_handle_exception_without_newrelic &blk
else
@monitored = true
txn_name = ""
begin
txn_name = NewRelic::Agent::Instrumentation::Sinatra::NewRelic.transaction_name(self.class.routes, @request) do |pattern, keys, conditions|
process_route(pattern, keys, conditions) do
pattern.source
end
end
rescue
# For a safe reason.
txn_name = "UNKNOWN"
end
params = @request ? @request.params : {}
perform_action_with_newrelic_trace(:category => :sinatra,
:name => "A-" + txn_name,
:params => params) do
async_handle_exception_without_newrelic &blk
end
end
end
end
# With NewRelic Sinatra information, DependencyDetection won't be called after
# required 'newrelic_rpm', so simply put the instrumentation here.
if defined?(::Sinatra) && defined?(::Sinatra::Base) &&
Sinatra::Base.method_defined?(:dispatch_with_newrelic)
::NewRelic::Agent.logger.info 'Installing Async Sinatra instrumentation'
::Sinatra::Async::Helpers.class_eval do
include AsyncSinatraInstrumentation
alias async_handle_exception_without_newrelic async_handle_exception
alias async_handle_exception async_handle_exception_with_newrelic
end
end
# DependencyDetection.defer do
# @name = :async_sinatra
#
# depends_on do
# defined?(::Sinatra::Async) and not NewRelic::Control.instance['disable_async_sinatra']
# end
#
# executes do
# ::NewRelic::Agent.logger.info 'Installing Async Sinatra instrumentation'
# end
#
# executes do
# ::Sinatra::Async::Helpers.class_eval do
# include AsyncSinatraInstrumentation
# alias async_handle_exception_without_newrelic async_handle_exception
# alias async_handle_exception async_handle_exception_with_newrelic
# end
# end
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment