From 35f95255d88b3efc2f7e4f5d801066d3ccfb8ac8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 10 Apr 2026 07:28:43 +0000 Subject: [PATCH 1/3] fix(node-integration-tests): fix flaky kafkajs test by making transaction order independent The test was flaky because the producer and consumer transactions could arrive in either order, but the test asserted them in a fixed sequence. Now both transactions are collected and assertions are performed after both have been received, regardless of arrival order. Fixes #20121 Co-Authored-By: Claude Agent-Logs-Url: https://github.com/getsentry/sentry-javascript/sessions/0bc166af-c93a-42a6-a762-dae244893e2b Co-authored-by: Lms24 <8420481+Lms24@users.noreply.github.com> --- .../suites/tracing/kafkajs/test.ts | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts index 176d947e1ecf..43d41c103e40 100644 --- a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts @@ -1,3 +1,4 @@ +import type { TransactionEvent } from '@sentry/core'; import { afterAll, describe, expect } from 'vitest'; import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; @@ -8,16 +9,32 @@ describe('kafkajs', () => { createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { test('traces producers and consumers', { timeout: 60_000 }, async () => { + // The producer and consumer transactions can arrive in any order, + // so we collect them and assert after both have been received. + const receivedTransactions: TransactionEvent[] = []; + await createRunner() .withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['9092'], }) .expect({ - transaction: { - transaction: 'send test-topic', - contexts: { - trace: expect.objectContaining({ + transaction: (transaction: TransactionEvent) => { + receivedTransactions.push(transaction); + }, + }) + .expect({ + transaction: (transaction: TransactionEvent) => { + receivedTransactions.push(transaction); + + const producer = receivedTransactions.find(t => t.transaction === 'send test-topic'); + const consumer = receivedTransactions.find(t => t.transaction === 'process test-topic'); + + expect(producer).toBeDefined(); + expect(consumer).toBeDefined(); + + expect(producer!.contexts?.trace).toMatchObject( + expect.objectContaining({ op: 'message', status: 'ok', data: expect.objectContaining({ @@ -28,14 +45,10 @@ describe('kafkajs', () => { 'sentry.origin': 'auto.kafkajs.otel.producer', }), }), - }, - }, - }) - .expect({ - transaction: { - transaction: 'process test-topic', - contexts: { - trace: expect.objectContaining({ + ); + + expect(consumer!.contexts?.trace).toMatchObject( + expect.objectContaining({ op: 'message', status: 'ok', data: expect.objectContaining({ @@ -46,7 +59,7 @@ describe('kafkajs', () => { 'sentry.origin': 'auto.kafkajs.otel.consumer', }), }), - }, + ); }, }) .start() From 282bd6698d8486d38ba6d2d2186db8f2c14f877e Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 10 Apr 2026 11:20:24 +0200 Subject: [PATCH 2/3] refactor ai slop --- .../suites/tracing/kafkajs/test.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts index 43d41c103e40..99fd99541758 100644 --- a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts @@ -27,12 +27,27 @@ describe('kafkajs', () => { transaction: (transaction: TransactionEvent) => { receivedTransactions.push(transaction); - const producer = receivedTransactions.find(t => t.transaction === 'send test-topic'); - const consumer = receivedTransactions.find(t => t.transaction === 'process test-topic'); + const producer = receivedTransactions.find( + t => t.contexts?.trace?.data?.['sentry.origin'] === 'auto.kafkajs.otel.producer', + ); + const consumer = receivedTransactions.find( + t => t.contexts?.trace?.data?.['sentry.origin'] === 'auto.kafkajs.otel.consumer', + ); expect(producer).toBeDefined(); expect(consumer).toBeDefined(); + for (const t of [producer, consumer]) { + // just to assert on the basic shape (for more straight-forward tests, this is usually done by the runner) + expect(t).toMatchObject({ + event_id: expect.any(String), + timestamp: expect.anything(), + start_timestamp: expect.anything(), + spans: expect.any(Array), + type: 'transaction', + }); + } + expect(producer!.contexts?.trace).toMatchObject( expect.objectContaining({ op: 'message', From a1e1287cad9391aed621cdcca6b1f515aa3070a9 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 10 Apr 2026 13:00:16 +0200 Subject: [PATCH 3/3] readd name check --- .../node-integration-tests/suites/tracing/kafkajs/test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts index 99fd99541758..84e8d4a5612e 100644 --- a/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts @@ -48,6 +48,9 @@ describe('kafkajs', () => { }); } + expect(producer!.transaction).toBe('send test-topic'); + expect(consumer!.transaction).toBe('process test-topic'); + expect(producer!.contexts?.trace).toMatchObject( expect.objectContaining({ op: 'message',