-
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.
Merge pull request #602 from dirk/sidekiq-client-tracer
Sidekiq client-side tracing
- Loading branch information
Showing
19 changed files
with
217 additions
and
97 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'ddtrace/contrib/sidekiq/tracing' | ||
|
||
module Datadog | ||
module Contrib | ||
module Sidekiq | ||
# Tracer is a Sidekiq client-side middleware which traces job enqueues/pushes | ||
class ClientTracer | ||
include Tracing | ||
|
||
def initialize(options = {}) | ||
super | ||
@sidekiq_service = options[:client_service_name] || Datadog.configuration[:sidekiq][:client_service_name] | ||
end | ||
|
||
# Client middleware arguments are documented here: | ||
# https://github.com/mperham/sidekiq/wiki/Middleware#client-middleware | ||
def call(worker_class, job, queue, redis_pool) | ||
service = @sidekiq_service | ||
set_service_info(service) | ||
|
||
resource = job_resource(job) | ||
|
||
@tracer.trace(Ext::SPAN_PUSH, service: service) do |span| | ||
span.resource = resource | ||
span.set_tag(Ext::TAG_JOB_ID, job['jid']) | ||
span.set_tag(Ext::TAG_JOB_QUEUE, job['queue']) | ||
span.set_tag(Ext::TAG_JOB_WRAPPER, job['class']) if job['wrapped'] | ||
|
||
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
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,50 @@ | ||
require 'ddtrace/contrib/sidekiq/tracing' | ||
|
||
module Datadog | ||
module Contrib | ||
module Sidekiq | ||
# Tracer is a Sidekiq server-side middleware which traces executed jobs | ||
class ServerTracer | ||
include Tracing | ||
|
||
def initialize(options = {}) | ||
super | ||
@sidekiq_service = options[:service_name] || Datadog.configuration[:sidekiq][:service_name] | ||
end | ||
|
||
def call(worker, job, queue) | ||
resource = job_resource(job) | ||
|
||
service = service_from_worker_config(resource) || @sidekiq_service | ||
set_service_info(service) | ||
|
||
@tracer.trace(Ext::SPAN_JOB, service: service, span_type: Datadog::Ext::AppTypes::WORKER) do |span| | ||
span.resource = resource | ||
span.set_tag(Ext::TAG_JOB_ID, job['jid']) | ||
span.set_tag(Ext::TAG_JOB_RETRY, job['retry']) | ||
span.set_tag(Ext::TAG_JOB_QUEUE, job['queue']) | ||
span.set_tag(Ext::TAG_JOB_WRAPPER, job['class']) if job['wrapped'] | ||
span.set_tag(Ext::TAG_JOB_DELAY, 1000.0 * (Time.now.utc.to_f - job['enqueued_at'].to_f)) | ||
|
||
yield | ||
end | ||
end | ||
|
||
private | ||
|
||
def service_from_worker_config(resource) | ||
# Try to get the Ruby class from the resource name. | ||
worker_klass = begin | ||
Object.const_get(resource) | ||
rescue NameError | ||
nil | ||
end | ||
|
||
if worker_klass.respond_to?(:datadog_tracer_config) | ||
worker_klass.datadog_tracer_config[:service_name] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,38 @@ | ||
require 'ddtrace/ext/app_types' | ||
require 'ddtrace/contrib/sidekiq/ext' | ||
|
||
module Datadog | ||
module Contrib | ||
module Sidekiq | ||
# Common functionality used by both client-side and server-side tracers. | ||
module Tracing | ||
def initialize(options = {}) | ||
@tracer = options[:tracer] || Datadog.configuration[:sidekiq][:tracer] | ||
end | ||
|
||
protected | ||
|
||
# If class is wrapping something else, the interesting resource info | ||
# is the underlying, wrapped class, and not the wrapper. This is | ||
# primarily to support `ActiveJob`. | ||
def job_resource(job) | ||
if job['wrapped'] | ||
job['wrapped'] | ||
else | ||
job['class'] | ||
end | ||
end | ||
|
||
def set_service_info(service) | ||
# Ensure the tracer knows about this service. | ||
return if @tracer.services[service] | ||
@tracer.set_service_info( | ||
service, | ||
Ext::APP, | ||
Datadog::Ext::AppTypes::WORKER | ||
) | ||
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
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
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,59 @@ | ||
require 'contrib/sidekiq/tracer_test_base' | ||
|
||
class ClientTracerTest < TracerTestBase | ||
class EmptyWorker | ||
include Sidekiq::Worker | ||
|
||
def perform(); end | ||
end | ||
|
||
def setup | ||
super | ||
|
||
Sidekiq.configure_client do |config| | ||
config.client_middleware.clear | ||
|
||
config.client_middleware do |chain| | ||
chain.add(Datadog::Contrib::Sidekiq::ClientTracer, | ||
tracer: @tracer, enabled: true) | ||
end | ||
end | ||
|
||
Sidekiq::Testing.server_middleware.clear | ||
end | ||
|
||
def test_empty | ||
@tracer.trace('parent.span', service: 'parent-service') do | ||
EmptyWorker.perform_async | ||
end | ||
|
||
spans = @writer.spans | ||
assert_equal(2, spans.length) | ||
|
||
parent_span, child_span = spans | ||
|
||
assert_equal('parent.span', parent_span.name) | ||
assert_equal(0, parent_span.status) | ||
assert_nil(parent_span.parent) | ||
|
||
assert_equal('sidekiq-client', child_span.service) | ||
assert_equal('ClientTracerTest::EmptyWorker', child_span.resource) | ||
assert_equal('default', child_span.get_tag('sidekiq.job.queue')) | ||
assert_equal(0, child_span.status) | ||
assert_equal(parent_span, child_span.parent) | ||
end | ||
|
||
def test_empty_parentless | ||
EmptyWorker.perform_async | ||
|
||
spans = @writer.spans | ||
assert_equal(1, spans.length) | ||
|
||
span = spans.first | ||
assert_equal('sidekiq-client', span.service) | ||
assert_equal('ClientTracerTest::EmptyWorker', span.resource) | ||
assert_equal('default', span.get_tag('sidekiq.job.queue')) | ||
assert_equal(0, span.status) | ||
assert_nil(span.parent) | ||
end | ||
end |
Oops, something went wrong.