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

Kafka.js instrumentation causes unhandled rejections

Open Gilbert142 opened this issue 1 year ago • 0 comments

Expected behaviour Using the kafkajs plugin should continue to allow for catching promises safely on the Producer.send method from kafka.js.

Actual behavior Since v4.21.0, dd-trace overwrites the Producer.send method and adds a .then handler on the promise returned by the original send method. However, this .then handler doesn't have an onRejection parameter nor is it followed by a .catch, nor is the resulting promise of the .then call returned at all, making it impossible to catch any rejections of the initial promise. https://github.com/DataDog/dd-trace-js/blob/ef8618193e3d062d20437278682497a6d82f3e08/packages/datadog-instrumentations/src/kafkajs.js#L79-L83 Adding a dummy no-op function (() => {}) as the rejection handler fixes the issue entirely for me (although I'm not sure if we want to actually do something on rejection there instead).

          result.then(res => {
            if (producerCommitCh.hasSubscribers) {
              producerCommitCh.publish(res)
            }
          }, () => {})

Steps to Reproduce Using [email protected] and [email protected], with the kafka.js plugin enabled, simply attempt to produce a message to the kafka cluster which fails without retry. For example, set a maximum message size on the topic and attempt to send a larger message to get a non-retriable KafkaJSProtocolError, which instantly crashes the process (without an unhandledRejection event handler).

Environment

  • Operation system: Ubuntu 20.04.6
  • Node.js version: 16.20.8 (also reproduced on 18.18.2)
  • Tracer version: 4.26.0
  • Relevant library version:
    • kafkajs: 2.2.4

If the proposed solution makes sense to folks I'm happy to open a PR with it.

Gilbert142 avatar Feb 08 '24 15:02 Gilbert142