-
Notifications
You must be signed in to change notification settings - Fork 369
/
request_patch.rb
93 lines (75 loc) · 3.02 KB
/
request_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
85
86
87
88
89
90
91
92
93
require 'ddtrace/ext/net'
require 'ddtrace/ext/distributed'
require 'ddtrace/ext/integration'
require 'ddtrace/propagation/http_propagator'
require 'ddtrace/contrib/rest_client/ext'
module Datadog
module Contrib
module RestClient
# RestClient RequestPatch
module RequestPatch
def self.included(base)
base.send(:prepend, InstanceMethods)
end
# InstanceMethods - implementing instrumentation
module InstanceMethods
def execute(&block)
uri = URI.parse(url)
return super(&block) unless datadog_configuration[:tracer].enabled
datadog_trace_request(uri) do |span|
if datadog_configuration[:distributed_tracing]
Datadog::HTTPPropagator.inject!(span.context, processed_headers)
end
super(&block)
end
end
def datadog_tag_request(uri, span)
span.resource = method.to_s.upcase
# Tag as an external peer service
span.set_tag(Datadog::Ext::Integration::TAG_PEER_SERVICE, span.service)
# Set analytics sample rate
Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
span.set_tag(Datadog::Ext::HTTP::URL, uri.path)
span.set_tag(Datadog::Ext::HTTP::METHOD, method.to_s.upcase)
span.set_tag(Datadog::Ext::NET::TARGET_HOST, uri.host)
span.set_tag(Datadog::Ext::NET::TARGET_PORT, uri.port)
end
def datadog_trace_request(uri)
span = datadog_configuration[:tracer].trace(Ext::SPAN_REQUEST,
service: datadog_configuration[:service_name],
span_type: Datadog::Ext::HTTP::TYPE_OUTBOUND)
datadog_tag_request(uri, span)
yield(span).tap do |response|
# Verify return value is a response
# If so, add additional tags.
if response.is_a?(::RestClient::Response)
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, response.code)
end
end
rescue ::RestClient::ExceptionWithResponse => e
span.set_error(e) if Datadog::Ext::HTTP::ERROR_RANGE.cover?(e.http_code)
span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, e.http_code)
raise e
# rubocop:disable Lint/RescueException
rescue Exception => e
# rubocop:enable Lint/RescueException
span.set_error(e) if span
raise e
ensure
span.finish if span
end
private
def datadog_configuration
Datadog.configuration[:rest_client]
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