Skip to content

Commit

Permalink
Move url building behavior from AgentBaseUrl to AgentSettings
Browse files Browse the repository at this point in the history
This is preparation to also share this behavior with profiling.
  • Loading branch information
ivoanjo committed Jan 2, 2025
1 parent b595d41 commit 6ada623
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 33 deletions.
18 changes: 18 additions & 0 deletions lib/datadog/core/configuration/agent_settings_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,26 @@ def initialize(adapter: nil, ssl: nil, hostname: nil, port: nil, uds_path: nil,
@timeout_seconds = timeout_seconds
freeze
end

def url
case adapter
when Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
hostname = self.hostname
hostname = "[#{hostname}]" if hostname =~ IPV6_REGEXP
"#{ssl ? 'https' : 'http'}://#{hostname}:#{port}/"
when Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
"unix://#{uds_path}"
else
raise ArgumentError, "Unexpected adapter: #{adapter}"
end
end
end

# IPv6 regular expression from
# https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
# Does not match IPv4 addresses.
IPV6_REGEXP = /\A(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\z)/.freeze # rubocop:disable Layout/LineLength

def self.call(settings, logger: Datadog.logger)
new(settings, logger: logger).send(:call)
end
Expand Down
14 changes: 1 addition & 13 deletions lib/datadog/core/crashtracking/agent_base_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,8 @@ module Core
module Crashtracking
# This module provides a method to resolve the base URL of the agent
module AgentBaseUrl
# IPv6 regular expression from
# https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
# Does not match IPv4 addresses.
IPV6_REGEXP = /\A(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\z)/.freeze # rubocop:disable Layout/LineLength

def self.resolve(agent_settings)
case agent_settings.adapter
when Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER
hostname = agent_settings.hostname
hostname = "[#{hostname}]" if hostname =~ IPV6_REGEXP
"#{agent_settings.ssl ? 'https' : 'http'}://#{hostname}:#{agent_settings.port}/"
when Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER
"unix://#{agent_settings.uds_path}"
end
agent_settings.url
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions sig/datadog/core/configuration/agent_settings_resolver.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Datadog
attr_reader port: untyped
attr_reader uds_path: untyped
attr_reader timeout_seconds: untyped

def url: () -> ::String
end

@settings: untyped
Expand All @@ -24,6 +26,11 @@ module Datadog
@mixed_http_and_uds: untyped
@parsed_url: untyped

# IPv6 regular expression from
# https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
# Does not match IPv4 addresses.
IPV6_REGEXP: ::Regexp

def self.call: (untyped settings, ?logger: untyped) -> untyped

private
Expand Down
57 changes: 57 additions & 0 deletions spec/datadog/core/configuration/agent_settings_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,61 @@
end
end
end

describe 'url' do
context 'when using HTTP adapter' do
before do
datadog_settings.agent.host = 'example.com'
datadog_settings.agent.port = 8080
end

context 'when SSL is enabled' do
before { datadog_settings.agent.use_ssl = true }

it 'returns the correct base URL' do
expect(resolver.url).to eq('https://example.com:8080/')
end
end

context 'when SSL is disabled' do
before { datadog_settings.agent.use_ssl = false }

it 'returns the correct base URL' do
expect(resolver.url).to eq('http://example.com:8080/')
end
end

context 'when hostname is an IPv4 address' do
before { datadog_settings.agent.host = '1.2.3.4' }

it 'returns the correct base URL' do
expect(resolver.url).to eq('http://1.2.3.4:8080/')
end
end

context 'when hostname is an IPv6 address' do
before { datadog_settings.agent.host = '1234:1234::1' }

it 'returns the correct base URL' do
expect(resolver.url).to eq('http://[1234:1234::1]:8080/')
end
end
end

context 'when using UnixSocket adapter' do
before { datadog_settings.agent.uds_path = '/var/run/datadog.sock' }

it 'returns the correct base URL' do
expect(resolver.url).to eq('unix:///var/run/datadog.sock')
end
end

context 'when using an unknown adapter' do
it 'raises an exception' do
agent_settings = Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(adapter: :unknown)

expect { agent_settings.url }.to raise_error(ArgumentError, /Unexpected adapter/)
end
end
end
end
25 changes: 5 additions & 20 deletions spec/datadog/core/crashtracking/agent_base_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
context 'when using HTTP adapter' do
context 'when SSL is enabled' do
let(:agent_settings) do
instance_double(
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings,
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: true,
hostname: 'example.com',
Expand All @@ -24,8 +23,7 @@

context 'when SSL is disabled' do
let(:agent_settings) do
instance_double(
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings,
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: false,
hostname: 'example.com',
Expand All @@ -40,8 +38,7 @@

context 'when hostname is an IPv4 address' do
let(:agent_settings) do
instance_double(
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings,
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: false,
hostname: '1.2.3.4',
Expand All @@ -56,8 +53,7 @@

context 'when hostname is an IPv6 address' do
let(:agent_settings) do
instance_double(
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings,
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(
adapter: Datadog::Core::Configuration::Ext::Agent::HTTP::ADAPTER,
ssl: false,
hostname: '1234:1234::1',
Expand All @@ -73,8 +69,7 @@

context 'when using UnixSocket adapter' do
let(:agent_settings) do
instance_double(
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings,
Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings.new(
adapter: Datadog::Core::Configuration::Ext::Agent::UnixSocket::ADAPTER,
uds_path: '/var/run/datadog.sock'
)
Expand All @@ -84,15 +79,5 @@
expect(described_class.resolve(agent_settings)).to eq('unix:///var/run/datadog.sock')
end
end

context 'when using unknownm adapter' do
let(:agent_settings) do
instance_double(Datadog::Core::Configuration::AgentSettingsResolver::AgentSettings, adapter: 'unknown')
end

it 'returns nil' do
expect(described_class.resolve(agent_settings)).to be_nil
end
end
end
end

0 comments on commit 6ada623

Please sign in to comment.