Skip to content

Commit

Permalink
automatically avoid WebMock stubs when sending telemetry events
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 4, 2024
1 parent 3a9e378 commit e6add8c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
10 changes: 9 additions & 1 deletion lib/datadog/core/telemetry/http/adapters/net.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(hostname:, port: nil, timeout: DEFAULT_TIMEOUT, ssl: true)
end

def open(&block)
req = ::Net::HTTP.new(@hostname, @port)
req = net_http_client.new(@hostname, @port)

req.use_ssl = @ssl
req.open_timeout = req.read_timeout = @timeout
Expand Down Expand Up @@ -103,6 +103,14 @@ def inspect
"#{super}, http_response:#{http_response}"
end
end

private

def net_http_client
return ::Net::HTTP unless defined?(WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP)

WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/core/telemetry/http/adapters/net.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ module Datadog

def inspect: () -> ::String
end

private

def net_http_client: () -> singleton(::Net::HTTP)
end
end
end
Expand Down
58 changes: 46 additions & 12 deletions spec/datadog/core/telemetry/http/adapters/net_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
let(:http_connection) { instance_double(::Net::HTTP) }

before do
allow(::Net::HTTP).to receive(:new)
# webmock stores real ::Net::HTTP in this constant
allow(::WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP).to receive(:new)
.with(
adapter.hostname,
adapter.port,
Expand Down Expand Up @@ -90,25 +91,58 @@
end

describe '#post' do
include_context 'HTTP connection stub'
include_context 'HTTP Env'

subject(:post) { adapter.post(env) }
context 'with stubbed Net::HTTP' do
include_context 'HTTP connection stub'

subject(:post) { adapter.post(env) }

let(:http_response) { double('http_response') }

let(:http_response) { double('http_response') }
context 'when request goes through' do
before { expect(http_connection).to receive(:request).and_return(http_response) }

context 'when request goes through' do
before { expect(http_connection).to receive(:request).and_return(http_response) }
it 'produces a response' do
is_expected.to be_a_kind_of(described_class::Response)
expect(post.http_response).to be(http_response)
end
end

it 'produces a response' do
is_expected.to be_a_kind_of(described_class::Response)
expect(post.http_response).to be(http_response)
context 'when error in connecting to agent' do
before { expect(http_connection).to receive(:request).and_raise(StandardError) }
it { expect(post).to be_a_kind_of(Datadog::Core::Telemetry::Http::InternalErrorResponse) }
end
end

context 'when error in connecting to agent' do
before { expect(http_connection).to receive(:request).and_raise(StandardError) }
it { expect(post).to be_a_kind_of(Datadog::Core::Telemetry::Http::InternalErrorResponse) }
context 'with real Net::HTTP' do
subject(:post) { adapter.post(env) }

let(:hostname) { 'hostname_that_doesnt_exist' }
let(:port) { 4444 }
let(:timeout) { 1 }
let(:expected_error) do
# Socket::ResolutionError is for Ruby 3.3+
if defined?(Socket::ResolutionError)
Socket::ResolutionError
else
SocketError
end
end

before do
WebMock.disable_net_connect!
end

after do
WebMock.disable!
end

it 'makes real HTTP request and fails' do
response = post
expect(response).to be_a_kind_of(Datadog::Core::Telemetry::Http::InternalErrorResponse)
expect(response.error).to be_a_kind_of(expected_error)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions vendor/rbs/webmock/0/webmock.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module WebMock
def self.enable!: -> void

def self.disable!: -> void

OriginalNetHTTP: singleton(::Net::HTTP)
end
end
end

0 comments on commit e6add8c

Please sign in to comment.