autotel 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/package.json +1 -2
  2. package/src/attribute-redacting-processor.test.ts +0 -763
  3. package/src/attribute-redacting-processor.ts +0 -621
  4. package/src/attributes/attachers.ts +0 -161
  5. package/src/attributes/builders.ts +0 -529
  6. package/src/attributes/domains.ts +0 -42
  7. package/src/attributes/index.ts +0 -81
  8. package/src/attributes/registry.ts +0 -323
  9. package/src/attributes/types.ts +0 -211
  10. package/src/attributes/utils.ts +0 -64
  11. package/src/attributes/validators.ts +0 -266
  12. package/src/attributes.test.ts +0 -292
  13. package/src/auto.ts +0 -67
  14. package/src/autotel-logger.test.ts +0 -548
  15. package/src/autotel-logger.ts +0 -364
  16. package/src/baggage-span-processor.test.ts +0 -202
  17. package/src/baggage-span-processor.ts +0 -100
  18. package/src/business-baggage.test.ts +0 -500
  19. package/src/business-baggage.ts +0 -669
  20. package/src/circuit-breaker.test.ts +0 -341
  21. package/src/circuit-breaker.ts +0 -184
  22. package/src/config.test.ts +0 -94
  23. package/src/config.ts +0 -172
  24. package/src/correlated-events.test.ts +0 -151
  25. package/src/correlated-events.ts +0 -47
  26. package/src/correlation-id.test.ts +0 -163
  27. package/src/correlation-id.ts +0 -206
  28. package/src/db.test.ts +0 -252
  29. package/src/db.ts +0 -447
  30. package/src/decorators.test.ts +0 -153
  31. package/src/decorators.ts +0 -188
  32. package/src/define-event.test.ts +0 -41
  33. package/src/define-event.ts +0 -58
  34. package/src/devtools.ts +0 -60
  35. package/src/drain-pipeline.test.ts +0 -68
  36. package/src/drain-pipeline.ts +0 -199
  37. package/src/drain-toolkit.test.ts +0 -113
  38. package/src/drain-toolkit.ts +0 -129
  39. package/src/enricher-toolkit.test.ts +0 -67
  40. package/src/enricher-toolkit.ts +0 -79
  41. package/src/enrichers.test.ts +0 -150
  42. package/src/enrichers.ts +0 -145
  43. package/src/env-config.test.ts +0 -323
  44. package/src/env-config.ts +0 -309
  45. package/src/error-catalog.test.ts +0 -133
  46. package/src/error-catalog.ts +0 -262
  47. package/src/event-queue.test.ts +0 -864
  48. package/src/event-queue.ts +0 -699
  49. package/src/event-subscriber.ts +0 -262
  50. package/src/event-testing.ts +0 -197
  51. package/src/event.test.ts +0 -1104
  52. package/src/event.ts +0 -988
  53. package/src/events-config.ts +0 -235
  54. package/src/exporters.ts +0 -165
  55. package/src/filtering-span-processor.test.ts +0 -281
  56. package/src/filtering-span-processor.ts +0 -111
  57. package/src/flatten-attributes.test.ts +0 -76
  58. package/src/flatten-attributes.ts +0 -80
  59. package/src/functional.strict-types.typecheck.ts +0 -53
  60. package/src/functional.test.ts +0 -1464
  61. package/src/functional.ts +0 -2539
  62. package/src/functional.types.test.ts +0 -135
  63. package/src/hook.mjs +0 -15
  64. package/src/http.test.ts +0 -485
  65. package/src/http.ts +0 -424
  66. package/src/index.ts +0 -433
  67. package/src/init-auto-redactor.test.ts +0 -53
  68. package/src/init-redactor.test.ts +0 -8
  69. package/src/init.customization.test.ts +0 -665
  70. package/src/init.integrations.test.ts +0 -399
  71. package/src/init.openllmetry.test.ts +0 -194
  72. package/src/init.protocol.test.ts +0 -215
  73. package/src/init.ts +0 -2439
  74. package/src/instrumentation.test.ts +0 -108
  75. package/src/instrumentation.ts +0 -319
  76. package/src/logger.test.ts +0 -125
  77. package/src/logger.ts +0 -341
  78. package/src/messaging-adapters.test.ts +0 -595
  79. package/src/messaging-adapters.ts +0 -583
  80. package/src/messaging-testing.test.ts +0 -573
  81. package/src/messaging-testing.ts +0 -935
  82. package/src/messaging.test.ts +0 -1646
  83. package/src/messaging.ts +0 -2245
  84. package/src/metric-helpers.ts +0 -47
  85. package/src/metric-testing.ts +0 -197
  86. package/src/metric.ts +0 -446
  87. package/src/metrics.test.ts +0 -241
  88. package/src/node-require.ts +0 -123
  89. package/src/operation-context.ts +0 -93
  90. package/src/parse-error.test.ts +0 -73
  91. package/src/parse-error.ts +0 -112
  92. package/src/posthog-logs.test.ts +0 -115
  93. package/src/posthog-logs.ts +0 -77
  94. package/src/pretty-console-exporter.test.ts +0 -545
  95. package/src/pretty-console-exporter.ts +0 -413
  96. package/src/pretty-log-formatter.test.ts +0 -123
  97. package/src/pretty-log-formatter.ts +0 -210
  98. package/src/processors/canonical-log-line-processor.test.ts +0 -523
  99. package/src/processors/canonical-log-line-processor.ts +0 -396
  100. package/src/processors.ts +0 -152
  101. package/src/rate-limiter.test.ts +0 -199
  102. package/src/rate-limiter.ts +0 -98
  103. package/src/redact-values.test.ts +0 -90
  104. package/src/redact-values.ts +0 -34
  105. package/src/register.ts +0 -37
  106. package/src/request-logger.test.ts +0 -545
  107. package/src/request-logger.ts +0 -342
  108. package/src/sampling.test.ts +0 -1060
  109. package/src/sampling.ts +0 -737
  110. package/src/security-schema.test.ts +0 -45
  111. package/src/security-schema.ts +0 -107
  112. package/src/semantic-conventions.ts +0 -15
  113. package/src/semantic-helpers.test.ts +0 -226
  114. package/src/semantic-helpers.ts +0 -438
  115. package/src/shutdown.test.ts +0 -364
  116. package/src/shutdown.ts +0 -246
  117. package/src/span-name-normalizer.test.ts +0 -377
  118. package/src/span-name-normalizer.ts +0 -213
  119. package/src/stable-hash.ts +0 -27
  120. package/src/structured-error.test.ts +0 -191
  121. package/src/structured-error.ts +0 -157
  122. package/src/stub.integration.test.ts +0 -361
  123. package/src/tail-sampling-processor.test.ts +0 -230
  124. package/src/tail-sampling-processor.ts +0 -55
  125. package/src/test-span-collector.test.ts +0 -234
  126. package/src/test-span-collector.ts +0 -150
  127. package/src/testing.ts +0 -705
  128. package/src/trace-context.test.ts +0 -73
  129. package/src/trace-context.ts +0 -567
  130. package/src/trace-helpers.new.test.ts +0 -278
  131. package/src/trace-helpers.test.ts +0 -290
  132. package/src/trace-helpers.ts +0 -710
  133. package/src/trace-hybrid.test.ts +0 -42
  134. package/src/trace-hybrid.ts +0 -37
  135. package/src/tracer-provider.test.ts +0 -183
  136. package/src/tracer-provider.ts +0 -266
  137. package/src/track.test.ts +0 -154
  138. package/src/track.ts +0 -216
  139. package/src/validate.test.ts +0 -287
  140. package/src/validate.ts +0 -307
  141. package/src/validation-attributes.ts +0 -43
  142. package/src/validation.test.ts +0 -330
  143. package/src/validation.ts +0 -246
  144. package/src/variable-name-inference.test.ts +0 -178
  145. package/src/variable-name-inference.ts +0 -242
  146. package/src/webhook.test.ts +0 -649
  147. package/src/webhook.ts +0 -637
  148. package/src/workflow-distributed.test.ts +0 -786
  149. package/src/workflow-distributed.ts +0 -916
  150. package/src/workflow.async-safety.integration.test.ts +0 -345
  151. package/src/workflow.test.ts +0 -647
  152. package/src/workflow.ts +0 -810
  153. package/src/yaml-config.test.ts +0 -373
  154. package/src/yaml-config.ts +0 -351
