-
Notifications
You must be signed in to change notification settings - Fork 599
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 #1761 from newrelic/city_gardens
- Loading branch information
Showing
6 changed files
with
173 additions
and
2 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
41 changes: 41 additions & 0 deletions
41
lib/new_relic/agent/instrumentation/active_job_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,41 @@ | ||
# 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 ActiveJobSubscriber < NotificationsSubscriber | ||
PAYLOAD_KEYS = %i[adapter db_runtime error job wait] | ||
|
||
def add_segment_params(segment, payload) | ||
PAYLOAD_KEYS.each do |key| | ||
segment.params[key] = payload[key] if payload.key?(key) | ||
end | ||
end | ||
|
||
def metric_name(name, payload) | ||
queue = payload[:job].queue_name | ||
method = method_from_name(name) | ||
"Ruby/ActiveJob/#{queue}/#{method}" | ||
end | ||
|
||
PATTERN = /\A([^\.]+)\.active_job\z/ | ||
|
||
METHOD_NAME_MAPPING = Hash.new do |h, k| | ||
if PATTERN =~ k | ||
h[k] = $1 | ||
else | ||
h[k] = NewRelic::UNKNOWN | ||
end | ||
end | ||
|
||
def method_from_name(name) | ||
METHOD_NAME_MAPPING[name] | ||
end | ||
end | ||
end | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
test/new_relic/agent/instrumentation/active_job_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,14 @@ | ||
# 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_job_subscriber' | ||
|
||
if defined?(ActiveJob) && | ||
ActiveJob.respond_to?(:gem_version) && | ||
ActiveJob.gem_version >= Gem::Version.new('6.0.0') | ||
require_relative 'rails/active_job_subscriber' | ||
else | ||
puts "Skipping tests in #{__FILE__} because ActiveJob is unavailable or < 6.0" | ||
end |
94 changes: 94 additions & 0 deletions
94
test/new_relic/agent/instrumentation/rails/active_job_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,94 @@ | ||
# 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_job_subscriber' | ||
|
||
module NewRelic::Agent::Instrumentation | ||
class RetryMe < StandardError; end | ||
class DiscardMe < StandardError; end | ||
|
||
class TestJob < ActiveJob::Base | ||
retry_on RetryMe | ||
|
||
discard_on DiscardMe | ||
|
||
def perform(error = nil) | ||
raise error if error | ||
|
||
rand(1138) | ||
end | ||
end | ||
|
||
class ActiveJobSubscriberTest < Minitest::Test | ||
NAME = 'perform.active_job' | ||
ID = 71741 | ||
SUBSCRIBER = NewRelic::Agent::Instrumentation::ActiveJobSubscriber.new | ||
|
||
def test_segment_naming_with_unknown_method | ||
assert_equal 'Ruby/ActiveJob/default/Unknown', | ||
SUBSCRIBER.send(:metric_name, 'indecipherable', {job: TestJob.new}) | ||
end | ||
|
||
# perform.active_job | ||
def test_perform_active_job | ||
job = TestJob.new | ||
in_transaction do |txn| | ||
job.perform_now | ||
validate_transaction(txn, 'perform') | ||
end | ||
end | ||
|
||
# enqueue_at.active_job | ||
def test_enqueue_at_active_job | ||
in_transaction do |txn| | ||
TestJob.set(wait_until: 7.hours.from_now).perform_later | ||
validate_transaction(txn, 'enqueue_at') | ||
end | ||
end | ||
|
||
# enqueue.active_job | ||
def test_enqueue_active_job | ||
in_transaction do |txn| | ||
TestJob.perform_later | ||
validate_transaction(txn, 'enqueue') | ||
end | ||
end | ||
|
||
# perform_start.active_job | ||
# enqueue_retry.active_job | ||
def test_perform_start_active_job_and_enqueue_retry_active_job | ||
in_transaction do |txn| | ||
TestJob.perform_now(RetryMe) | ||
validate_transaction(txn, %w[enqueue_retry perform_start]) | ||
end | ||
end | ||
|
||
# discard.active_job | ||
def test_discard_active_job | ||
in_transaction do |txn| | ||
TestJob.perform_now(DiscardMe) | ||
validate_transaction(txn, 'discard') | ||
end | ||
end | ||
|
||
# TODO: test for retry_stopped.active_job | ||
|
||
private | ||
|
||
def validate_transaction(txn, methods = []) | ||
methods = Array(methods) | ||
segments = txn.segments.select { |s| s.name.start_with?('Ruby/ActiveJob') } | ||
|
||
refute_empty segments | ||
|
||
methods.each do |method| | ||
segment = segments.detect { |s| s.name == "Ruby/ActiveJob/default/#{method}" } | ||
|
||
assert segment | ||
assert_equal 'ActiveJob::QueueAdapters::AsyncAdapter', segment.params[:adapter].class.name | ||
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