[SDTEST-409] Automatically avoid webmock stubs when sending telemetry events #3758
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Configures telemetry transport to use the original ::Net::HTTP client even if it is stubbed by WebMock. We do this by using WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP in place of Net::HTTP (if OriginalNetHTTP is present).
One can see that WebMock stores the original Net::HTTP in this constant here:
https://github.com/bblimke/webmock/blob/fc6a2ab897a069d861adbc1c88e51b2cf8aa88ac/lib/webmock/http_lib_adapters/net_http.rb#L14
See the PR adding the same functionality to the datadog-ci gem:
DataDog/datadog-ci-rb#193
It was released in version 1.1 of datadog-ci.
Motivation:
WebMock is often used when running test suites, but we still need to report library telemetry.
Currently we recommend in our docs making an exception for Datadog endpoints when using datadog-ci gem but this exception is error-prone and leads to a lot of support version. Since v1.1 of datadog-ci we use this approach to send HTTP requests avoiding webmock stubs automatically, without user intervention. This PR adds the same capabilities to the telemetry transport.
How to test the change?
There is a unit with assertion that webmock is not raising even after
WebMock.disable_net_connect!
is called.datadog-ci
gem uses exactly the same approach for WebMock handling:DataDog/datadog-ci-rb#193
Example project where we use test visibility without any custom configuration and webmock is enabled:
https://github.com/DataDog/fast_castle/blob/main/spec/spec_helper.rb#L5
https://github.com/DataDog/fast_castle/pull/6