Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDTEST-409] Automatically avoid webmock stubs when sending telemetry events #3758

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions 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 All @@ -34,19 +34,17 @@ def open(&block)
end

def post(env)
begin
post = ::Net::HTTP::Post.new(env.path, env.headers)
post.body = env.body

http_response = open do |http|
http.request(post)
end

Response.new(http_response)
rescue StandardError => e
Datadog.logger.debug("Unable to send telemetry event to agent: #{e}")
Telemetry::Http::InternalErrorResponse.new(e)
post = ::Net::HTTP::Post.new(env.path, env.headers)
post.body = env.body

http_response = open do |http|
http.request(post)
end

Response.new(http_response)
rescue StandardError => e
Datadog.logger.debug("Unable to send telemetry event to agent: #{e}")
Telemetry::Http::InternalErrorResponse.new(e)
end

# Data structure for an HTTP Response
Expand Down Expand Up @@ -105,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.allow_net_connect!
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
15 changes: 14 additions & 1 deletion static-analysis.datadog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ schema-version: v1
rulesets:
- ruby-code-style
- ruby-security
- ruby-best-practices
- ruby-best-practices:
rules:
symbols-as-keys:
ignore:
- "**"
hash-fetch:
ignore:
- "**"
percent-w:
ignore:
- spec/**/*
no-optional-hash-params:
ignore:
- "**"
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
Loading