-
Notifications
You must be signed in to change notification settings - Fork 369
/
multi_patch.rb
84 lines (70 loc) · 2.49 KB
/
multi_patch.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
require 'ddtrace/ext/net'
require 'ddtrace/ext/distributed'
require 'ddtrace/ext/integration'
require 'ddtrace/propagation/http_propagator'
require 'ddtrace/contrib/ethon/ext'
module Datadog
module Contrib
module Ethon
# Ethon MultiPatch
module MultiPatch
def self.included(base)
# No need to prepend here since add method is included into Multi class
base.send(:include, InstanceMethods)
end
# InstanceMethods - implementing instrumentation
module InstanceMethods
def add(easy)
handles = super
return handles if handles.nil? || !tracer_enabled?
if datadog_multi_performing?
# Start Easy span in case Multi is already performing
easy.datadog_before_request(parent_span: datadog_multi_span)
end
handles
end
def perform
if tracer_enabled?
easy_handles.each do |easy|
easy.datadog_before_request(parent_span: datadog_multi_span) unless easy.datadog_span_started?
end
end
super
ensure
if tracer_enabled? && datadog_multi_performing?
@datadog_multi_span.finish
@datadog_multi_span = nil
end
end
private
def datadog_multi_performing?
instance_variable_defined?(:@datadog_multi_span) && !@datadog_multi_span.nil?
end
def datadog_multi_span
@datadog_multi_span ||= datadog_configuration[:tracer].trace(
Ext::SPAN_MULTI_REQUEST,
service: datadog_configuration[:service_name]
)
# Tag as an external peer service
@datadog_multi_span.set_tag(Datadog::Ext::Integration::TAG_PEER_SERVICE, @datadog_multi_span.service)
# Set analytics sample rate
Contrib::Analytics.set_sample_rate(@datadog_multi_span, analytics_sample_rate) if analytics_enabled?
@datadog_multi_span
end
def datadog_configuration
Datadog.configuration[:ethon]
end
def tracer_enabled?
datadog_configuration[:tracer].enabled
end
def analytics_enabled?
Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
end
def analytics_sample_rate
datadog_configuration[:analytics_sample_rate]
end
end
end
end
end
end