@@ -1,1646 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import {
3
- traceProducer,
4
- traceConsumer,
5
- clearOrderingState,
6
- type ProducerConfig,
7
- type ConsumerConfig,
8
- type DLQOptions,
9
- type DLQReplayOptions,
10
- type DLQReasonCategory,
11
- type OrderingConfig,
12
- type OutOfOrderInfo,
13
- type ConsumerGroupTrackingConfig,
14
- type RebalanceEvent,
15
- type PartitionAssignment,
16
- type ConsumerGroupState,
17
- type PartitionLag,
18
- type RebalanceType,
19
- } from './messaging';
20
-
21
- // Mock the trace function to capture span options
22
- vi.mock('./functional', () => ({
23
- trace: vi.fn((name, factory) => {
24
- return (...args: unknown[]) => {
25
- const mockCtx = createMockContext();
26
- const fn = factory(mockCtx);
27
- return fn(...args);
28
- };
29
- }),
30
- }));
31
-
32
- // Mock sampling functions
33
- vi.mock('./sampling', () => ({
34
- createLinkFromHeaders: vi.fn((headers) => {
35
- if (headers.traceparent) {
36
- return {
37
- context: {
38
- traceId: '00000000000000000000000000000001',
39
- spanId: '0000000000000001',
40
- traceFlags: 1,
41
- },
42
- attributes: {},
43
- };
44
- }
45
- return null;
46
- }),
47
- extractLinksFromBatch: vi.fn((messages) => {
48
- return messages
49
- .filter(
50
- (m: { headers?: Record<string, string> }) => m.headers?.traceparent,
51
- )
52
- .map(() => ({
53
- context: {
54
- traceId: '00000000000000000000000000000001',
55
- spanId: '0000000000000001',
56
- traceFlags: 1,
57
- },
58
- attributes: {},
59
- }));
60
- }),
61
- }));
62
-
63
- function createMockContext() {
64
- const attributes: Record<string, unknown> = {};
65
- const events: Array<{ name: string; attributes?: Record<string, unknown> }> =
66
- [];
67
- const links: unknown[] = [];
68
-
69
- return {
70
- setAttribute: vi.fn((key, value) => {
71
- attributes[key] = value;
72
- }),
73
- setAttributes: vi.fn((attrs) => {
74
- Object.assign(attributes, attrs);
75
- }),
76
- addEvent: vi.fn((name, attrs) => {
77
- events.push({ name, attributes: attrs });
78
- }),
79
- addLink: vi.fn((link) => {
80
- links.push(link);
81
- }),
82
- addLinks: vi.fn((newLinks) => {
83
- links.push(...newLinks);
84
- }),
85
- setStatus: vi.fn(),
86
- getAttributes: () => attributes,
87
- getEvents: () => events,
88
- getLinks: () => links,
89
- // ProducerContext methods
90
- getTraceHeaders: vi.fn(() => ({
91
- traceparent: '00-00000000000000000000000000000001-0000000000000001-01',
92
- })),
93
- getAllPropagationHeaders: vi.fn(() => ({
94
- traceparent: '00-00000000000000000000000000000001-0000000000000001-01',
95
- baggage: 'key=value',
96
- })),
97
- // ConsumerContext methods
98
- recordDLQ: vi.fn(),
99
- recordRetry: vi.fn(),
100
- recordReplay: vi.fn(),
101
- getProducerLinks: vi.fn(() => []),
102
- };
103
- }
104
-
105
- describe('Messaging Helpers', () => {
106
- beforeEach(() => {
107
- vi.clearAllMocks();
108
- });
109
-
110
- describe('traceProducer', () => {
111
- it('should create a producer function with correct span name', async () => {
112
- const config: ProducerConfig = {
113
- system: 'kafka',
114
- destination: 'user-events',
115
- };
116
-
117
- const producer = traceProducer(config)(
118
- (_ctx) => async (_event: { id: string }) => {
119
- return { sent: true };
120
- },
121
- );
122
-
123
- const result = await producer({ id: 'event-1' });
124
- expect(result).toEqual({ sent: true });
125
- });
126
-
127
- it('should set messaging semantic attributes', async () => {
128
- const config: ProducerConfig = {
129
- system: 'kafka',
130
- destination: 'user-events',
131
- messageIdFrom: (args) => (args[0] as { id: string }).id,
132
- };
133
-
134
- // We need to directly test the attribute setting
135
- // Since the trace mock doesn't persist context properly,
136
- // we test the config structure
137
- expect(config.system).toBe('kafka');
138
- expect(config.destination).toBe('user-events');
139
- });
140
-
141
- it('should support SQS system type', () => {
142
- const config: ProducerConfig = {
143
- system: 'sqs',
144
- destination: 'orders-queue',
145
- };
146
-
147
- const producer = traceProducer(config)(
148
- (_ctx) => async (_message: string) => {
149
- return { messageId: '123' };
150
- },
151
- );
152
-
153
- expect(producer).toBeDefined();
154
- });
155
-
156
- it('should support RabbitMQ system type', () => {
157
- const config: ProducerConfig = {
158
- system: 'rabbitmq',
159
- destination: 'notifications',
160
- };
161
-
162
- const producer = traceProducer(config)(
163
- (_ctx) => async (_message: string) => {
164
- return { delivered: true };
165
- },
166
- );
167
-
168
- expect(producer).toBeDefined();
169
- });
170
-
171
- it('should call beforeSend callback', async () => {
172
- const beforeSend = vi.fn();
173
- const config: ProducerConfig = {
174
- system: 'kafka',
175
- destination: 'events',
176
- beforeSend,
177
- };
178
-
179
- const producer = traceProducer(config)(
180
- (_ctx) => async (_event: unknown) => {
181
- return { sent: true };
182
- },
183
- );
184
-
185
- await producer({ id: '1' });
186
- expect(beforeSend).toHaveBeenCalled();
187
- });
188
-
189
- it('should call onError callback on failure', async () => {
190
- const onError = vi.fn();
191
- const testError = new Error('Send failed');
192
-
193
- const config: ProducerConfig = {
194
- system: 'kafka',
195
- destination: 'events',
196
- onError,
197
- };
198
-
199
- const producer = traceProducer(config)((_ctx) => async () => {
200
- throw testError;
201
- });
202
-
203
- await expect(producer()).rejects.toThrow('Send failed');
204
- expect(onError).toHaveBeenCalledWith(testError, expect.anything());
205
- });
206
-
207
- it('should extract message ID from function extractor', () => {
208
- const config: ProducerConfig = {
209
- system: 'kafka',
210
- destination: 'events',
211
- messageIdFrom: (args) => (args[0] as { eventId: string }).eventId,
212
- };
213
-
214
- expect(typeof config.messageIdFrom).toBe('function');
215
- const extractor = config.messageIdFrom as (
216
- args: unknown[],
217
- ) => string | undefined;
218
- expect(extractor([{ eventId: 'evt-123' }])).toBe('evt-123');
219
- });
220
-
221
- it('should extract partition from function extractor', () => {
222
- const config: ProducerConfig = {
223
- system: 'kafka',
224
- destination: 'events',
225
- partitionFrom: (args) => (args[0] as { partition: number }).partition,
226
- };
227
-
228
- expect(typeof config.partitionFrom).toBe('function');
229
- const extractor = config.partitionFrom as (
230
- args: unknown[],
231
- ) => number | undefined;
232
- expect(extractor([{ partition: 3 }])).toBe(3);
233
- });
234
- });
235
-
236
- describe('traceConsumer', () => {
237
- it('should create a consumer function with correct span name', async () => {
238
- const config: ConsumerConfig = {
239
- system: 'kafka',
240
- destination: 'user-events',
241
- consumerGroup: 'my-consumer',
242
- };
243
-
244
- const consumer = traceConsumer(config)(
245
- (_ctx) => async (_message: unknown) => {
246
- return { processed: true };
247
- },
248
- );
249
-
250
- const result = await consumer({ value: 'test' });
251
- expect(result).toEqual({ processed: true });
252
- });
253
-
254
- it('should support batch mode', async () => {
255
- const config: ConsumerConfig = {
256
- system: 'kafka',
257
- destination: 'user-events',
258
- batchMode: true,
259
- headersFrom: (msg) =>
260
- (msg as { headers: Record<string, string> }).headers,
261
- };
262
-
263
- const consumer = traceConsumer(config)(
264
- (_ctx) => async (messages: unknown[]) => {
265
- return { count: messages.length };
266
- },
267
- );
268
-
269
- const messages = [
270
- { value: 'a', headers: { traceparent: '00-abc-def-01' } },
271
- { value: 'b', headers: { traceparent: '00-xyz-uvw-01' } },
272
- ];
273
-
274
- const result = await consumer(messages);
275
- expect(result).toEqual({ count: 2 });
276
- });
277
-
278
- it('should call onError callback on failure', async () => {
279
- const onError = vi.fn();
280
- const testError = new Error('Process failed');
281
-
282
- const config: ConsumerConfig = {
283
- system: 'sqs',
284
- destination: 'orders',
285
- onError,
286
- };
287
-
288
- const consumer = traceConsumer(config)((_ctx) => async () => {
289
- throw testError;
290
- });
291
-
292
- await expect(consumer({})).rejects.toThrow('Process failed');
293
- expect(onError).toHaveBeenCalledWith(testError, expect.anything());
294
- });
295
-
296
- it('should support lag metrics configuration', () => {
297
- const config: ConsumerConfig = {
298
- system: 'kafka',
299
- destination: 'events',
300
- lagMetrics: {
301
- getCurrentOffset: (msg) => (msg as { offset: number }).offset,
302
- getEndOffset: () => Promise.resolve(1000),
303
- getPartition: (msg) => (msg as { partition: number }).partition,
304
- },
305
- };
306
-
307
- expect(config.lagMetrics).toBeDefined();
308
- expect(config.lagMetrics!.getCurrentOffset).toBeDefined();
309
- expect(config.lagMetrics!.getCurrentOffset!({ offset: 500 })).toBe(500);
310
- });
311
-
312
- it('should extract headers using string path', () => {
313
- const config: ConsumerConfig = {
314
- system: 'kafka',
315
- destination: 'events',
316
- headersFrom: 'headers',
317
- };
318
-
319
- expect(config.headersFrom).toBe('headers');
320
- });
321
-
322
- it('should extract headers using function', () => {
323
- const config: ConsumerConfig = {
324
- system: 'sqs',
325
- destination: 'events',
326
- headersFrom: (msg) => {
327
- const m = msg as {
328
- MessageAttributes: Record<string, { StringValue: string }>;
329
- };
330
- const result: Record<string, string> = {};
331
- for (const [k, v] of Object.entries(m.MessageAttributes || {})) {
332
- result[k] = v.StringValue;
333
- }
334
- return result;
335
- },
336
- };
337
-
338
- const extractor = config.headersFrom as (
339
- msg: unknown,
340
- ) => Record<string, string> | undefined;
341
- const headers = extractor({
342
- MessageAttributes: {
343
- traceparent: { StringValue: '00-abc-def-01' },
344
- },
345
- });
346
-
347
- expect(headers).toEqual({ traceparent: '00-abc-def-01' });
348
- });
349
- });
350
-
351
- describe('ProducerContext', () => {
352
- it('getTraceHeaders should return W3C trace context headers', () => {
353
- const mockCtx = createMockContext();
354
- const headers = mockCtx.getTraceHeaders();
355
-
356
- expect(headers.traceparent).toBeDefined();
357
- expect(headers.traceparent).toMatch(
358
- /^\d{2}-[a-f0-9]{32}-[a-f0-9]{16}-\d{2}$/,
359
- );
360
- });
361
-
362
- it('getAllPropagationHeaders should include baggage when enabled', () => {
363
- const mockCtx = createMockContext();
364
- const headers = mockCtx.getAllPropagationHeaders();
365
-
366
- expect(headers.traceparent).toBeDefined();
367
- expect(headers.baggage).toBeDefined();
368
- });
369
- });
370
-
371
- describe('ConsumerContext', () => {
372
- it('recordDLQ should be callable with basic args', () => {
373
- const mockCtx = createMockContext();
374
- mockCtx.recordDLQ('Max retries exceeded', 'orders-dlq');
375
-
376
- expect(mockCtx.recordDLQ).toHaveBeenCalledWith(
377
- 'Max retries exceeded',
378
- 'orders-dlq',
379
- );
380
- });
381
-
382
- it('recordRetry should be callable', () => {
383
- const mockCtx = createMockContext();
384
- mockCtx.recordRetry(2, 5);
385
-
386
- expect(mockCtx.recordRetry).toHaveBeenCalledWith(2, 5);
387
- });
388
-
389
- it('recordReplay should be callable', () => {
390
- const mockCtx = createMockContext();
391
- mockCtx.recordReplay({ replayAttempt: 1, dlqDwellTimeMs: 5000 });
392
-
393
- expect(mockCtx.recordReplay).toHaveBeenCalledWith({
394
- replayAttempt: 1,
395
- dlqDwellTimeMs: 5000,
396
- });
397
- });
398
-
399
- it('getProducerLinks should return links', () => {
400
- const mockCtx = createMockContext();
401
- const links = mockCtx.getProducerLinks();
402
-
403
- expect(mockCtx.getProducerLinks).toHaveBeenCalled();
404
- expect(Array.isArray(links)).toBe(true);
405
- });
406
- });
407
-
408
- describe('Enhanced DLQ Handling', () => {
409
- describe('DLQOptions types', () => {
410
- it('should support all reason categories', () => {
411
- const categories: DLQReasonCategory[] = [
412
- 'validation',
413
- 'processing',
414
- 'timeout',
415
- 'poison',
416
- 'unknown',
417
- ];
418
-
419
- for (const category of categories) {
420
- const options: DLQOptions = {
421
- reasonCategory: category,
422
- };
423
- expect(options.reasonCategory).toBe(category);
424
- }
425
- });
426
-
427
- it('should support full DLQ options', () => {
428
- const error = new Error('Processing failed');
429
- const options: DLQOptions = {
430
- linkToProducer: true,
431
- reasonCategory: 'processing',
432
- attemptCount: 3,
433
- originalError: error,
434
- metadata: {
435
- customField: 'value',
436
- retryDelay: 1000,
437
- isReplayable: true,
438
- },
439
- };
440
-
441
- expect(options.linkToProducer).toBe(true);
442
- expect(options.reasonCategory).toBe('processing');
443
- expect(options.attemptCount).toBe(3);
444
- expect(options.originalError).toBe(error);
445
- expect(options.metadata?.customField).toBe('value');
446
- });
447
-
448
- it('should allow partial options', () => {
449
- const options: DLQOptions = {
450
- reasonCategory: 'validation',
451
- };
452
-
453
- expect(options.reasonCategory).toBe('validation');
454
- expect(options.linkToProducer).toBeUndefined();
455
- expect(options.attemptCount).toBeUndefined();
456
- });
457
- });
458
-
459
- describe('DLQReplayOptions types', () => {
460
- it('should support replay options with span context', () => {
461
- const options: DLQReplayOptions = {
462
- originalDLQSpanContext: {
463
- traceId: '00000000000000000000000000000001',
464
- spanId: '0000000000000002',
465
- traceFlags: 1,
466
- },
467
- dlqDwellTimeMs: 3_600_000, // 1 hour
468
- replayAttempt: 2,
469
- };
470
-
471
- expect(options.originalDLQSpanContext?.traceId).toBe(
472
- '00000000000000000000000000000001',
473
- );
474
- expect(options.dlqDwellTimeMs).toBe(3_600_000);
475
- expect(options.replayAttempt).toBe(2);
476
- });
477
-
478
- it('should allow partial replay options', () => {
479
- const options: DLQReplayOptions = {
480
- replayAttempt: 1,
481
- };
482
-
483
- expect(options.replayAttempt).toBe(1);
484
- expect(options.originalDLQSpanContext).toBeUndefined();
485
- });
486
- });
487
-
488
- describe('recordDLQ overloads', () => {
489
- it('should support basic signature (reason only)', () => {
490
- const mockCtx = createMockContext();
491
- // This tests the type system accepts reason-only calls
492
- mockCtx.recordDLQ('Processing failed');
493
- expect(mockCtx.recordDLQ).toHaveBeenCalledWith('Processing failed');
494
- });
495
-
496
- it('should support reason + dlqName signature', () => {
497
- const mockCtx = createMockContext();
498
- mockCtx.recordDLQ('Schema validation failed', 'orders-dlq');
499
- expect(mockCtx.recordDLQ).toHaveBeenCalledWith(
500
- 'Schema validation failed',
501
- 'orders-dlq',
502
- );
503
- });
504
-
505
- it('should support reason + options signature', () => {
506
- const mockCtx = createMockContext();
507
- const options: DLQOptions = {
508
- reasonCategory: 'validation',
509
- attemptCount: 3,
510
- };
511
- mockCtx.recordDLQ('Invalid payload format', options);
512
- expect(mockCtx.recordDLQ).toHaveBeenCalledWith(
513
- 'Invalid payload format',
514
- options,
515
- );
516
- });
517
-
518
- it('should support reason + dlqName + options signature', () => {
519
- const mockCtx = createMockContext();
520
- const error = new Error('Timeout exceeded');
521
- const options: DLQOptions = {
522
- reasonCategory: 'timeout',
523
- attemptCount: 5,
524
- originalError: error,
525
- linkToProducer: true,
526
- metadata: { processingTimeMs: 30_000 },
527
- };
528
- mockCtx.recordDLQ('Processing timeout', 'orders-dlq', options);
529
- expect(mockCtx.recordDLQ).toHaveBeenCalledWith(
530
- 'Processing timeout',
531
- 'orders-dlq',
532
- options,
533
- );
534
- });
535
- });
536
-
537
- describe('Poison pill detection', () => {
538
- it('should support poison category for repeated failures', () => {
539
- const options: DLQOptions = {
540
- reasonCategory: 'poison',
541
- attemptCount: 10,
542
- metadata: {
543
- failurePattern: 'OutOfMemoryError',
544
- affectedConsumers: 3,
545
- },
546
- };
547
-
548
- expect(options.reasonCategory).toBe('poison');
549
- expect(options.attemptCount).toBe(10);
550
- });
551
- });
552
- });
553
-
554
- describe('MessagingSystem types', () => {
555
- it('should support kafka', () => {
556
- const config: ProducerConfig = { system: 'kafka', destination: 'topic' };
557
- expect(config.system).toBe('kafka');
558
- });
559
-
560
- it('should support sqs', () => {
561
- const config: ProducerConfig = { system: 'sqs', destination: 'queue' };
562
- expect(config.system).toBe('sqs');
563
- });
564
-
565
- it('should support rabbitmq', () => {
566
- const config: ProducerConfig = {
567
- system: 'rabbitmq',
568
- destination: 'exchange',
569
- };
570
- expect(config.system).toBe('rabbitmq');
571
- });
572
-
573
- it('should support sns', () => {
574
- const config: ProducerConfig = {
575
- system: 'sns',
576
- destination: 'topic-arn',
577
- };
578
- expect(config.system).toBe('sns');
579
- });
580
-
581
- it('should support pubsub', () => {
582
- const config: ProducerConfig = { system: 'pubsub', destination: 'topic' };
583
- expect(config.system).toBe('pubsub');
584
- });
585
-
586
- it('should support custom system strings', () => {
587
- const config: ProducerConfig = {
588
- system: 'custom-mq',
589
- destination: 'channel',
590
- };
591
- expect(config.system).toBe('custom-mq');
592
- });
593
- });
594
-
595
- describe('Span naming conventions', () => {
596
- it('producer should use system.publish destination format', () => {
597
- const config: ProducerConfig = {
598
- system: 'kafka',
599
- destination: 'user-events',
600
- };
601
- // The span name is constructed as `${config.system}.publish ${config.destination}`
602
- const expectedSpanName = `${config.system}.publish ${config.destination}`;
603
- expect(expectedSpanName).toBe('kafka.publish user-events');
604
- });
605
-
606
- it('consumer (single) should use system.process destination format', () => {
607
- const config: ConsumerConfig = {
608
- system: 'kafka',
609
- destination: 'user-events',
610
- };
611
- const expectedSpanName = `${config.system}.process ${config.destination}`;
612
- expect(expectedSpanName).toBe('kafka.process user-events');
613
- });
614
-
615
- it('consumer (batch) should use system.receive destination format', () => {
616
- const config: ConsumerConfig = {
617
- system: 'kafka',
618
- destination: 'user-events',
619
- batchMode: true,
620
- };
621
- const expectedSpanName = `${config.system}.receive ${config.destination}`;
622
- expect(expectedSpanName).toBe('kafka.receive user-events');
623
- });
624
- });
625
-
626
- // =========================================================================
627
- // Extensible Hooks ("Bring Your Own" System Support)
628
- // =========================================================================
629
-
630
- describe('Extensible Hooks', () => {
631
- describe('Producer customAttributes hook', () => {
632
- it('should call customAttributes hook with context and args', async () => {
633
- const customAttributes = vi.fn().mockReturnValue({
634
- 'nats.subject': 'orders.created',
635
- 'nats.stream': 'ORDERS',
636
- });
637
-
638
- const config: ProducerConfig = {
639
- system: 'nats' as const,
640
- destination: 'orders.created',
641
- customAttributes,
642
- };
643
-
644
- const producer = traceProducer(config)(
645
- (_ctx) => async (event: { orderId: string }) => {
646
- return { sent: true, orderId: event.orderId };
647
- },
648
- );
649
-
650
- await producer({ orderId: 'order-123' });
651
- expect(customAttributes).toHaveBeenCalled();
652
- // Args should include the event
653
- const [, args] = customAttributes.mock.calls[0];
654
- expect(args[0]).toEqual({ orderId: 'order-123' });
655
- });
656
-
657
- it('should support NATS-style attributes', () => {
658
- const config: ProducerConfig = {
659
- system: 'nats' as const,
660
- destination: 'orders.created',
661
- customAttributes: (_ctx, args) => ({
662
- 'nats.subject':
663
- (args[0] as { subject?: string })?.subject || 'default',
664
- 'nats.reply_to': (args[0] as { replyTo?: string })?.replyTo,
665
- 'nats.stream': 'ORDERS',
666
- }),
667
- };
668
-
669
- expect(config.customAttributes).toBeDefined();
670
- const attrs = config.customAttributes!(createMockContext() as never, [
671
- { subject: 'orders.created', replyTo: '_INBOX.reply' },
672
- ]);
673
- expect(attrs['nats.subject']).toBe('orders.created');
674
- expect(attrs['nats.reply_to']).toBe('_INBOX.reply');
675
- });
676
-
677
- it('should support Temporal-style attributes', () => {
678
- const config: ProducerConfig = {
679
- system: 'temporal' as const,
680
- destination: 'orders-queue',
681
- customAttributes: (_ctx, args) => ({
682
- 'temporal.workflow_id': (args[0] as { workflowId: string })
683
- .workflowId,
684
- 'temporal.run_id': (args[0] as { runId: string }).runId,
685
- 'temporal.task_queue': 'orders-queue',
686
- }),
687
- };
688
-
689
- expect(config.customAttributes).toBeDefined();
690
- const attrs = config.customAttributes!(createMockContext() as never, [
691
- { workflowId: 'wf-123', runId: 'run-456' },
692
- ]);
693
- expect(attrs['temporal.workflow_id']).toBe('wf-123');
694
- expect(attrs['temporal.run_id']).toBe('run-456');
695
- });
696
- });
697
-
698
- describe('Producer customHeaders hook', () => {
699
- it('should call customHeaders hook', async () => {
700
- const customHeaders = vi.fn().mockReturnValue({
701
- 'x-correlation-id': 'corr-123',
702
- 'x-request-id': 'req-456',
703
- });
704
-
705
- const config: ProducerConfig = {
706
- system: 'kafka',
707
- destination: 'events',
708
- customHeaders,
709
- };
710
-
711
- // The customHeaders is called via getFullHeaders()
712
- // For testing, we verify the config accepts it
713
- expect(config.customHeaders).toBeDefined();
714
- });
715
-
716
- it('should support Datadog-style custom headers', () => {
717
- const config: ProducerConfig = {
718
- system: 'kafka',
719
- destination: 'events',
720
- customHeaders: (ctx) => ({
721
- 'x-datadog-trace-id': ctx.getTraceId?.() || 'unknown',
722
- 'x-datadog-parent-id': ctx.getSpanId?.() || 'unknown',
723
- }),
724
- };
725
-
726
- expect(config.customHeaders).toBeDefined();
727
- });
728
- });
729
-
730
- describe('Consumer customAttributes hook', () => {
731
- it('should call customAttributes hook with context and message', async () => {
732
- const customAttributes = vi.fn().mockReturnValue({
733
- 'cloudflare.queue_id': 'q-123',
734
- 'cloudflare.attempts': 1,
735
- });
736
-
737
- const config: ConsumerConfig = {
738
- system: 'cloudflare_queues' as const,
739
- destination: 'orders-queue',
740
- customAttributes,
741
- };
742
-
743
- const consumer = traceConsumer(config)(
744
- (_ctx) => async (msg: { id: string; attempts: number }) => {
745
- return { processed: true, id: msg.id };
746
- },
747
- );
748
-
749
- await consumer({ id: 'msg-123', attempts: 1 });
750
- expect(customAttributes).toHaveBeenCalled();
751
- });
752
-
753
- it('should support Cloudflare Queue attributes', () => {
754
- const config: ConsumerConfig = {
755
- system: 'cloudflare_queues' as const,
756
- destination: 'orders',
757
- customAttributes: (_ctx, msg) => ({
758
- 'cloudflare.queue_id': (msg as { id: string }).id,
759
- 'cloudflare.timestamp_ms': Date.now(),
760
- 'cloudflare.attempts': (msg as { attempts: number }).attempts,
761
- }),
762
- };
763
-
764
- expect(config.customAttributes).toBeDefined();
765
- const attrs = config.customAttributes!(createMockContext() as never, {
766
- id: 'msg-123',
767
- attempts: 2,
768
- });
769
- expect(attrs['cloudflare.queue_id']).toBe('msg-123');
770
- expect(attrs['cloudflare.attempts']).toBe(2);
771
- });
772
-
773
- it('should support Redis Streams attributes', () => {
774
- const config: ConsumerConfig = {
775
- system: 'redis_streams' as const,
776
- destination: 'orders:stream',
777
- customAttributes: (_ctx, msg) => ({
778
- 'redis.stream_id': (msg as { streamId: string }).streamId,
779
- 'redis.consumer_group': 'processors',
780
- 'redis.pending_count': (msg as { pendingCount: number })
781
- .pendingCount,
782
- }),
783
- };
784
-
785
- expect(config.customAttributes).toBeDefined();
786
- const attrs = config.customAttributes!(createMockContext() as never, {
787
- streamId: '1234567890-0',
788
- pendingCount: 5,
789
- });
790
- expect(attrs['redis.stream_id']).toBe('1234567890-0');
791
- });
792
- });
793
-
794
- describe('Consumer customContextExtractor hook', () => {
795
- it('should support B3 format extraction', () => {
796
- const config: ConsumerConfig = {
797
- system: 'kafka',
798
- destination: 'events',
799
- headersFrom: 'headers',
800
- customContextExtractor: (headers) => {
801
- const traceId = headers['x-b3-traceid'];
802
- const spanId = headers['x-b3-spanid'];
803
- const sampled = headers['x-b3-sampled'] === '1';
804
- if (!traceId || !spanId) return null;
805
- return {
806
- traceId,
807
- spanId,
808
- traceFlags: sampled ? 1 : 0,
809
- isRemote: true,
810
- };
811
- },
812
- };
813
-
814
- expect(config.customContextExtractor).toBeDefined();
815
- const context = config.customContextExtractor!({
816
- 'x-b3-traceid': '00000000000000000000000000000abc',
817
- 'x-b3-spanid': '00000000000def',
818
- 'x-b3-sampled': '1',
819
- });
820
- expect(context).not.toBeNull();
821
- expect(context!.traceId).toBe('00000000000000000000000000000abc');
822
- expect(context!.spanId).toBe('00000000000def');
823
- expect(context!.traceFlags).toBe(1);
824
- });
825
-
826
- it('should support Datadog format extraction', () => {
827
- const config: ConsumerConfig = {
828
- system: 'kafka',
829
- destination: 'events',
830
- headersFrom: 'headers',
831
- customContextExtractor: (headers) => {
832
- const traceId = headers['x-datadog-trace-id'];
833
- const spanId = headers['x-datadog-parent-id'];
834
- if (!traceId || !spanId) return null;
835
- // Convert Datadog's decimal format to OTel hex
836
- return {
837
- traceId: BigInt(traceId).toString(16).padStart(32, '0'),
838
- spanId: BigInt(spanId).toString(16).padStart(16, '0'),
839
- traceFlags: 1,
840
- isRemote: true,
841
- };
842
- },
843
- };
844
-
845
- expect(config.customContextExtractor).toBeDefined();
846
- const context = config.customContextExtractor!({
847
- 'x-datadog-trace-id': '123456789',
848
- 'x-datadog-parent-id': '987654321',
849
- });
850
- expect(context).not.toBeNull();
851
- // 123456789 decimal = 75bcd15 hex (7 chars) → padStart(32) = 25 zeros + 7 = 32 chars
852
- expect(context!.traceId).toBe('000000000000000000000000075bcd15');
853
- // 987654321 decimal = 3ade68b1 hex (8 chars) → padStart(16) = 8 zeros + 8 = 16 chars
854
- expect(context!.spanId).toBe('000000003ade68b1');
855
- });
856
-
857
- it('should return null for missing headers', () => {
858
- const config: ConsumerConfig = {
859
- system: 'kafka',
860
- destination: 'events',
861
- headersFrom: 'headers',
862
- customContextExtractor: (headers) => {
863
- const traceId = headers['x-b3-traceid'];
864
- const spanId = headers['x-b3-spanid'];
865
- if (!traceId || !spanId) return null;
866
- return { traceId, spanId, traceFlags: 1, isRemote: true };
867
- },
868
- };
869
-
870
- const context = config.customContextExtractor!({});
871
- expect(context).toBeNull();
872
- });
873
- });
874
-
875
- describe('Combined hooks usage', () => {
876
- it('should support using all hooks together for NATS', () => {
877
- const producerConfig: ProducerConfig = {
878
- system: 'nats' as const,
879
- destination: 'orders.created',
880
- customAttributes: (_ctx, args) => ({
881
- 'nats.subject': 'orders.created',
882
- 'nats.stream': 'ORDERS',
883
- 'nats.sequence': (args[0] as { seq?: number })?.seq || 0,
884
- }),
885
- customHeaders: (_ctx) => ({
886
- 'Nats-Msg-Id': `msg-${Date.now()}`,
887
- }),
888
- };
889
-
890
- const consumerConfig: ConsumerConfig = {
891
- system: 'nats' as const,
892
- destination: 'orders.created',
893
- headersFrom: 'headers',
894
- customAttributes: (_ctx, msg) => ({
895
- 'nats.subject': (msg as { subject: string }).subject,
896
- 'nats.redelivered':
897
- (msg as { redelivered?: boolean })?.redelivered || false,
898
- }),
899
- customContextExtractor: (headers) => {
900
- // NATS uses its own tracing headers
901
- const traceId = headers['nats-trace-id'];
902
- const spanId = headers['nats-span-id'];
903
- if (!traceId || !spanId) return null;
904
- return { traceId, spanId, traceFlags: 1, isRemote: true };
905
- },
906
- };
907
-
908
- expect(producerConfig.customAttributes).toBeDefined();
909
- expect(producerConfig.customHeaders).toBeDefined();
910
- expect(consumerConfig.customAttributes).toBeDefined();
911
- expect(consumerConfig.customContextExtractor).toBeDefined();
912
- });
913
- });
914
- });
915
-
916
- describe('Message Ordering Support', () => {
917
- beforeEach(() => {
918
- // Clear global ordering state between tests
919
- clearOrderingState();
920
- });
921
-
922
- describe('OrderingConfig types', () => {
923
- it('should define OrderingConfig interface correctly', () => {
924
- const config: OrderingConfig = {
925
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
926
- partitionKeyFrom: (msg) => (msg as { key: string }).key,
927
- messageIdFrom: (msg) => (msg as { id: string }).id,
928
- detectOutOfOrder: true,
929
- detectDuplicates: true,
930
- deduplicationWindowSize: 500,
931
- onOutOfOrder: (_ctx, _info) => {},
932
- onDuplicate: (_ctx, _id) => {},
933
- };
934
-
935
- expect(config.sequenceFrom).toBeDefined();
936
- expect(config.partitionKeyFrom).toBeDefined();
937
- expect(config.messageIdFrom).toBeDefined();
938
- expect(config.detectOutOfOrder).toBe(true);
939
- expect(config.detectDuplicates).toBe(true);
940
- expect(config.deduplicationWindowSize).toBe(500);
941
- });
942
-
943
- it('should define OutOfOrderInfo interface correctly', () => {
944
- const info: OutOfOrderInfo = {
945
- currentSequence: 10,
946
- expectedSequence: 5,
947
- partitionKey: 'partition-1',
948
- gap: 5,
949
- };
950
-
951
- expect(info.currentSequence).toBe(10);
952
- expect(info.expectedSequence).toBe(5);
953
- expect(info.partitionKey).toBe('partition-1');
954
- expect(info.gap).toBe(5);
955
- });
956
- });
957
-
958
- describe('Sequence tracking', () => {
959
- it('should extract sequence number from message', () => {
960
- const config: ConsumerConfig = {
961
- system: 'kafka',
962
- destination: 'orders',
963
- ordering: {
964
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
965
- },
966
- };
967
-
968
- expect(config.ordering?.sequenceFrom?.({ offset: 42 })).toBe(42);
969
- });
970
-
971
- it('should extract partition key from message', () => {
972
- const config: ConsumerConfig = {
973
- system: 'kafka',
974
- destination: 'orders',
975
- ordering: {
976
- partitionKeyFrom: (msg) => (msg as { key: string }).key,
977
- },
978
- };
979
-
980
- expect(config.ordering?.partitionKeyFrom?.({ key: 'user-123' })).toBe(
981
- 'user-123',
982
- );
983
- });
984
-
985
- it('should extract message ID for deduplication', () => {
986
- const config: ConsumerConfig = {
987
- system: 'kafka',
988
- destination: 'orders',
989
- ordering: {
990
- messageIdFrom: (msg) => (msg as { id: string }).id,
991
- },
992
- };
993
-
994
- expect(config.ordering?.messageIdFrom?.({ id: 'msg-abc' })).toBe(
995
- 'msg-abc',
996
- );
997
- });
998
- });
999
-
1000
- describe('Out-of-order detection', () => {
1001
- it('should detect out-of-order messages and call callback', async () => {
1002
- const outOfOrderCallback = vi.fn();
1003
-
1004
- const config: ConsumerConfig = {
1005
- system: 'kafka',
1006
- destination: 'orders',
1007
- ordering: {
1008
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
1009
- detectOutOfOrder: true,
1010
- onOutOfOrder: outOfOrderCallback,
1011
- },
1012
- };
1013
-
1014
- // Verify config is set up correctly
1015
- expect(config.ordering?.detectOutOfOrder).toBe(true);
1016
- expect(config.ordering?.onOutOfOrder).toBe(outOfOrderCallback);
1017
- });
1018
-
1019
- it('should calculate gap correctly for missing messages', () => {
1020
- // Test gap calculation: if we expect 5 but get 10, gap is 5
1021
- const info: OutOfOrderInfo = {
1022
- currentSequence: 10,
1023
- expectedSequence: 5,
1024
- gap: 10 - 5, // = 5 (5 messages missing)
1025
- };
1026
-
1027
- expect(info.gap).toBe(5);
1028
- });
1029
-
1030
- it('should calculate negative gap for out-of-order (earlier) messages', () => {
1031
- // If we expect 10 but get 5 (received earlier message), gap is -5
1032
- const info: OutOfOrderInfo = {
1033
- currentSequence: 5,
1034
- expectedSequence: 10,
1035
- gap: 5 - 10, // = -5 (message from past)
1036
- };
1037
-
1038
- expect(info.gap).toBe(-5);
1039
- });
1040
-
1041
- it('should track sequence per partition key', () => {
1042
- // Verify config supports partition-specific tracking
1043
- const config: ConsumerConfig = {
1044
- system: 'kafka',
1045
- destination: 'orders',
1046
- ordering: {
1047
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
1048
- partitionKeyFrom: (msg) => (msg as { partition: string }).partition,
1049
- detectOutOfOrder: true,
1050
- },
1051
- };
1052
-
1053
- expect(config.ordering?.partitionKeyFrom).toBeDefined();
1054
- expect(config.ordering?.sequenceFrom).toBeDefined();
1055
- });
1056
- });
1057
-
1058
- describe('Duplicate detection', () => {
1059
- it('should detect duplicate messages and call callback', async () => {
1060
- const duplicateCallback = vi.fn();
1061
-
1062
- const config: ConsumerConfig = {
1063
- system: 'kafka',
1064
- destination: 'orders',
1065
- ordering: {
1066
- messageIdFrom: (msg) => (msg as { id: string }).id,
1067
- detectDuplicates: true,
1068
- onDuplicate: duplicateCallback,
1069
- },
1070
- };
1071
-
1072
- // Verify config is set up correctly
1073
- expect(config.ordering?.detectDuplicates).toBe(true);
1074
- expect(config.ordering?.onDuplicate).toBe(duplicateCallback);
1075
- });
1076
-
1077
- it('should use custom deduplication window size', () => {
1078
- const config: ConsumerConfig = {
1079
- system: 'kafka',
1080
- destination: 'orders',
1081
- ordering: {
1082
- messageIdFrom: (msg) => (msg as { id: string }).id,
1083
- detectDuplicates: true,
1084
- deduplicationWindowSize: 500,
1085
- },
1086
- };
1087
-
1088
- expect(config.ordering?.deduplicationWindowSize).toBe(500);
1089
- });
1090
-
1091
- it('should default deduplication window to 1000', () => {
1092
- const config: ConsumerConfig = {
1093
- system: 'kafka',
1094
- destination: 'orders',
1095
- ordering: {
1096
- messageIdFrom: (msg) => (msg as { id: string }).id,
1097
- detectDuplicates: true,
1098
- // No deduplicationWindowSize - defaults to 1000
1099
- },
1100
- };
1101
-
1102
- expect(config.ordering?.deduplicationWindowSize).toBeUndefined();
1103
- // The actual default (1000) is applied at runtime in extractAndProcessOrdering
1104
- });
1105
- });
1106
-
1107
- describe('ConsumerContext ordering methods', () => {
1108
- it('should have isDuplicate() method in ConsumerContext interface', () => {
1109
- // Type check - these methods are defined in ConsumerContext interface
1110
- const config: ConsumerConfig = {
1111
- system: 'kafka',
1112
- destination: 'orders',
1113
- ordering: {
1114
- detectDuplicates: true,
1115
- },
1116
- };
1117
-
1118
- // Verify the config is valid
1119
- expect(config.ordering?.detectDuplicates).toBe(true);
1120
- });
1121
-
1122
- it('should have getOutOfOrderInfo() method in ConsumerContext interface', () => {
1123
- const config: ConsumerConfig = {
1124
- system: 'kafka',
1125
- destination: 'orders',
1126
- ordering: {
1127
- detectOutOfOrder: true,
1128
- },
1129
- };
1130
-
1131
- expect(config.ordering?.detectOutOfOrder).toBe(true);
1132
- });
1133
-
1134
- it('should have getSequenceNumber() method in ConsumerContext interface', () => {
1135
- const config: ConsumerConfig = {
1136
- system: 'kafka',
1137
- destination: 'orders',
1138
- ordering: {
1139
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
1140
- },
1141
- };
1142
-
1143
- expect(config.ordering?.sequenceFrom).toBeDefined();
1144
- });
1145
-
1146
- it('should have getPartitionKey() method in ConsumerContext interface', () => {
1147
- const config: ConsumerConfig = {
1148
- system: 'kafka',
1149
- destination: 'orders',
1150
- ordering: {
1151
- partitionKeyFrom: (msg) => (msg as { key: string }).key,
1152
- },
1153
- };
1154
-
1155
- expect(config.ordering?.partitionKeyFrom).toBeDefined();
1156
- });
1157
- });
1158
-
1159
- describe('Combined ordering configuration', () => {
1160
- it('should support full Kafka ordering configuration', () => {
1161
- const outOfOrderCallback = vi.fn();
1162
- const duplicateCallback = vi.fn();
1163
-
1164
- const config: ConsumerConfig = {
1165
- system: 'kafka',
1166
- destination: 'user-events',
1167
- consumerGroup: 'event-processor',
1168
- ordering: {
1169
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
1170
- partitionKeyFrom: (msg) => (msg as { key: string }).key,
1171
- messageIdFrom: (msg) =>
1172
- (msg as { headers: { idempotencyKey: string } }).headers
1173
- .idempotencyKey,
1174
- detectOutOfOrder: true,
1175
- detectDuplicates: true,
1176
- deduplicationWindowSize: 2000,
1177
- onOutOfOrder: outOfOrderCallback,
1178
- onDuplicate: duplicateCallback,
1179
- },
1180
- };
1181
-
1182
- expect(config.ordering).toBeDefined();
1183
- expect(config.ordering?.detectOutOfOrder).toBe(true);
1184
- expect(config.ordering?.detectDuplicates).toBe(true);
1185
- expect(config.ordering?.deduplicationWindowSize).toBe(2000);
1186
- });
1187
-
1188
- it('should support SQS ordering configuration with message group', () => {
1189
- const config: ConsumerConfig = {
1190
- system: 'sqs',
1191
- destination: 'orders.fifo',
1192
- ordering: {
1193
- sequenceFrom: (msg) =>
1194
- Number.parseInt(
1195
- (msg as { Attributes: { SequenceNumber: string } }).Attributes
1196
- .SequenceNumber,
1197
- 10,
1198
- ),
1199
- partitionKeyFrom: (msg) =>
1200
- (msg as { Attributes: { MessageGroupId: string } }).Attributes
1201
- .MessageGroupId,
1202
- messageIdFrom: (msg) => (msg as { MessageId: string }).MessageId,
1203
- detectOutOfOrder: true,
1204
- detectDuplicates: true,
1205
- },
1206
- };
1207
-
1208
- // Test extraction functions
1209
- const sqsMessage = {
1210
- MessageId: 'msg-123',
1211
- Attributes: {
1212
- SequenceNumber: '12345',
1213
- MessageGroupId: 'order-group-1',
1214
- },
1215
- };
1216
-
1217
- expect(config.ordering?.sequenceFrom?.(sqsMessage)).toBe(12_345);
1218
- expect(config.ordering?.partitionKeyFrom?.(sqsMessage)).toBe(
1219
- 'order-group-1',
1220
- );
1221
- expect(config.ordering?.messageIdFrom?.(sqsMessage)).toBe('msg-123');
1222
- });
1223
-
1224
- it('should support RabbitMQ ordering configuration', () => {
1225
- const config: ConsumerConfig = {
1226
- system: 'rabbitmq',
1227
- destination: 'orders',
1228
- consumerGroup: 'order-processor',
1229
- ordering: {
1230
- sequenceFrom: (msg) =>
1231
- (msg as { properties: { headers: { 'x-sequence': number } } })
1232
- .properties.headers['x-sequence'],
1233
- messageIdFrom: (msg) =>
1234
- (msg as { properties: { messageId: string } }).properties
1235
- .messageId,
1236
- detectDuplicates: true,
1237
- },
1238
- };
1239
-
1240
- const rabbitMessage = {
1241
- properties: {
1242
- messageId: 'rabbit-msg-1',
1243
- headers: {
1244
- 'x-sequence': 42,
1245
- },
1246
- },
1247
- };
1248
-
1249
- expect(config.ordering?.sequenceFrom?.(rabbitMessage)).toBe(42);
1250
- expect(config.ordering?.messageIdFrom?.(rabbitMessage)).toBe(
1251
- 'rabbit-msg-1',
1252
- );
1253
- });
1254
- });
1255
-
1256
- describe('clearOrderingState()', () => {
1257
- it('should be exported for test isolation', () => {
1258
- expect(typeof clearOrderingState).toBe('function');
1259
- });
1260
-
1261
- it('should not throw when called', () => {
1262
- expect(() => clearOrderingState()).not.toThrow();
1263
- });
1264
-
1265
- it('should allow multiple calls', () => {
1266
- clearOrderingState();
1267
- clearOrderingState();
1268
- clearOrderingState();
1269
- // No error = success
1270
- expect(true).toBe(true);
1271
- });
1272
- });
1273
-
1274
- describe('Span attributes', () => {
1275
- it('should define expected attribute names for sequence number', () => {
1276
- // Document expected span attributes
1277
- const expectedAttributes = [
1278
- 'messaging.message.sequence_number',
1279
- 'messaging.message.partition_key',
1280
- 'messaging.message.id',
1281
- 'messaging.ordering.out_of_order',
1282
- 'messaging.ordering.expected_sequence',
1283
- 'messaging.ordering.gap',
1284
- 'messaging.ordering.duplicate',
1285
- ];
1286
-
1287
- // These are the attributes set by extractAndProcessOrdering
1288
- expect(expectedAttributes).toContain(
1289
- 'messaging.message.sequence_number',
1290
- );
1291
- expect(expectedAttributes).toContain('messaging.ordering.out_of_order');
1292
- expect(expectedAttributes).toContain('messaging.ordering.duplicate');
1293
- });
1294
-
1295
- it('should define expected event names', () => {
1296
- const expectedEvents = ['message_out_of_order', 'message_duplicate'];
1297
-
1298
- expect(expectedEvents).toContain('message_out_of_order');
1299
- expect(expectedEvents).toContain('message_duplicate');
1300
- });
1301
- });
1302
- });
1303
-
1304
- describe('Consumer Group Tracking', () => {
1305
- describe('ConsumerGroupTrackingConfig types', () => {
1306
- it('should define ConsumerGroupTrackingConfig interface correctly', () => {
1307
- const config: ConsumerGroupTrackingConfig = {
1308
- memberId: 'consumer-1',
1309
- groupInstanceId: 'instance-1',
1310
- onRebalance: (_ctx, _event) => {},
1311
- onPartitionsAssigned: (_ctx, _partitions) => {},
1312
- onPartitionsRevoked: (_ctx, _partitions) => {},
1313
- trackPartitionLag: true,
1314
- trackHeartbeat: true,
1315
- heartbeatIntervalMs: 3000,
1316
- };
1317
-
1318
- expect(config.memberId).toBe('consumer-1');
1319
- expect(config.groupInstanceId).toBe('instance-1');
1320
- expect(config.trackPartitionLag).toBe(true);
1321
- expect(config.trackHeartbeat).toBe(true);
1322
- expect(config.heartbeatIntervalMs).toBe(3000);
1323
- });
1324
-
1325
- it('should support function-based memberId and groupInstanceId', () => {
1326
- let dynamicMemberId = 'consumer-initial';
1327
-
1328
- const config: ConsumerGroupTrackingConfig = {
1329
- memberId: () => dynamicMemberId,
1330
- groupInstanceId: () => 'static-instance',
1331
- };
1332
-
1333
- expect(typeof config.memberId).toBe('function');
1334
- expect((config.memberId as () => string)()).toBe('consumer-initial');
1335
-
1336
- dynamicMemberId = 'consumer-updated';
1337
- expect((config.memberId as () => string)()).toBe('consumer-updated');
1338
- });
1339
- });
1340
-
1341
- describe('RebalanceEvent types', () => {
1342
- it('should define RebalanceEvent interface correctly', () => {
1343
- const event: RebalanceEvent = {
1344
- type: 'assigned',
1345
- partitions: [
1346
- { topic: 'orders', partition: 0, offset: 100 },
1347
- { topic: 'orders', partition: 1, offset: 200 },
1348
- ],
1349
- timestamp: Date.now(),
1350
- generation: 5,
1351
- memberId: 'consumer-1',
1352
- reason: 'new consumer joined',
1353
- };
1354
-
1355
- expect(event.type).toBe('assigned');
1356
- expect(event.partitions).toHaveLength(2);
1357
- expect(event.generation).toBe(5);
1358
- expect(event.memberId).toBe('consumer-1');
1359
- });
1360
-
1361
- it('should support all rebalance types', () => {
1362
- const types: RebalanceType[] = ['assigned', 'revoked', 'lost'];
1363
-
1364
- for (const type of types) {
1365
- const event: RebalanceEvent = {
1366
- type,
1367
- partitions: [],
1368
- timestamp: Date.now(),
1369
- };
1370
- expect(event.type).toBe(type);
1371
- }
1372
- });
1373
- });
1374
-
1375
- describe('PartitionAssignment types', () => {
1376
- it('should define PartitionAssignment interface correctly', () => {
1377
- const assignment: PartitionAssignment = {
1378
- topic: 'user-events',
1379
- partition: 3,
1380
- offset: 12_345,
1381
- metadata: 'leader-epoch:10',
1382
- };
1383
-
1384
- expect(assignment.topic).toBe('user-events');
1385
- expect(assignment.partition).toBe(3);
1386
- expect(assignment.offset).toBe(12_345);
1387
- expect(assignment.metadata).toBe('leader-epoch:10');
1388
- });
1389
-
1390
- it('should allow minimal partition assignment', () => {
1391
- const assignment: PartitionAssignment = {
1392
- topic: 'events',
1393
- partition: 0,
1394
- };
1395
-
1396
- expect(assignment.topic).toBe('events');
1397
- expect(assignment.partition).toBe(0);
1398
- expect(assignment.offset).toBeUndefined();
1399
- });
1400
- });
1401
-
1402
- describe('ConsumerGroupState types', () => {
1403
- it('should define ConsumerGroupState interface correctly', () => {
1404
- const state: ConsumerGroupState = {
1405
- groupId: 'order-processors',
1406
- memberId: 'consumer-abc123',
1407
- groupInstanceId: 'instance-1',
1408
- assignedPartitions: [
1409
- { topic: 'orders', partition: 0 },
1410
- { topic: 'orders', partition: 1 },
1411
- ],
1412
- generation: 10,
1413
- isActive: true,
1414
- lastHeartbeat: Date.now(),
1415
- state: 'stable',
1416
- };
1417
-
1418
- expect(state.groupId).toBe('order-processors');
1419
- expect(state.isActive).toBe(true);
1420
- expect(state.state).toBe('stable');
1421
- expect(state.assignedPartitions).toHaveLength(2);
1422
- });
1423
-
1424
- it('should support all consumer group states', () => {
1425
- const states: ConsumerGroupState['state'][] = [
1426
- 'stable',
1427
- 'preparing_rebalance',
1428
- 'completing_rebalance',
1429
- 'dead',
1430
- 'empty',
1431
- ];
1432
-
1433
- for (const groupState of states) {
1434
- const state: ConsumerGroupState = {
1435
- groupId: 'test',
1436
- assignedPartitions: [],
1437
- isActive: groupState !== 'dead',
1438
- state: groupState,
1439
- };
1440
- expect(state.state).toBe(groupState);
1441
- }
1442
- });
1443
- });
1444
-
1445
- describe('PartitionLag types', () => {
1446
- it('should define PartitionLag interface correctly', () => {
1447
- const lag: PartitionLag = {
1448
- topic: 'events',
1449
- partition: 2,
1450
- currentOffset: 1000,
1451
- endOffset: 1500,
1452
- lag: 500,
1453
- timestamp: Date.now(),
1454
- };
1455
-
1456
- expect(lag.topic).toBe('events');
1457
- expect(lag.partition).toBe(2);
1458
- expect(lag.lag).toBe(500);
1459
- expect(lag.currentOffset).toBe(1000);
1460
- expect(lag.endOffset).toBe(1500);
1461
- });
1462
-
1463
- it('should calculate lag correctly', () => {
1464
- const currentOffset = 5000;
1465
- const endOffset = 5250;
1466
-
1467
- const lag: PartitionLag = {
1468
- topic: 'orders',
1469
- partition: 0,
1470
- currentOffset,
1471
- endOffset,
1472
- lag: endOffset - currentOffset,
1473
- timestamp: Date.now(),
1474
- };
1475
-
1476
- expect(lag.lag).toBe(250);
1477
- });
1478
- });
1479
-
1480
- describe('Consumer config with group tracking', () => {
1481
- it('should support full Kafka consumer group configuration', () => {
1482
- const rebalanceCallback = vi.fn();
1483
- const assignedCallback = vi.fn();
1484
- const revokedCallback = vi.fn();
1485
-
1486
- const config: ConsumerConfig = {
1487
- system: 'kafka',
1488
- destination: 'user-events',
1489
- consumerGroup: 'event-processor',
1490
- consumerGroupTracking: {
1491
- memberId: () => 'consumer-123',
1492
- groupInstanceId: 'static-instance-1',
1493
- onRebalance: rebalanceCallback,
1494
- onPartitionsAssigned: assignedCallback,
1495
- onPartitionsRevoked: revokedCallback,
1496
- trackPartitionLag: true,
1497
- trackHeartbeat: true,
1498
- heartbeatIntervalMs: 3000,
1499
- },
1500
- };
1501
-
1502
- expect(config.consumerGroupTracking).toBeDefined();
1503
- expect(config.consumerGroupTracking?.trackPartitionLag).toBe(true);
1504
- expect(config.consumerGroupTracking?.trackHeartbeat).toBe(true);
1505
- });
1506
-
1507
- it('should support minimal group tracking configuration', () => {
1508
- const config: ConsumerConfig = {
1509
- system: 'kafka',
1510
- destination: 'events',
1511
- consumerGroup: 'processors',
1512
- consumerGroupTracking: {
1513
- memberId: 'consumer-1',
1514
- },
1515
- };
1516
-
1517
- expect(config.consumerGroupTracking?.memberId).toBe('consumer-1');
1518
- expect(config.consumerGroupTracking?.trackPartitionLag).toBeUndefined();
1519
- });
1520
- });
1521
-
1522
- describe('Span attributes for consumer groups', () => {
1523
- it('should define expected attribute names', () => {
1524
- const expectedAttributes = [
1525
- 'messaging.consumer_group.rebalance.type',
1526
- 'messaging.consumer_group.rebalance.partition_count',
1527
- 'messaging.consumer_group.generation',
1528
- 'messaging.consumer_group.member_id',
1529
- 'messaging.consumer_group.rebalance.reason',
1530
- 'messaging.consumer_group.heartbeat.healthy',
1531
- 'messaging.consumer_group.heartbeat.latency_ms',
1532
- ];
1533
-
1534
- expect(expectedAttributes).toContain(
1535
- 'messaging.consumer_group.rebalance.type',
1536
- );
1537
- expect(expectedAttributes).toContain(
1538
- 'messaging.consumer_group.generation',
1539
- );
1540
- expect(expectedAttributes).toContain(
1541
- 'messaging.consumer_group.heartbeat.healthy',
1542
- );
1543
- });
1544
-
1545
- it('should define expected event names', () => {
1546
- const expectedEvents = [
1547
- 'consumer_group_assigned',
1548
- 'consumer_group_revoked',
1549
- 'consumer_group_lost',
1550
- 'consumer_group_heartbeat',
1551
- 'partition_lag_recorded',
1552
- ];
1553
-
1554
- expect(expectedEvents).toContain('consumer_group_assigned');
1555
- expect(expectedEvents).toContain('consumer_group_revoked');
1556
- expect(expectedEvents).toContain('consumer_group_heartbeat');
1557
- });
1558
- });
1559
-
1560
- describe('Integration with other features', () => {
1561
- it('should work alongside ordering configuration', () => {
1562
- const config: ConsumerConfig = {
1563
- system: 'kafka',
1564
- destination: 'orders',
1565
- consumerGroup: 'order-processor',
1566
- ordering: {
1567
- sequenceFrom: (msg) => (msg as { offset: number }).offset,
1568
- detectOutOfOrder: true,
1569
- },
1570
- consumerGroupTracking: {
1571
- memberId: 'consumer-1',
1572
- trackPartitionLag: true,
1573
- },
1574
- };
1575
-
1576
- expect(config.ordering).toBeDefined();
1577
- expect(config.consumerGroupTracking).toBeDefined();
1578
- });
1579
-
1580
- it('should work alongside lag metrics configuration', () => {
1581
- const config: ConsumerConfig = {
1582
- system: 'kafka',
1583
- destination: 'events',
1584
- consumerGroup: 'processor',
1585
- lagMetrics: {
1586
- getCurrentOffset: (msg) => (msg as { offset: number }).offset,
1587
- getEndOffset: () => Promise.resolve(10_000),
1588
- },
1589
- consumerGroupTracking: {
1590
- memberId: 'consumer-1',
1591
- trackPartitionLag: true,
1592
- },
1593
- };
1594
-
1595
- expect(config.lagMetrics).toBeDefined();
1596
- expect(config.consumerGroupTracking).toBeDefined();
1597
- });
1598
-
1599
- it('should work with custom attributes hook', () => {
1600
- const config: ConsumerConfig = {
1601
- system: 'kafka',
1602
- destination: 'events',
1603
- consumerGroup: 'processor',
1604
- consumerGroupTracking: {
1605
- memberId: 'consumer-1',
1606
- },
1607
- customAttributes: (_ctx, msg) => ({
1608
- 'custom.key': (msg as { key: string }).key,
1609
- }),
1610
- };
1611
-
1612
- expect(config.consumerGroupTracking).toBeDefined();
1613
- expect(config.customAttributes).toBeDefined();
1614
- });
1615
- });
1616
-
1617
- describe('SQS and RabbitMQ support', () => {
1618
- it('should support SQS with group tracking', () => {
1619
- const config: ConsumerConfig = {
1620
- system: 'sqs',
1621
- destination: 'orders.fifo',
1622
- consumerGroup: 'order-processor',
1623
- consumerGroupTracking: {
1624
- // SQS doesn't have member IDs but we can track instance
1625
- groupInstanceId: process.env.ECS_TASK_ID || 'local',
1626
- },
1627
- };
1628
-
1629
- expect(config.consumerGroupTracking?.groupInstanceId).toBeDefined();
1630
- });
1631
-
1632
- it('should support RabbitMQ with group tracking', () => {
1633
- const config: ConsumerConfig = {
1634
- system: 'rabbitmq',
1635
- destination: 'orders',
1636
- consumerGroup: 'order-consumer',
1637
- consumerGroupTracking: {
1638
- memberId: () => 'rabbitmq-consumer-tag-123',
1639
- },
1640
- };
1641
-
1642
- expect(config.consumerGroupTracking?.memberId).toBeDefined();
1643
- });
1644
- });
1645
- });
1646
- });