-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,6 @@ Metrics/CyclomaticComplexity: | |
|
||
Metrics/PerceivedComplexity: | ||
Max: 15 | ||
|
||
Lint/UnusedMethodArgument: | ||
Enabled: false |
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 |
---|---|---|
|
@@ -66,4 +66,5 @@ appraise "contrib" do | |
gem "hiredis" | ||
gem "rack-test" | ||
gem "sinatra" | ||
gem "sidekiq" | ||
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
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 @@ | ||
|
||
require 'sidekiq/api' | ||
|
||
require 'ddtrace/ext/app_types' | ||
|
||
sidekiq_vs = Gem::Version.new(Sidekiq::VERSION) | ||
sidekiq_min_vs = Gem::Version.new('4.0.0') | ||
if sidekiq_vs < sidekiq_min_vs | ||
raise "sidekiq version #{sidekiq_vs} is not supported yet " \ | ||
+ "(supporting versions >=#{sidekiq_min_vs})" | ||
end | ||
|
||
Datadog::Tracer.log.info("activating instrumentation for sidekiq #{sidekiq_vs}") | ||
|
||
module Datadog | ||
module Contrib | ||
module Sidekiq | ||
# Middleware is a Sidekiq server-side middleware which traces executed | ||
# jobs. | ||
class Tracer | ||
def initialize(options) | ||
@enabled = options.fetch(:enabled, true) | ||
@default_service = options.fetch(:default_service, 'sidekiq') | ||
@tracer = options.fetch(:tracer, Datadog.tracer) | ||
@debug = options.fetch(:debug, false) | ||
@trace_agent_hostname = options.fetch(:trace_agent_hostname, | ||
Datadog::Writer::HOSTNAME) | ||
@trace_agent_port = options.fetch(:trace_agent_port, | ||
Datadog::Writer::PORT) | ||
|
||
Datadog::Tracer.debug_logging = @debug | ||
|
||
@tracer.enabled = @enabled | ||
@tracer.set_service_info(@default_service, 'sidekiq', | ||
Datadog::Ext::AppTypes::JOB_PROCESSOR) | ||
end | ||
|
||
def call(worker, job, queue) | ||
return yield unless @enabled | ||
|
||
@tracer.trace('sidekiq.job', | ||
service: @default_service, span_type: 'job') do |span| | ||
span.resource = job['class'] | ||
span.set_tag('sidekiq.job.queue', job['queue']) | ||
|
||
yield | ||
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,28 @@ | ||
|
||
require 'contrib/sidekiq/tracer_test_base' | ||
|
||
class DisabledTracerTest < TracerTestBase | ||
class EmptyWorker | ||
include Sidekiq::Worker | ||
|
||
def perform() | ||
end | ||
end | ||
|
||
def setup | ||
super | ||
|
||
Sidekiq::Testing.server_middleware do |chain| | ||
chain.add(Datadog::Contrib::Sidekiq::Tracer, | ||
tracer: @tracer, enabled: false) | ||
end | ||
end | ||
|
||
def test_empty | ||
EmptyWorker.perform_async() | ||
|
||
spans = @writer.spans() | ||
assert_equal(0, spans.length) | ||
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,63 @@ | ||
|
||
require 'contrib/sidekiq/tracer_test_base' | ||
|
||
class TracerTest < TracerTestBase | ||
class TestError < StandardError; end | ||
|
||
class EmptyWorker | ||
include Sidekiq::Worker | ||
|
||
def perform() | ||
end | ||
end | ||
|
||
class ErrorWorker | ||
include Sidekiq::Worker | ||
|
||
def perform() | ||
raise TestError, 'job error' | ||
end | ||
end | ||
|
||
def setup | ||
super | ||
|
||
Sidekiq::Testing.server_middleware do |chain| | ||
chain.add(Datadog::Contrib::Sidekiq::Tracer, | ||
tracer: @tracer, enabled: true) | ||
end | ||
end | ||
|
||
def test_empty | ||
EmptyWorker.perform_async() | ||
|
||
spans = @writer.spans() | ||
assert_equal(1, spans.length) | ||
|
||
span = spans[0] | ||
assert_equal('sidekiq', span.service) | ||
assert_equal('TracerTest::EmptyWorker', span.resource) | ||
assert_equal('default', span.get_tag('sidekiq.job.queue')) | ||
assert_equal(0, span.status) | ||
assert_nil(span.parent) | ||
end | ||
|
||
def test_error | ||
begin | ||
ErrorWorker.perform_async() | ||
rescue TestError | ||
end | ||
|
||
spans = @writer.spans() | ||
assert_equal(1, spans.length) | ||
|
||
span = spans[0] | ||
assert_equal('sidekiq', span.service) | ||
assert_equal('TracerTest::ErrorWorker', span.resource) | ||
assert_equal('default', span.get_tag('sidekiq.job.queue')) | ||
assert_equal(1, span.status) | ||
assert_equal('job error', span.get_tag(Datadog::Ext::Errors::MSG)) | ||
assert_equal('TracerTest::TestError', span.get_tag(Datadog::Ext::Errors::TYPE)) | ||
assert_nil(span.parent) | ||
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,27 @@ | ||
|
||
require 'sidekiq/testing' | ||
require 'ddtrace' | ||
require 'ddtrace/contrib/sidekiq/tracer' | ||
require 'helper' | ||
|
||
class TracerTestBase < Minitest::Test | ||
REDIS_HOST = '127.0.0.1'.freeze() | ||
REDIS_PORT = 46379 | ||
|
||
def setup | ||
@writer = FauxWriter.new() | ||
@tracer = Datadog::Tracer.new(writer: @writer) | ||
|
||
redis_url = "redis://#{REDIS_HOST}:#{REDIS_PORT}" | ||
|
||
Sidekiq.configure_client do |config| | ||
config.redis = { url: redis_url } | ||
end | ||
|
||
Sidekiq.configure_server do |config| | ||
config.redis = { url: redis_url } | ||
end | ||
|
||
Sidekiq::Testing.inline! | ||
end | ||
end |