Skip to content

Conversation

itsarijitray
Copy link
Contributor

@itsarijitray itsarijitray commented Sep 18, 2025

This pull request improves error handling and logging for Kafka connection and send operations by extracting and reporting nested Kafka error causes. It also adds new tests to ensure these enhancements work as expected.

Testing

Sample logs on local machine

{"level":"ERROR","timestamp":"2025-09-18T05:56:21.383Z","logger":"kafkajs","message":"[Connection] Connection error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com","broker":"kafka-240f0580-actions-kafka.c.aivencloud.com:27263","clientId":"segment-actions-kafka-producer","stack":"Error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com\n    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26)\n    at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17)"}
{"level":"ERROR","timestamp":"2025-09-18T05:56:21.384Z","logger":"kafkajs","message":"[BrokerPool] Failed to connect to seed broker, trying another broker from the list: Connection error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com","retryCount":0,"retryTime":340}
2025-09-18T05:56:21.385Z - CRIT - integrations-kafka - Kafka Connection Error - KafkaJSConnectionError | {"name":"KafkaJSConnectionError","retriable":true,"broker":"kafka-240f0580-actions-kafka.c.aivencloud.com:27263","code":"ENOTFOUND"} | stack: KafkaJSConnectionError: Connection error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com

Sample Cloudevents Response:

{
  "id": "1234567890",
  "source": "fake-source",
  "specversion": "2.0",
  "type": "com.segment.event.ack",
  "time": "2025-09-18T05:56:21.386Z",
  "status": 207,
  "trace": {
    "name": "invoke",
    "time": "2025-09-18T05:56:21.269Z",
    "duration": "0.117s",
    "logs": [
      {
        "time": "2025-09-18T05:56:21.385Z",
        "key": "CRIT",
        "value": "Kafka Connection Error - KafkaJSConnectionError | {\"name\":\"KafkaJSConnectionError\",\"retriable\":true,\"broker\":\"kafka-240f0580-actions-kafka.c.aivencloud.com:27263\",\"code\":\"ENOTFOUND\"} | stack: KafkaJSConnectionError: Connection error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com\n    at TLSSocket.onError (/Users/array/dev/src/github.com/segmentio/action-destinations/node_modules/kafkajs/src/network/connection.js:210:23)\n    at TLSSocket.emit (node:events:524:28)\n    at TLSSocket.emit (node:domain:489:12)\n    at emitErrorNT (node:internal/streams/destroy:170:8)\n    at emitErrorCloseNT (node:internal/streams/destroy:129:3)\n    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)"
      }
    ],
    "spans": []
  },
  "responses": [
    {
      "status": 500,
      "events": [
        {
          "indices": [],
          "errortype": "KafkaJSConnectionError",
          "errormessage": "Kafka Connection Error - KafkaJSConnectionError: Connection error: getaddrinfo ENOTFOUND kafka-240f0580-actions-kafka.c.aivencloud.com",
          "errorreporter": "INTEGRATIONS"
        }
      ]
    }
  ]
}

Stage Testing

Stage instance: https://app.segment.build/arijit-dev/destinations/actions-kafka/sources/http_api/instances/6880c59b62bcbb1aabdd3887/event-delivery?period=past-hour

The error names (like KafkaJSConnectionError, KafkaJSProtocolError) are now appropriately surfaced instead of being marked KafkaJSNumberOfRetriesExceeded
image

Stage logs:
Screenshot 2025-09-18 at 2 33 17 PM

Datadog stats:
Screenshot 2025-09-18 at 3 15 15 PM

  • Added unit tests for new functionality
  • Tested end-to-end using the local server
  • [If destination is already live] Tested for backward compatibility of destination. Note: New required fields are a breaking change.
  • [Segmenters] Tested in the staging environment
  • [Segmenters] [If applicable for this change] Tested for regression with Hadron.

Copy link

codecov bot commented Sep 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.95%. Comparing base (5ed96b0) to head (3622a51).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3294   +/-   ##
=======================================
  Coverage   79.94%   79.95%           
=======================================
  Files        1202     1202           
  Lines       22069    22076    +7     
  Branches     4337     4337           
=======================================
+ Hits        17644    17651    +7     
  Misses       3644     3644           
  Partials      781      781           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@itsarijitray itsarijitray merged commit 60674bb into main Sep 18, 2025
15 checks passed
@itsarijitray itsarijitray deleted the extract-kafka-errors branch September 18, 2025 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants