dd-trace-rb icon indicating copy to clipboard operation
dd-trace-rb copied to clipboard

How to change trace error to warn on specific errors?

Open khrisnagunanasurya opened this issue 3 years ago • 3 comments

ddtrace version: 1.3.0

I want to change the trace level from error to warning, is it possible? Because currently, I have an error that is expected to be raised when the feature is not on yet. ApplicationConsumer::StopConsume::FlipperToggleNotEnabled which is an error, but I want to change this level to warn instead, and I want to add the error message into the span, so it'll look like this

image

As I can see that there is an option to pass the error handler on Datadog::Tracing.trace But I'm not sure why it's not working at all, and I already tried to search it on Google and the issue here, but I wasn't able to find the solution,

what I have found

  • https://github.com/DataDog/dd-trace-rb/pull/1212

Here is what I tried so far

module DatadogTracerService
  module ConsumerConsume
    def consume
      expected_errors = [
        StandardError
      ]

      options = {
        service: DatadogTracerService::SERVICE_NAME,
        resource: "#{self.class.name}##{__method__}",
        on_error: proc { |span, error| debugger } # I want be able to what is the span and error attribute from rails console
      }

      Datadog::Tracing.trace('consumer.consume', **options) do
        super
      end
    end
  end
end

khrisnagunanasurya avatar Aug 24 '22 09:08 khrisnagunanasurya

👋 @khrisnagunanasurya, our team is currently working on to figure out how to present those warning events instead of a hard error, using reporting N+1 query as an example.

It seems like DatadogTracerService::ConsumerConsume would be prepended to generate datadog span. I think passing on_error option with a proc would be feasible.

An alternative I could have thought of would simply be handling this error in your block.

Datadog::Tracing.trace('consumer.consume', **options) do |span|
  super
rescue FlipperToggleNotEnabled => e
  # Do something
end

Would that be helpful?

TonyCTHsu avatar Aug 29 '22 10:08 TonyCTHsu

Hi @TonyCTHsu , thanks for the quick response, yes correct, i'm prepended the module to consumer classes.

But actually I already do a workaround for this after looking through dd-trace codes, but I forgot to mention it.

Here is my workaround, I hope my workaround is able to help somebody else 😉

module DatadogTracerService
  module ConsumerConsume
    def consume
      options = {
        service: DatadogTracerService::SERVICE_NAME,
        resource: "#{self.class.name}##{__method__}",
        on_error: lambda do |span, error|
          return if span.nil?

          if error.instance_of? ApplicationConsumer::StopConsume::FlipperToggleNotEnabled
            span.name = "#{span.name}.toggle_off"
            span.set_tag :flipper_flag, error.message

            return  # After updating the `span.name` and add a new tag, then return immediately, so it doesn't mark as an error
          end

          span.set_error(error)  # This is the one that mark the trace level as `error`
        end
      }

      Datadog::Tracing.trace('consumer.consume', **options) do
        super
      end
    end
  end
end

khrisnagunanasurya avatar Aug 29 '22 10:08 khrisnagunanasurya

👋 Hi @khrisnagunanasurya, since you already have a workaround for it, and our exploration for solution will take time to mature. Do you think we could close the issue for now?

TonyCTHsu avatar Sep 14 '22 09:09 TonyCTHsu