DataDog/dd-trace-rb

Capture tracing when a specific header is passed in an HTTP request

LeeHester opened this issue · 3 comments

We currently trace at a low sample rate (1%) in our web app. Sometimes, a trace would be really helpful in tracking down a problem for a specific user. I'd like to be able to add a special HTTP header (for example, "FORCE_DATADOG_TRACE" or something) to an HTTP request, have my rails app notice that, and trace that specific request, so that I can use the trace for debugging purposes. I tried doing this with a Rails middleware, like this:

(in config/application.rb)

  ...

  # This middleware checks for an HTTP header named "FORCE_DATADOG_TRACE", and
  # tells DataDog tracing to keep the trace for any request with this present.
  class ForceDataDogTraceMiddleware
    def initialize(app)
      @app = app
    end

    def call(env)
      if env["HTTP_FORCE_DATADOG_TRACE"]
        # Keeps the active trace
        Datadog::Tracing.keep!
      end
      @app.call(env)
    end
  end

  ...

  class Application < Rails::Application
    ...
    config.middleware.use ForceDataDogTraceMiddleware
    ...
  end

I debugged this locally and I see the Datadog::Tracing.keep! line of code is being executed when that header is passed, but I'm not getting those "forced" traces to show up in the DataDog UI. I'm not sure if this is because there is no active trace at the time, or if I'm just going down the wrong path here. Has anyone else tried to accomplish something similar?