-
Notifications
You must be signed in to change notification settings - Fork 369
/
instrumentation.rb
90 lines (74 loc) · 2.56 KB
/
instrumentation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'ddtrace/contrib/analytics'
require 'ddtrace/contrib/rake/ext'
module Datadog
module Contrib
module Rake
# Instrumentation for Rake tasks
module Instrumentation
def self.included(base)
base.prepend(InstanceMethods)
end
# Instance methods for Rake instrumentation
module InstanceMethods
def invoke(*args)
return super unless enabled?
tracer.trace(Ext::SPAN_INVOKE, span_options) do |span|
annotate_invoke!(span, args)
super
end
ensure
shutdown_tracer!
end
def execute(args = nil)
return super unless enabled?
tracer.trace(Ext::SPAN_EXECUTE, span_options) do |span|
annotate_execute!(span, args)
super
end
ensure
shutdown_tracer!
end
private
def shutdown_tracer!
tracer.shutdown! if tracer.active_span.nil? && ::Rake.application.top_level_tasks.include?(name)
end
def annotate_invoke!(span, args)
span.resource = name
# Set analytics sample rate
if Contrib::Analytics.enabled?(configuration[:analytics_enabled])
Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate])
end
# Measure service stats
Contrib::Analytics.set_measured(span)
span.set_tag(Ext::TAG_TASK_ARG_NAMES, arg_names)
span.set_tag(Ext::TAG_INVOKE_ARGS, quantize_args(args)) unless args.nil?
rescue StandardError => e
Datadog.logger.debug("Error while tracing Rake invoke: #{e.message}")
end
def annotate_execute!(span, args)
span.resource = name
span.set_tag(Ext::TAG_EXECUTE_ARGS, quantize_args(args.to_hash)) unless args.nil?
rescue StandardError => e
Datadog.logger.debug("Error while tracing Rake execute: #{e.message}")
end
def quantize_args(args)
quantize_options = configuration[:quantize][:args]
Datadog::Quantization::Hash.format(args, quantize_options)
end
def enabled?
configuration[:enabled] == true
end
def tracer
configuration[:tracer]
end
def span_options
{ service: configuration[:service_name] }
end
def configuration
Datadog.configuration[:rake]
end
end
end
end
end
end