|
| 1 | +import type { Envelope } from '@sentry/core'; |
| 2 | +import { expect, it } from 'vitest'; |
| 3 | +import { createRunner } from '../../runner'; |
| 4 | + |
| 5 | +function envelopeItemType(envelope: Envelope): string | undefined { |
| 6 | + return envelope[1][0]?.[0]?.type as string | undefined; |
| 7 | +} |
| 8 | + |
| 9 | +function envelopeItem(envelope: Envelope): Record<string, unknown> { |
| 10 | + return envelope[1][0]![1] as Record<string, unknown>; |
| 11 | +} |
| 12 | + |
| 13 | +function findPublishSpan(envelope: Envelope): Record<string, unknown> | undefined { |
| 14 | + if (envelopeItemType(envelope) !== 'transaction') return undefined; |
| 15 | + const tx = envelopeItem(envelope); |
| 16 | + const spans = (tx.spans as Array<Record<string, unknown>>) || []; |
| 17 | + return spans.find(s => (s.op as string) === 'queue.publish'); |
| 18 | +} |
| 19 | + |
| 20 | +function isConsumerTransaction(envelope: Envelope): boolean { |
| 21 | + if (envelopeItemType(envelope) !== 'transaction') return false; |
| 22 | + const tx = envelopeItem(envelope); |
| 23 | + return tx.transaction === 'process test-queue'; |
| 24 | +} |
| 25 | + |
| 26 | +it('captures errors thrown by the queue handler with the correct mechanism', async ({ signal }) => { |
| 27 | + const runner = createRunner(__dirname) |
| 28 | + .ignore('transaction') |
| 29 | + .expect((envelope: Envelope) => { |
| 30 | + expect(envelopeItemType(envelope)).toBe('event'); |
| 31 | + const event = envelopeItem(envelope); |
| 32 | + expect(event).toMatchObject({ |
| 33 | + level: 'error', |
| 34 | + exception: { |
| 35 | + values: [ |
| 36 | + { |
| 37 | + type: 'Error', |
| 38 | + value: 'Boom from queue handler', |
| 39 | + mechanism: { type: 'auto.faas.cloudflare.queue', handled: false }, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + }); |
| 44 | + }) |
| 45 | + .start(signal); |
| 46 | + |
| 47 | + await runner.makeRequest('post', '/enqueue/error'); |
| 48 | + await runner.completed(); |
| 49 | +}); |
| 50 | + |
| 51 | +it('emits a queue.publish span on env.MY_QUEUE.send and a queue.process transaction on the consumer', async ({ |
| 52 | + signal, |
| 53 | +}) => { |
| 54 | + const runner = createRunner(__dirname) |
| 55 | + .unordered() |
| 56 | + .expect((envelope: Envelope) => { |
| 57 | + // Producer transaction must contain a queue.publish child span |
| 58 | + const publishSpan = findPublishSpan(envelope); |
| 59 | + expect(publishSpan).toBeDefined(); |
| 60 | + expect(publishSpan).toMatchObject({ |
| 61 | + op: 'queue.publish', |
| 62 | + description: 'send MY_QUEUE', |
| 63 | + data: expect.objectContaining({ |
| 64 | + 'messaging.system': 'cloudflare', |
| 65 | + 'messaging.destination.name': 'MY_QUEUE', |
| 66 | + 'messaging.operation.type': 'send', |
| 67 | + 'messaging.operation.name': 'send', |
| 68 | + 'sentry.origin': 'auto.faas.cloudflare.queue', |
| 69 | + }), |
| 70 | + }); |
| 71 | + }) |
| 72 | + .expect((envelope: Envelope) => { |
| 73 | + expect(isConsumerTransaction(envelope)).toBe(true); |
| 74 | + const tx = envelopeItem(envelope); |
| 75 | + const trace = (tx.contexts as Record<string, Record<string, unknown>>).trace as Record<string, unknown>; |
| 76 | + expect(trace).toMatchObject({ |
| 77 | + op: 'queue.process', |
| 78 | + origin: 'auto.faas.cloudflare.queue', |
| 79 | + data: expect.objectContaining({ |
| 80 | + 'messaging.system': 'cloudflare', |
| 81 | + 'messaging.destination.name': 'test-queue', |
| 82 | + 'messaging.operation.type': 'process', |
| 83 | + 'messaging.operation.name': 'process', |
| 84 | + 'messaging.batch.message_count': 1, |
| 85 | + 'faas.trigger': 'pubsub', |
| 86 | + }), |
| 87 | + }); |
| 88 | + }) |
| 89 | + .start(signal); |
| 90 | + |
| 91 | + await runner.makeRequest('post', '/enqueue/ok'); |
| 92 | + await runner.completed(); |
| 93 | +}); |
| 94 | + |
| 95 | +it('emits a queue.publish span with batch attributes on env.MY_QUEUE.sendBatch', async ({ signal }) => { |
| 96 | + const runner = createRunner(__dirname) |
| 97 | + .unordered() |
| 98 | + .expect((envelope: Envelope) => { |
| 99 | + const publishSpan = findPublishSpan(envelope); |
| 100 | + expect(publishSpan).toBeDefined(); |
| 101 | + expect(publishSpan).toMatchObject({ |
| 102 | + op: 'queue.publish', |
| 103 | + description: 'send MY_QUEUE', |
| 104 | + data: expect.objectContaining({ |
| 105 | + 'messaging.system': 'cloudflare', |
| 106 | + 'messaging.destination.name': 'MY_QUEUE', |
| 107 | + 'messaging.operation.type': 'send', |
| 108 | + 'messaging.operation.name': 'send', |
| 109 | + 'messaging.batch.message_count': 3, |
| 110 | + 'sentry.origin': 'auto.faas.cloudflare.queue', |
| 111 | + }), |
| 112 | + }); |
| 113 | + }) |
| 114 | + .expect((envelope: Envelope) => { |
| 115 | + expect(isConsumerTransaction(envelope)).toBe(true); |
| 116 | + const tx = envelopeItem(envelope); |
| 117 | + const trace = (tx.contexts as Record<string, Record<string, unknown>>).trace as Record<string, unknown>; |
| 118 | + expect(trace).toMatchObject({ |
| 119 | + data: expect.objectContaining({ |
| 120 | + 'messaging.batch.message_count': 3, |
| 121 | + }), |
| 122 | + }); |
| 123 | + }) |
| 124 | + .start(signal); |
| 125 | + |
| 126 | + await runner.makeRequest('post', '/enqueue/batch'); |
| 127 | + await runner.completed(); |
| 128 | +}); |
0 commit comments