Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
stormsilver committed Dec 27, 2019
1 parent 1cb66f4 commit 4cc04a0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Resolvers
# Matches strings against Regexps.
class RegexpResolver < Datadog::Contrib::Configuration::Resolver
def add_key(pattern)
patterns << pattern.is_a?(Regexp) ? pattern : /#{pattern}/
patterns << pattern.is_a?(Regexp) ? pattern : /#{Regexp.quote(pattern)}/
end

def resolve(name)
Expand Down
14 changes: 9 additions & 5 deletions lib/ddtrace/contrib/ethon/easy_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def datadog_before_request(parent_span: nil)
load_datadog_configuration_for(url)
@datadog_span = datadog_configuration[:tracer].trace(
Ext::SPAN_REQUEST,
service: datadog_configuration[:service_name],
service: uri ? service_name(uri.host, datadog_configuration) : datadog_configuration[:service_name],
span_type: Datadog::Ext::HTTP::TYPE_OUTBOUND
)
@datadog_span.parent = parent_span unless parent_span.nil?
Expand All @@ -98,7 +98,6 @@ def datadog_span_started?

def datadog_tag_request
span = @datadog_span
uri = URI.parse(url)
method = 'N/A'
if instance_variable_defined?(:@datadog_method) && !@datadog_method.nil?
method = @datadog_method.to_s
Expand All @@ -108,12 +107,11 @@ def datadog_tag_request
# Set analytics sample rate
Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?

return unless uri
span.set_tag(Datadog::Ext::HTTP::URL, uri.path)
span.set_tag(Datadog::Ext::HTTP::METHOD, method)
span.set_tag(Datadog::Ext::NET::TARGET_HOST, uri.host)
span.set_tag(Datadog::Ext::NET::TARGET_PORT, uri.port)
rescue URI::InvalidURIError
return
end

def set_span_error_message(message)
Expand All @@ -122,8 +120,14 @@ def set_span_error_message(message)
@datadog_span.set_tag(Datadog::Ext::Errors::MSG, message)
end

def uri
URI.parse(url)
# rubocop:disable Lint/HandleExceptions
rescue URI::InvalidURIError
end

def load_datadog_configuration_for(host = :default)
@datadog_configuration ||= Datadog.configuration[:ethon, host]
@datadog_configuration = Datadog.configuration[:ethon, host]
end

def tracer_enabled?
Expand Down
34 changes: 30 additions & 4 deletions lib/ddtrace/contrib/http/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def request(req, body = nil, &block) # :yield: +response+
begin
# even though service_name might already be in request_options,
# we need to capture the name from the pin since it could be
# overridden. But it might also be the same :)
request_options[:service_name] = datadog_pin.service_name
# overridden
request_options[:service_name] = pin.service_name
span.service = service_name(host, request_options)
span.span_type = Datadog::Ext::HTTP::TYPE_OUTBOUND
span.resource = req.method
Expand Down Expand Up @@ -95,10 +95,36 @@ def set_analytics_sample_rate(span, request_options)
end

def datadog_pin(config = Datadog.configuration[:http])
service = config[:service_name]
tracer = config[:tracer]

@datadog_pin ||= begin
service = config[:service_name]
tracer = config[:tracer]
Datadog::Pin.new(service, app: Ext::APP, app_type: Datadog::Ext::AppTypes::WEB, tracer: tracer)
end

# this shockingly poor code exists to solve the case where someone
# calls datadog_pin on this object before running a request, which
# would cause the :default config to be used. If a request is then
# run for a hostname that matches a different configuration, we
# would use the wrong configs since the pin is memoized.
# The solution is to detect if we are using the default config and
# apply the new config if necessary, while still allowing custom
# values to be supplied
if @datadog_pin.service_name == default_datadog_pin.service_name && @datadog_pin.service_name != service
@datadog_pin.service = service
end
if @datadog_pin.tracer == default_datadog_pin.tracer && @datadog_pin.tracer != tracer
@datadog_pin.tracer = tracer
end

@datadog_pin
end

def default_datadog_pin
config = Datadog.configuration[:http]
service = config[:service_name]
tracer = config[:tracer]
@default_datadog_pin ||= begin
Datadog::Pin.new(service, app: Ext::APP, app_type: Datadog::Ext::AppTypes::WEB, tracer: tracer)
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/ddtrace/contrib/ethon/easy_patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@
expect(easy.instance_eval { @datadog_span }).to be_instance_of(Datadog::Span)
end

context 'when split by domain' do
let(:configuration_options) { super().merge(split_by_domain: true) }

it do
subject
expect(span.name).to eq(Datadog::Contrib::Ethon::Ext::SPAN_REQUEST)
expect(span.service).to eq('example.com')
expect(span.resource).to eq('N/A')
end

context 'and the host matches a specific configuration' do
before do
Datadog.configure do |c|
c.use :ethon, describe: /example\.com/ do |ethon|
ethon.service_name = 'baz'
ethon.split_by_domain = false
end
end
end

it 'uses the configured service name over the domain name' do
subject
expect(span.service).to eq('baz')
end
end
end

it_behaves_like 'span' do
before { subject }
let(:method) { 'N/A' }
Expand Down
25 changes: 0 additions & 25 deletions spec/ddtrace/contrib/ethon/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,5 @@
end
end
end

context 'when split by domain' do
let(:configuration_options) { super().merge(split_by_domain: true) }

it do
expect(span.name).to eq(Datadog::Contrib::Ethon::Ext::SPAN_REQUEST)
expect(span.service).to eq('example.com')
expect(span.resource).to eq('GET')
end

context 'and the host matches a specific configuration' do
before do
Datadog.configure do |c|
c.use :ethon, describe: /example\.com/ do |ethon|
ethon.service_name = 'bar'
ethon.split_by_domain = false
end
end
end

it 'uses the configured service name over the domain name' do
expect(request_span.service).to eq('bar')
end
end
end
end
end

0 comments on commit 4cc04a0

Please sign in to comment.