Dd-trace-rb: Mock HTTPTransport in dev/test

Created on 26 Oct 2018  路  4Comments  路  Source: DataDog/dd-trace-rb

We keep ddtrace gem preloaded for all environments including _development_ and also _test_ for the regression detection purpose. The gem configuration is different though, we configure Datadog.configure only for _staging_ and _production_, but still see the following messages in the other environments:

ddtrace: [ddtrace] (/bundle/gems/ddtrace-0.13.2/lib/ddtrace/transport.rb:112:in `rescue in post') Connection refused - connect(2) for "127.0.0.1" port 8126
ddtrace: [ddtrace] (/bundle/gems/ddtrace-0.13.2/lib/ddtrace/transport.rb:112:in `rescue in post') Connection refused - connect(2) for "127.0.0.1" port 8126

While we still want to keep ddtrace gem preloaded, HTTP POST requests are excessive, because we neither have ddtrace in CI nor in _development_. I doubt this is a common practice to run datadog agent in _development_. Workaround is the following:

if Rails.env.development? || Rails.env.test?
  Datadog::HTTPTransport.class_eval do
    def post(url, data, count = nil)
      200
    end
  end
end

By default we wait for 1 second to connect, that time can be saved per every request.

So the question is there any native way to disable HTTP transport apart from monkey-patching? cc @irabinovitch

community feature-request

Most helpful comment

@mikhailov @billthompson Hey folks, just wanted to update that we implemented a test adapter for the transport during its recent overhaul.

In test environments, you can activate test transport adapter using the following:

Datadog.configure do |c|
  c.tracer transport_options: proc { |t| t.adapter :test }
end

You can read more details about it in our documentation. I'm going to close this for now, but let me know how this works for you, or if there are outstanding issues remaining, and I'll be happy to revisit them with you.

All 4 comments

Hey @mikhailov , this is definitely something on our list: to improve the tooling aspect, and how it fits in with test suites. We have a mock transport that we use in our own test suite, but we haven't made it directly available through the gem itself. We do plan to change that, and expose a set of mock/test components that can be configured into test suites so the trace code can be used, and even asserted upon, as a part of application tests.

In the interim, what I can offer you is that you can reconfigure the tracer with your own custom writer (or reconfigure the default writer with your own custom transport) via:

Datadog.configure { |c| c.tracer instance: Datadog::Tracer.new(writer: FauxWriter.new) }

Then you could implement (or copy) a fake Writer or Transport from the following in our test suite:

https://github.com/DataDog/dd-trace-rb/blob/master/spec/support/faux_writer.rb
https://github.com/DataDog/dd-trace-rb/blob/master/spec/support/faux_transport.rb

My hope is eventually this code will be migrated out of the test suite and into a test module that can be optionally included and composed into the test suite.

Does this help?

Just an FYI, I started working on the groundwork to make this possible. I opened this #628 which refactors how our Transport works, so as to make it much easier to implement a mock transport to capture output with.

Might still be a while till this is available, but if you have any thoughts about how that helps this issue, feel free to share them.

Update: The solution below stopped working when we added the Rails integration, so we went back to a conditional configuration in the initializer.


For v0.22.0, the solution in https://github.com/DataDog/dd-trace-rb/issues/599#issuecomment-433936577 didn't work for me, but this did:

if Rails.env.development? || Rails.env.test?
  Datadog.configuration.tracer = Datadog::Tracer.new(enabled: false, writer: nil)
else
  # normal config
end

@mikhailov @billthompson Hey folks, just wanted to update that we implemented a test adapter for the transport during its recent overhaul.

In test environments, you can activate test transport adapter using the following:

Datadog.configure do |c|
  c.tracer transport_options: proc { |t| t.adapter :test }
end

You can read more details about it in our documentation. I'm going to close this for now, but let me know how this works for you, or if there are outstanding issues remaining, and I'll be happy to revisit them with you.

Was this page helpful?
0 / 5 - 0 ratings