-
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 #409 from DataDog/feature/add_rake_instrumentation
Add Rake instrumentation
- Loading branch information
Showing
11 changed files
with
501 additions
and
3 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
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,70 @@ | ||
module Datadog | ||
module Contrib | ||
module Rake | ||
# Instrumentation for Rake tasks | ||
module Instrumentation | ||
SPAN_NAME_INVOKE = 'rake.invoke'.freeze | ||
SPAN_NAME_EXECUTE = 'rake.execute'.freeze | ||
|
||
def self.included(base) | ||
base.send(:prepend, InstanceMethods) | ||
end | ||
|
||
# Instance methods for Rake instrumentation | ||
module InstanceMethods | ||
def invoke(*args) | ||
return super unless enabled? | ||
|
||
tracer.trace(SPAN_NAME_INVOKE) do |span| | ||
super | ||
annotate_invoke!(span, args) | ||
end | ||
end | ||
|
||
def execute(args = nil) | ||
return super unless enabled? | ||
|
||
tracer.trace(SPAN_NAME_EXECUTE) do |span| | ||
super | ||
annotate_execute!(span, args) | ||
end | ||
end | ||
|
||
private | ||
|
||
def annotate_invoke!(span, args) | ||
span.resource = name | ||
span.set_tag('rake.task.arg_names', arg_names) | ||
span.set_tag('rake.invoke.args', quantize_args(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.execute.args', quantize_args(args.to_hash)) unless args.nil? | ||
rescue StandardError => e | ||
Datadog::Tracer.log.debug("Error while tracing Rake execute: #{e.message}") | ||
end | ||
|
||
def quantize_args(args) | ||
quantize_options = Datadog.configuration[:rake][:quantize][:args] | ||
Datadog::Quantization::Hash.format(args, quantize_options) | ||
end | ||
|
||
def enabled? | ||
configuration[:enabled] == true | ||
end | ||
|
||
def tracer | ||
configuration[:tracer] | ||
end | ||
|
||
def configuration | ||
Datadog.configuration[:rake] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'ddtrace/ext/app_types' | ||
require 'ddtrace/contrib/rake/instrumentation' | ||
|
||
module Datadog | ||
module Contrib | ||
module Rake | ||
# Patcher for Rake instrumentation | ||
module Patcher | ||
include Base | ||
|
||
register_as :rake | ||
option :service_name, default: 'rake' | ||
option :tracer, default: Datadog.tracer | ||
option :enabled, default: true | ||
option :quantize, default: {} | ||
|
||
module_function | ||
|
||
def patch | ||
return patched? if patched? || !compatible? | ||
|
||
patch_rake | ||
|
||
# Set service info | ||
configuration[:tracer].set_service_info( | ||
configuration[:service_name], | ||
'rake', | ||
Ext::AppTypes::WORKER | ||
) | ||
|
||
@patched = true | ||
end | ||
|
||
def patched? | ||
return @patched if defined?(@patched) | ||
@patched = false | ||
end | ||
|
||
def patch_rake | ||
::Rake::Task.send(:include, Instrumentation) | ||
end | ||
|
||
def compatible? | ||
RUBY_VERSION >= '2.0.0' && defined?(::Rake) | ||
end | ||
|
||
def configuration | ||
Datadog.configuration[:rake] | ||
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
Oops, something went wrong.