-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2144 from newrelic/roda_instrumentation
Add Roda instrumentation
- Loading branch information
Showing
17 changed files
with
465 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require_relative 'roda/instrumentation' | ||
|
||
DependencyDetection.defer do | ||
named :roda | ||
|
||
depends_on do | ||
defined?(Roda) && | ||
Gem::Version.new(Roda::RodaVersion) >= Gem::Version.new('3.19.0') && | ||
Roda::RodaPlugins::Base::ClassMethods.private_method_defined?(:build_rack_app) && | ||
Roda::RodaPlugins::Base::InstanceMethods.method_defined?(:_roda_handle_main_route) | ||
end | ||
|
||
executes do | ||
require_relative '../../rack/agent_hooks' | ||
require_relative '../../rack/browser_monitoring' | ||
|
||
NewRelic::Agent.logger.info('Installing Roda instrumentation') | ||
|
||
if use_prepend? | ||
require_relative 'roda/prepend' | ||
prepend_instrument Roda.singleton_class, NewRelic::Agent::Instrumentation::Roda::Build::Prepend | ||
prepend_instrument Roda, NewRelic::Agent::Instrumentation::Roda::Prepend | ||
else | ||
require_relative 'roda/chain' | ||
chain_instrument NewRelic::Agent::Instrumentation::Roda::Build::Chain | ||
chain_instrument NewRelic::Agent::Instrumentation::Roda::Chain | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module Roda | ||
module Chain | ||
def self.instrument! | ||
::Roda.class_eval do | ||
include ::NewRelic::Agent::Instrumentation::Roda::Tracer | ||
|
||
alias_method(:_roda_handle_main_route_without_tracing, :_roda_handle_main_route) | ||
|
||
def _roda_handle_main_route(*args) | ||
_roda_handle_main_route_with_tracing(*args) do | ||
_roda_handle_main_route_without_tracing(*args) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
module Build | ||
module Chain | ||
def self.instrument! | ||
::Roda.class_eval do | ||
include ::NewRelic::Agent::Instrumentation::Roda::Tracer | ||
|
||
class << self | ||
alias_method(:build_rack_app_without_tracing, :build_rack_app) | ||
|
||
def build_rack_app | ||
build_rack_app_with_tracing do | ||
build_rack_app_without_tracing | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
52 changes: 52 additions & 0 deletions
52
lib/new_relic/agent/instrumentation/roda/instrumentation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module Roda | ||
module Tracer | ||
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation | ||
|
||
def self.included(clazz) | ||
clazz.extend(self) | ||
end | ||
|
||
def newrelic_middlewares | ||
middlewares = [NewRelic::Rack::BrowserMonitoring] | ||
if NewRelic::Rack::AgentHooks.needed? | ||
middlewares << NewRelic::Rack::AgentHooks | ||
end | ||
middlewares | ||
end | ||
|
||
def build_rack_app_with_tracing | ||
unless NewRelic::Agent.config[:disable_roda_auto_middleware] | ||
newrelic_middlewares.each do |middleware_class| | ||
self.use middleware_class | ||
end | ||
end | ||
yield | ||
end | ||
|
||
# Roda makes use of Rack, so we can get params from the request object | ||
def rack_request_params | ||
begin | ||
@_request.params | ||
rescue => e | ||
NewRelic::Agent.logger.debug('Failed to get params from Rack request.', e) | ||
NewRelic::EMPTY_HASH | ||
end | ||
end | ||
|
||
def _roda_handle_main_route_with_tracing(*args) | ||
perform_action_with_newrelic_trace( | ||
category: :roda, | ||
name: TransactionNamer.transaction_name(request), | ||
params: ::NewRelic::Agent::ParameterFiltering::apply_filters(request.env, rack_request_params) | ||
) do | ||
yield | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic::Agent::Instrumentation | ||
module Roda | ||
module Prepend | ||
include ::NewRelic::Agent::Instrumentation::Roda::Tracer | ||
|
||
def _roda_handle_main_route(*args) | ||
_roda_handle_main_route_with_tracing(*args) { super } | ||
end | ||
end | ||
|
||
module Build | ||
module Prepend | ||
include ::NewRelic::Agent::Instrumentation::Roda::Tracer | ||
def build_rack_app | ||
build_rack_app_with_tracing { super } | ||
end | ||
end | ||
end | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
lib/new_relic/agent/instrumentation/roda/roda_transaction_namer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
module NewRelic | ||
module Agent | ||
module Instrumentation | ||
module Roda | ||
module TransactionNamer | ||
extend self | ||
|
||
ROOT = '/'.freeze | ||
REGEX_MULTIPLE_SLASHES = %r{^[/^\A]*(.*?)[/$?\z]*$}.freeze | ||
|
||
def transaction_name(request) | ||
path = request.path || ::NewRelic::Agent::UNKNOWN_METRIC | ||
name = path.gsub(REGEX_MULTIPLE_SLASHES, '\1') # remove any rogue slashes | ||
name = ROOT if name.empty? | ||
name = "#{request.request_method} #{name}" if request.respond_to?(:request_method) | ||
|
||
name | ||
rescue => e | ||
::NewRelic::Agent.logger.debug("#{e.class} : #{e.message} - Error encountered trying to identify Roda transaction name") | ||
::NewRelic::Agent::UNKNOWN_METRIC | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
require 'new_relic/control/frameworks/ruby' | ||
module NewRelic | ||
class Control | ||
module Frameworks | ||
# Contains basic control logic for Roda | ||
class Roda < NewRelic::Control::Frameworks::Ruby | ||
protected | ||
|
||
def install_shim | ||
super | ||
::Roda.class_eval { include NewRelic::Agent::Instrumentation::ControllerInstrumentation::Shim } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This file is distributed under New Relic's license terms. | ||
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. | ||
# frozen_string_literal: true | ||
|
||
instrumentation_methods :chain, :prepend | ||
|
||
RODA_VERSIONS = [ | ||
[nil, 2.4], | ||
['3.19.0', 2.4] | ||
] | ||
|
||
def gem_list(roda_version = nil) | ||
<<~RB | ||
gem 'roda'#{roda_version} | ||
gem 'rack' | ||
gem 'rack-test', '>= 0.8.0', :require => 'rack/test' | ||
RB | ||
end | ||
|
||
create_gemfiles(RODA_VERSIONS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
development: | ||
error_collector: | ||
enabled: true | ||
apdex_t: 0.5 | ||
monitor_mode: true | ||
license_key: bootstrap_newrelic_admin_license_key_000 | ||
instrumentation: | ||
roda: <%= $instrumentation_method %> | ||
app_name: test | ||
log_level: debug | ||
host: 127.0.0.1 | ||
api_host: 127.0.0.1 | ||
transaction_trace: | ||
record_sql: obfuscated | ||
enabled: true | ||
stack_trace_threshold: 0.5 | ||
transaction_threshold: 1.0 | ||
capture_params: false |
Oops, something went wrong.