Skip to content

Commit

Permalink
Support Sidekiq v7.0+
Browse files Browse the repository at this point in the history
* Conditionally stop attempting to apply instrumentation to Delayed
  Extensions, which were dropped in v7.0. Attempting to do so was
  causing the installation of Sidekiq instrumentation by the agent to
  fail when used with Sidekiq v7.0.
* Have the agent's client and server middleware classes inherit from
  the new Sidekiq v7.0 middleware base classes.
  • Loading branch information
fallwith committed Nov 11, 2022
1 parent 9471a2c commit dcb7c3f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## v8.13.0

Version 8.13.0 of the agent updates our Rack instrumentation and delivers some bugfixes.
Version 8.13.0 of the agent updates our Rack and Sidekiq instrumentation and delivers some bugfixes.

* **Support for Sidekiq v7**

Sidekiq v7.0 has removed Delayed Extensions and began offering client and server [middleware](https://github.com/mperham/sidekiq/blob/main/docs/middleware.md) classes to inherit from. The agent's Sidekiq instrumentation has been updated accordingly. The agent's behavior when used with older Sidekiq versions will remain unaffected. [PR#1615](https://github.com/newrelic/newrelic-ruby-agent/pull/1615)

* **Support for Rack v3+ Rack::Builder#new accepting a block**

Expand Down
2 changes: 2 additions & 0 deletions lib/new_relic/agent/instrumentation/sidekiq/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module NewRelic::Agent::Instrumentation::Sidekiq
class Client
include Sidekiq::ClientMiddleware if defined?(Sidekiq::ClientMiddleware)

def call(_worker_class, job, *_)
job[NewRelic::NEWRELIC_KEY] ||= distributed_tracing_headers if ::NewRelic::Agent.config[:'distributed_tracing.enabled']
yield
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

class Sidekiq::Extensions::DelayedClass
def newrelic_trace_args(msg, queue)
(target, method_name, _args) = if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load(msg['args'][0])
else
YAML.load(msg['args'][0])
end
if Sidekiq::VERSION < '7.0.0'
class Sidekiq::Extensions::DelayedClass
def newrelic_trace_args(msg, queue)
(target, method_name, _args) = if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load(msg['args'][0])
else
YAML.load(msg['args'][0])
end

{
:name => method_name,
:class_name => target.name,
:category => 'OtherTransaction/SidekiqJob'
}
rescue => e
NewRelic::Agent.logger.error("Failure during deserializing YAML for Sidekiq::Extensions::DelayedClass", e)
NewRelic::Agent::Instrumentation::Sidekiq::Server.default_trace_args(msg)
{
:name => method_name,
:class_name => target.name,
:category => 'OtherTransaction/SidekiqJob'
}
rescue => e
NewRelic::Agent.logger.error("Failure during deserializing YAML for Sidekiq::Extensions::DelayedClass", e)
NewRelic::Agent::Instrumentation::Sidekiq::Server.default_trace_args(msg)
end
end
end
1 change: 1 addition & 0 deletions lib/new_relic/agent/instrumentation/sidekiq/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module NewRelic::Agent::Instrumentation::Sidekiq
class Server
include NewRelic::Agent::Instrumentation::ControllerInstrumentation
include Sidekiq::ServerMiddleware if defined?(Sidekiq::ServerMiddleware)

# Client middleware has additional parameters, and our tests use the
# middleware client-side to work inline.
Expand Down
3 changes: 1 addition & 2 deletions test/multiverse/suites/sidekiq/Envfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ suite_condition("Sidekiq does not run on JRuby") do
end

SIDEKIQ_VERSIONS = [
# TODO: Uncomment once we refactor tests to use Sidekiq 7's logger, issue #1567
# [nil, 2.5],
[nil, 2.7],
['6.4.0', 2.5],
['5.0.3', 2.3],
['4.2.0', 2.2]
Expand Down
10 changes: 9 additions & 1 deletion test/multiverse/suites/sidekiq/sidekiq_instrumentation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ class SidekiqTest < Minitest::Test

string_logger = ::Logger.new(@sidekiq_log)
string_logger.formatter = Sidekiq.logger.formatter
Sidekiq.logger = string_logger
set_sidekiq_logger(string_logger)
end

def set_sidekiq_logger(logger)
if Sidekiq::VERSION >= '7.0.0'
Sidekiq.default_configuration.logger = logger
else
Sidekiq.logger = logger
end
end

def teardown
Expand Down

0 comments on commit dcb7c3f

Please sign in to comment.