Skip to content

Commit

Permalink
Added: Quantization for args fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Apr 26, 2018
1 parent 05c7267 commit 35ad3a5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
41 changes: 24 additions & 17 deletions lib/ddtrace/contrib/rake/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,43 @@ def self.included(base)
# Instance methods for Rake instrumentation
module InstanceMethods
def invoke(*args)
if enabled?
tracer.trace(SPAN_NAME_INVOKE) do |span|
super
annotate!(span)
# TODO: Add quantization
span.set_tag('rake.args', args)
end
else
return super unless enabled?

tracer.trace(SPAN_NAME_INVOKE) do |span|
super
annotate_invoke!(span, args)
end
end

def execute(args = nil)
if enabled?
tracer.trace(SPAN_NAME_EXECUTE) do |span|
super
annotate!(span)
# TODO: Add quantization
span.set_tag('rake.args', args.to_hash) unless args.nil?
end
else
return super unless enabled?

tracer.trace(SPAN_NAME_EXECUTE) do |span|
super
annotate_execute!(span, args)
end
end

private

def annotate!(span)
def annotate_invoke!(span, args)
span.resource = name
span.set_tag('rake.arg_names', arg_names)
span.set_tag('rake.args', quantize(args)) unless args.nil?
rescue StandardError => e
Datadog::Tracer.log.debug("Error while tracing Rake invoke: #{e.message}")
end

def annotate_execute!(span, args)
span.resource = name
span.set_tag('rake.args', quantize(args.to_hash)) unless args.nil?
rescue StandardError => e
Datadog::Tracer.log.debug("Error while tracing Rake execute: #{e.message}")
end

def quantize(args)
quantize_options = Datadog.configuration[:rake][:quantize]
Datadog::Quantization::Hash.format(args, quantize_options)
end

def enabled?
Expand Down
1 change: 1 addition & 0 deletions lib/ddtrace/contrib/rake/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Patcher
option :service_name, default: 'rake'
option :tracer, default: Datadog.tracer
option :enabled, default: true
option :quantize, default: {}

module_function

Expand Down
44 changes: 39 additions & 5 deletions spec/ddtrace/contrib/rake/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,48 @@ def reset_task!(task_name)
let(:task_arg_names) { args_hash.keys }
let(:args) { args_hash.values }

def define_task!
let(:define_task!) do
reset_task!(task_name)
Rake::Task.define_task(task_name, *task_arg_names, &task_body)
end

before(:each) { define_task! }

context 'without args' do
it_behaves_like 'a single task execution'
it_behaves_like 'a single task execution' do
describe '\'rake.invoke\' span tags' do
it do
expect(invoke_span.get_tag('rake.arg_names')).to eq([].to_s)
expect(invoke_span.get_tag('rake.args')).to eq(['?'].to_s)
end
end

describe '\'rake.execute\' span tags' do
it do
expect(execute_span.get_tag('rake.arg_names')).to be nil
expect(execute_span.get_tag('rake.args')).to eq({}.to_s)
end
end
end
end

context 'with args' do
let(:args_hash) { { one: 1, two: 2, three: 3 } }
it_behaves_like 'a single task execution'
it_behaves_like 'a single task execution' do
describe '\'rake.invoke\' span tags' do
it do
expect(invoke_span.get_tag('rake.arg_names')).to eq([:one, :two, :three].to_s)
expect(invoke_span.get_tag('rake.args')).to eq(['?'].to_s)
end
end

describe '\'rake.execute\' span tags' do
it do
expect(execute_span.get_tag('rake.arg_names')).to be nil
expect(execute_span.get_tag('rake.args')).to eq({ one: '?', two: '?', three: '?' }.to_s)
end
end
end
end

context 'with a prerequisite task' do
Expand All @@ -113,7 +141,7 @@ def define_task!
let(:prerequisite_spy) { double('prerequisite spy') }
let(:prerequisite_task) { Rake::Task[prerequisite_task_name] }

def define_task!
let(:define_task!) do
reset_task!(task_name)
reset_task!(prerequisite_task_name)
Rake::Task.define_task(prerequisite_task_name, &prerequisite_task_body)
Expand Down Expand Up @@ -157,6 +185,8 @@ def define_task!
expect(invoke_span.name).to eq(described_class::SPAN_NAME_INVOKE)
expect(invoke_span.resource).to eq(task_name.to_s)
expect(invoke_span.parent_id).to eq(0)
expect(invoke_span.get_tag('rake.arg_names')).to eq([].to_s)
expect(invoke_span.get_tag('rake.args')).to eq(['?'].to_s)
end
end

Expand All @@ -165,6 +195,8 @@ def define_task!
expect(prerequisite_task_execute_span.name).to eq(described_class::SPAN_NAME_EXECUTE)
expect(prerequisite_task_execute_span.resource).to eq(prerequisite_task_name.to_s)
expect(prerequisite_task_execute_span.parent_id).to eq(invoke_span.span_id)
expect(prerequisite_task_execute_span.get_tag('rake.arg_names')).to be nil
expect(prerequisite_task_execute_span.get_tag('rake.args')).to eq({}.to_s)
end
end

Expand All @@ -173,12 +205,14 @@ def define_task!
expect(task_execute_span.name).to eq(described_class::SPAN_NAME_EXECUTE)
expect(task_execute_span.resource).to eq(task_name.to_s)
expect(task_execute_span.parent_id).to eq(invoke_span.span_id)
expect(task_execute_span.get_tag('rake.arg_names')).to be nil
expect(task_execute_span.get_tag('rake.args')).to eq({}.to_s)
end
end
end

context 'defined by a class' do
def define_task!
let(:define_task!) do
reset_task!(task_name)
task_class.new(task_name, *task_arg_names)
end
Expand Down

0 comments on commit 35ad3a5

Please sign in to comment.