-
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 #1742 from newrelic/active-support-instrumentation
Add Active Support instrumentation
- Loading branch information
Showing
8 changed files
with
371 additions
and
12 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
74 changes: 74 additions & 0 deletions
74
lib/new_relic/agent/instrumentation/active_support_subscriber.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,74 @@ | ||
# 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/agent/instrumentation/notifications_subscriber' | ||
|
||
module NewRelic | ||
module Agent | ||
module Instrumentation | ||
class ActiveSupportSubscriber < NotificationsSubscriber | ||
def start(name, id, payload) | ||
return unless state.is_execution_traced? | ||
|
||
start_segment(name, id, payload) | ||
rescue => e | ||
log_notification_error(e, name, 'start') | ||
end | ||
|
||
def finish(name, id, payload) | ||
return unless state.is_execution_traced? | ||
|
||
finish_segment(id, payload) | ||
rescue => e | ||
log_notification_error(e, name, 'finish') | ||
end | ||
|
||
def start_segment(name, id, payload) | ||
segment = Tracer.start_segment(name: metric_name(name, payload)) | ||
|
||
add_segment_params(segment, payload) | ||
push_segment(id, segment) | ||
end | ||
|
||
def add_segment_params(segment, payload) | ||
segment.params[:key] = payload[:key] | ||
segment.params[:store] = payload[:store] | ||
segment.params[:hit] = payload[:hit] if payload.key?(:hit) | ||
segment.params[:super_operation] = payload[:super_operation] if payload.key?(:super_operation) | ||
segment | ||
end | ||
|
||
def finish_segment(id, payload) | ||
if segment = pop_segment(id) | ||
if exception = exception_object(payload) | ||
segment.notice_error(exception) | ||
end | ||
segment.finish | ||
end | ||
end | ||
|
||
def metric_name(name, payload) | ||
store = payload[:store] | ||
method = method_from_name(name) | ||
"Ruby/ActiveSupport/#{store}/#{method}" | ||
end | ||
|
||
PATTERN = /\Acache_([^\.]*)\.active_support\z/ | ||
UNKNOWN = "unknown".freeze | ||
|
||
METHOD_NAME_MAPPING = Hash.new do |h, k| | ||
if PATTERN =~ k | ||
h[k] = $1 | ||
else | ||
h[k] = UNKNOWN | ||
end | ||
end | ||
|
||
def method_from_name(name) | ||
METHOD_NAME_MAPPING[name] | ||
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,12 @@ | ||
# 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 | ||
|
||
# This is a helper file that will allow apps using ActiveSupport without Rails | ||
# to still leverage all ActiveSupport based instrumentation functionality | ||
# offered by the agent that would otherwise be gated by the detection of Rails. | ||
|
||
# ActiveSupport notifications custom events | ||
if !defined?(Rails) && defined?(ActiveSupport::Notifications) && defined?(ActiveSupport::IsolatedExecutionState) | ||
require_relative 'rails_notifications/custom_events' | ||
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
12 changes: 12 additions & 0 deletions
12
test/new_relic/agent/instrumentation/active_support_subscriber_test.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,12 @@ | ||
# 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 '../../../test_helper' | ||
require 'new_relic/agent/instrumentation/active_support_subscriber' | ||
|
||
if defined?(ActiveSupport) | ||
require_relative 'rails/active_support_subscriber' | ||
else | ||
puts "Skipping tests in #{__FILE__} because Active Support is unavailable" | ||
end |
Oops, something went wrong.