|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest' |
| 2 | +import { createClient } from 'redis' |
| 3 | +import { createTracer } from '@prsm/trace' |
| 4 | +import Queue from '../src/index.js' |
| 5 | + |
| 6 | +const REDIS = {} |
| 7 | +let admin |
| 8 | + |
| 9 | +async function flush() { |
| 10 | + if (!admin) { |
| 11 | + admin = createClient() |
| 12 | + admin.on('error', () => {}) |
| 13 | + await admin.connect() |
| 14 | + } |
| 15 | + await admin.flushAll() |
| 16 | +} |
| 17 | + |
| 18 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) |
| 19 | + |
| 20 | +const queues = [] |
| 21 | +function make(opts) { |
| 22 | + const q = new Queue({ redisOptions: REDIS, ...opts }) |
| 23 | + queues.push(q) |
| 24 | + return q |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(async () => { await flush() }) |
| 28 | + |
| 29 | +afterEach(async () => { |
| 30 | + while (queues.length) await queues.pop().close().catch(() => {}) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('cross-instance trace propagation', () => { |
| 34 | + it('producer span on one instance, consumer span on another, share traceId', async () => { |
| 35 | + const tracer = createTracer({ service: 'svc' }) |
| 36 | + const spans = [] |
| 37 | + tracer.onSpan((s) => spans.push(s)) |
| 38 | + |
| 39 | + const producer = make({ tracer, concurrency: 0 }) |
| 40 | + const consumer = make({ tracer, concurrency: 2 }) |
| 41 | + |
| 42 | + await producer.ready() |
| 43 | + await consumer.ready() |
| 44 | + |
| 45 | + let processed = null |
| 46 | + consumer.process(async (payload) => { processed = payload; return { ok: true } }) |
| 47 | + |
| 48 | + let pushedTraceId = null |
| 49 | + await tracer.span('http.POST /order', async () => { |
| 50 | + pushedTraceId = tracer.current().traceId |
| 51 | + await producer.push({ orderId: 42 }) |
| 52 | + }) |
| 53 | + |
| 54 | + await sleep(400) |
| 55 | + |
| 56 | + expect(processed).toEqual({ orderId: 42 }) |
| 57 | + |
| 58 | + const pushSpan = spans.find((s) => s.name === 'queue.push') |
| 59 | + const processSpan = spans.find((s) => s.name === 'queue.process') |
| 60 | + expect(pushSpan).toBeTruthy() |
| 61 | + expect(processSpan).toBeTruthy() |
| 62 | + expect(pushSpan.traceId).toBe(pushedTraceId) |
| 63 | + expect(processSpan.traceId).toBe(pushedTraceId) |
| 64 | + expect(processSpan.parentSpanId).toBe(pushSpan.spanId) |
| 65 | + }) |
| 66 | + |
| 67 | + it('producer with no upstream context: push is root, process is its child', async () => { |
| 68 | + const tracer = createTracer({ service: 'svc' }) |
| 69 | + const spans = [] |
| 70 | + tracer.onSpan((s) => spans.push(s)) |
| 71 | + |
| 72 | + const producer = make({ tracer, concurrency: 0 }) |
| 73 | + const consumer = make({ tracer, concurrency: 2 }) |
| 74 | + await producer.ready() |
| 75 | + await consumer.ready() |
| 76 | + consumer.process(async () => 'done') |
| 77 | + |
| 78 | + // push outside any tracer.span — push becomes the trace root |
| 79 | + await producer.push({ x: 1 }) |
| 80 | + await sleep(300) |
| 81 | + |
| 82 | + const pushSpan = spans.find((s) => s.name === 'queue.push') |
| 83 | + const processSpan = spans.find((s) => s.name === 'queue.process') |
| 84 | + expect(pushSpan).toBeTruthy() |
| 85 | + expect(processSpan).toBeTruthy() |
| 86 | + expect(pushSpan.parentSpanId).toBeNull() |
| 87 | + expect(processSpan.parentSpanId).toBe(pushSpan.spanId) |
| 88 | + expect(processSpan.traceId).toBe(pushSpan.traceId) |
| 89 | + }) |
| 90 | + |
| 91 | + it('grouped job carries trace context through the grouped queue path', async () => { |
| 92 | + const tracer = createTracer({ service: 'svc' }) |
| 93 | + const spans = [] |
| 94 | + tracer.onSpan((s) => spans.push(s)) |
| 95 | + |
| 96 | + const producer = make({ tracer, concurrency: 0 }) |
| 97 | + const consumer = make({ tracer, groups: { concurrency: 1 } }) |
| 98 | + await producer.ready() |
| 99 | + await consumer.ready() |
| 100 | + consumer.process(async () => 'done') |
| 101 | + |
| 102 | + let pushedTraceId = null |
| 103 | + await tracer.span('grouped-entry', async () => { |
| 104 | + pushedTraceId = tracer.current().traceId |
| 105 | + await producer.push({ p: 1 }, { group: 'tenant-a' }) |
| 106 | + }) |
| 107 | + |
| 108 | + await sleep(400) |
| 109 | + |
| 110 | + const pushSpan = spans.find((s) => s.name === 'queue.push') |
| 111 | + const processSpan = spans.find((s) => s.name === 'queue.process') |
| 112 | + expect(pushSpan.traceId).toBe(pushedTraceId) |
| 113 | + expect(processSpan.traceId).toBe(pushedTraceId) |
| 114 | + }) |
| 115 | +}) |
0 commit comments